Install
Immutable styles can be integrated with Webpack using the Webpack plugin.
🔮 Support for other build systems (such as Parcel) will be added in future releases.
The first step is to install the dependency immutable-styles:
1
npm install immutable-styles
Copied!
Next, since immutable styles uses JSX the following devDependencies are required:
1
npm install --save-dev @babel/core
2
npm install --save-dev @babel/preset-env
3
npm install --save-dev @babel/plugin-transform-react-jsx
4
npm install --save-dev @babel/plugin-transform-react-jsx-source
5
npm install --save-dev babel-loader
Copied!

Note on devDependencies

It would be reasonable to assume the packages @babel/plugin-transform-react-jsx and @babel/plugin-transform-react-jsx-source indicate that immutable styles is coupled to React. Don't let the package names fool you! These packages enable immutable styles to use JSX and nothing more. Immutable styles is markup agnostic – meaning it isn’t coupled or biased to a specific way of generating HTML.

Configure Plugin

In webpack.config.js add the ImmutableStylesWebpackPlugin:
1
var { ImmutableStylesWebpackPlugin } = require('immutable-styles');
2
3
module.exports = {
4
plugins: [
5
new ImmutableStylesWebpackPlugin()
6
]
7
}
Copied!
Then enable source-maps and set nodes fs module to empty:
1
module.exports = {
2
/* ... */
3
devtool: "source-map",
4
node: {
5
fs: 'empty'
6
}
7
}
Copied!

Note on Source Maps

Source maps are required for compile-time errors. If source maps are not enabled the error Missing Source Maps will be thrown.
By default the CSS generated by immutable styles is located in dist/bundle.css. This can be optionally configured when initialising the plugin:
1
module.exports = {
2
plugins: [
3
new ImmutableStylesWebpackPlugin({
4
dist: './custom-path-to-file.css'
5
})
6
],
7
/* ... */
8
}
Copied!
Then make sure the immutable style sheets are passed through babel-loader:
1
module.exports = {
2
/* .. */
3
module: {
4
rules: [
5
{
6
test: /\.iss\.jsx)$/,
7
exclude: /node_modules/,
8
use: 'babel-loader'
9
}
10
]
11
},
12
/* ... */
13
]
Copied!
And lastly in .babelrc register the following presets and plugins:
1
{
2
"presets": [
3
[
4
"@babel/preset-env",
5
{
6
"modules": "commonjs"
7
}
8
]
9
],
10
"plugins": [
11
"@babel/plugin-transform-react-jsx",
12
"@babel/transform-react-jsx-source"
13
]
14
}
Copied!

Examples

Some examples of using immutable styles with Webpack can be found in the following repos:
Copy link