Snazzy stylus workflow with webpack
Life-changing CSS workflow, leveraging the magic of Stylus,
Webpack, and CSS Modules.
Stylepack gives you:
npm install --save stylepack stylus
Simple example:
// webpack.config.js
const webpackConfig = {
// ... your existing webpack configuration ...
};
// Pass the webpack config object through this function to add
// stylus & CSS module goodness to your webpack setup:
const applyStylepack = require('stylepack')({
webpack: require('webpack'),
// optional stylepack config goes here
})
module.exports = applyStylepack(webpackConfig)
Now, say you have a foo.styl
file in your project as follows:
.bar
color: red
font-weight: bold
Then in your client-side JS code, you can import this stylus file, and webpack
will compile and bundle it automatically. The default import will be a hash of
classnames that you can use in your JavaScript (eg. for React components):
import classes from './foo.styl'
document.body.classList.add(classes.bar)
The stylepack function takes an optional configuration object, with the following
supported keys:
cssModules
- Set this to true
to enable CSS Modulesvars
- An object containing variables that will be available to the stylus files.Note: nested objects and arrays are not yet supported within the vars option
fileMatchRegex
- A RegExp used to match files that Stylepack will process./\.styl$/
)extractTo
- The name of a CSS file to extract the style rules into. Usually you only want