
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
postcss-classes-to-mixins
Advanced tools
PostCSS plugin to intelligently convert CSS classes to SASS, Less and Stylus mixins
PostCSS plugin to intelligently convert CSS classes to SASS, Less and Stylus mixins.
npm install --save-dev postcss postcss-classes-to-mixins
Input:
/* style.css */
.red-btn {
background: red;
color: white;
}
Output:
/* style.scss */
@mixin red-btn() {
background: red;
color: white;
}
import postcss from 'postcss'
import classesToMixins from 'postcss-classes-to-mixins'
import fs from 'fs'
const css = fs.readFileSync('style.css')
postcss([
classesToMixins({ // Object: options
scss: 'style.scss', // String: sass output
less: 'style.less', // String: less output
styl: 'style.styl', // String: stylus output
mixinsOnly: false // Defaults to false. true strips all non-classname selectors (like `body` etc.)
})
]).process(css, {from: 'undefined'})
.then((done) => console.log('done!'))
This will write the output to the file paths specified. See PostCSS docs for examples for your environment.
Now, import the exported stylesheet and start using the mixins:
/* my.scss */
@import 'style.scss'
.red-green-btn {
@include red-button;
color: green;
}
When sharing CSS between projects you often want to distribute it so people can consume it in Sass, Less and Stylus. This can be tricky without maintaining separate versions of the code written in each of these languages. Also, when loading different versions of the same stylesheet globally (by widgets on the same page) name space collisions will eventually occur, resulting in styling errors.
This plugin will solve that:
Versioning: Class names from the exported CSS are no longer global and won't collide with each other.
Instead they are built into your own classes by extending them. This also makes your HTML more clean, as you don't need to use several
classes (class="standard-btn red-btn my-btn"
) to style one thing.
Three shaking: Using mixins you only include the styles that you actually use in your project, which is nice for load. Loading the entire stylesheet globally (several times for each widget) is wasteful and not nice.
FAQs
PostCSS plugin to intelligently convert CSS classes to SASS, Less and Stylus mixins
We found that postcss-classes-to-mixins demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 147 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.