Created
June 15, 2018 08:39
-
-
Save Grsmto/8e7ade37862f1c8b4d019da8dd423450 to your computer and use it in GitHub Desktop.
Next.js support for css-modules & normal css at the same time, same behaviour as CRA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
const cssLoaderConfig = require('@zeit/next-css/css-loader-config'); | |
const commonsChunkConfig = (config, test = /\.css$/) => { | |
config.plugins = config.plugins.map(plugin => { | |
if ( | |
plugin.constructor.name === 'CommonsChunkPlugin' && | |
// disable filenameTemplate checks here because they never match | |
// (plugin.filenameTemplate === 'commons.js' || | |
// plugin.filenameTemplate === 'main.js') | |
// do check for minChunks though, because this has to (should?) exist | |
plugin.minChunks != null | |
) { | |
const defaultMinChunks = plugin.minChunks; | |
plugin.minChunks = (module, count) => { | |
if (module.resource && module.resource.match(test)) { | |
return true; | |
} | |
return defaultMinChunks(module, count); | |
}; | |
} | |
return plugin; | |
}); | |
return config; | |
}; | |
module.exports = { | |
webpack: (config, options) => { | |
const { dev, isServer } = options; | |
extractCSSPlugin = new ExtractTextPlugin({ | |
filename: 'static/style.css' | |
}); | |
config.plugins.push(extractCSSPlugin); | |
options.extractCSSPlugin = extractCSSPlugin; | |
if (!isServer) { | |
config = commonsChunkConfig(config); | |
} | |
options.defaultLoaders.css = cssLoaderConfig( | |
config, | |
options.extractCSSPlugin, | |
{} | |
); | |
config.module.rules.push( | |
{ | |
test: /\.css$/, | |
exclude: /\.module\.css$/, | |
use: options.defaultLoaders.css | |
}, | |
{ | |
test: /\.module\.css$/, | |
use: cssLoaderConfig(config, extractCSSPlugin, { | |
cssModules: true, | |
dev, | |
isServer | |
}) | |
} | |
); | |
config = commonsChunkConfig(config, /\.(module\.css|css)$/); | |
return config; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Guys, just found this issue inside NextJS:
vercel/next-plugins#149
And also, @webdeb created this package:
https://github.com/webdeb/next-styles
Just installed and worked flawless - using NextJS version
9.2.0