Skip to content

Instantly share code, notes, and snippets.

@cdeutsch
Last active March 26, 2019 12:57
Show Gist options
  • Save cdeutsch/361fc44b545eb4a832212324d5f298d8 to your computer and use it in GitHub Desktop.
Save cdeutsch/361fc44b545eb4a832212324d5f298d8 to your computer and use it in GitHub Desktop.
Caching Webpack v4 css-loader CSS Modules with LESS
module: {
rules: [
{
test: /\.less$/,
use: [
...(process.env.NODE_ENV === 'production'
? [
{
loader: MiniCssExtractPlugin.loader,
},
]
: [
{
loader: 'style-loader',
},
{
loader: 'cache-loader',
},
]),
{
loader: 'css-loader',
options: {
minimize: process.env.NODE_ENV === 'production',
modules: true,
localIdentName: '[path][name]_[local]-[hash:base64:5]',
camelCase: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer],
},
},
{
loader: 'less-loader',
options: {
javascriptEnabled: true,
paths: [paths.appSrc, paths.appNodeModules],
modifyVars: {
'primary-color': '#9771EC',
'error-color': '#FC7272',
}
},
},
],
},
]
}
@cdeutsch
Copy link
Author

cdeutsch commented May 1, 2018

webpack-dev-server recompiles were really slow (10 seconds) because editing a single .tsx file caused all of the other .ts and .tsx files to recompile, which means any of them that require a .less file will trigger those files to recompile, which means webpack was constantly recompiling all the .less even if nothing .less related changed.

Adding cache-loader after css-loader brought recompiles down to 1.5 to 3 seconds.

Note, it seems to be important to have cache-loader right after css-loader, otherwise I didn't see a difference in recompile times.

This took 1.5 days to figure out and improve. 😭 😭 😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment