If you like our work, check out our Redux-based router redux-first-router or its sucessor which, Rudy # extract-css-chunks-webpack-plugin

Version Build Status Downloads License License

🍾🍾🍾It's our absolute pleasure to announce Webpack 4 Support πŸš€πŸš€πŸš€

> **HEADLINES (May 2018): Now Independently supports Webpack 4:** > Yep that's right. The universal family is now fully Webpack 4. Thank you to all our users for your loyalty and patience! If you love Universal, then you are gonna fall head over heels when we bring out the main course! So... why did we rebuild `extract-css-chunks-webpack-plugin`? What does it offer? It's got all the goodness of `mini-css-extract-plugin` but with 2 gleaming, sought after benefits. Compared to the existing loaders, we are offering a single solution as opposed to needing to depend on multiple loaders to cater for different features: ## Perks - **HMR:** It also has first-class support for **Hot Module Replacement** across ALL those css files/chunks!!! - cacheable stylesheets - smallest total bytes sent compared to "render-path" css-in-js solutions that include your CSS definitions in JS - Faster than the V2! - Async loading - No duplicate compilation (performance) - Easier to use - Specific to CSS - SSR Friendly development build, focused on frontend DX - Works seamlessly with the Universal family - Works fantastically as a standalone style loader (You can use it for any webpack project! with no extra dependencies!) - Async styles do not render block webkit browsers, if you use the `insert` option Additionally, if you are already a user of the universal family -- we will be waving goodbye to the mandatory `window.__CSS_CHUNKS__`. The functionality is still available to you via chunk flushing, and it can come in super handy when needing to easily resolve style assets as urls that might need to be passed to a third party. **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [new ExtractCssChunks()], module: { rules: [ { test: /\.css$/i, use: [ExtractCssChunks.loader, 'css-loader'], }, ], }, }; ``` ## Options ### `publicPath` Type: `String|Function` Default: the `publicPath` in `webpackOptions.output` Specifies a custom public path for the target file(s). #### `String` **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', chunkFilename: '[id].css', }), ], module: { rules: [ { test: /\.css$/, use: [ { loader: ExtractCssChunks.loader, options: { publicPath: '/public/path/to/', }, }, 'css-loader', ], }, ], }, }; ``` #### `Function` **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', chunkFilename: '[id].css', }), ], module: { rules: [ { test: /\.css$/, use: [ { loader: ExtractCssChunks.loader, options: { publicPath: (resourcePath, context) => { return path.relative(path.dirname(resourcePath), context) + '/'; }, }, }, 'css-loader', ], }, ], }, }; ``` ### `esModule` Type: `Boolean` Default: `false` By default, `extract-css-chunks-webpack-plugin` generates JS modules that use the CommonJS modules syntax. There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/). You can enable a ES module syntax using: **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [new ExtractCssChunks()], module: { rules: [ { test: /\.css$/i, use: [ { loader: ExtractCssChunks.loader, options: { esModule: true, }, }, 'css-loader', ], }, ], }, }; ``` ## Examples ### Minimal example **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // all options are optional filename: '[name].css', chunkFilename: '[id].css', ignoreOrder: false, // Enable to remove warnings about conflicting order }), ], module: { rules: [ { test: /\.css$/, use: [ { loader: ExtractCssChunks.loader, options: { // you can specify a publicPath here // by default it uses publicPath in webpackOptions.output publicPath: '../', hmr: process.env.NODE_ENV === 'development', }, }, 'css-loader', ], }, ], }, }; ``` ### The `publicPath` option as function **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', chunkFilename: '[id].css', }), ], module: { rules: [ { test: /\.css$/, use: [ { loader: ExtractCssChunks.loader, options: { publicPath: (resourcePath, context) => { // publicPath is the relative path of the resource to the context // e.g. for ./css/admin/main.css the publicPath will be ../../ // while for ./css/main.css the publicPath will be ../ return path.relative(path.dirname(resourcePath), context) + '/'; }, }, }, 'css-loader', ], }, ], }, }; ``` ### Advanced configuration example This plugin should be used only on `production` builds without `style-loader` in the loaders chain, especially if you want to have HMR in `development`. Here is an example to have both HMR in `development` and your styles extracted in a file for `production` builds. (Loaders options left out for clarity, adapt accordingly to your needs.) **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); const devMode = process.env.NODE_ENV !== 'production'; module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: devMode ? '[name].css' : '[name].[hash].css', chunkFilename: devMode ? '[id].css' : '[id].[hash].css', }), ], module: { rules: [ { test: /\.(sa|sc|c)ss$/, use: [ { loader: ExtractCssChunks.loader, options: { hmr: process.env.NODE_ENV === 'development', }, }, 'css-loader', 'postcss-loader', 'sass-loader', ], }, ], }, }; ``` ### Hot Module Reloading (HMR) The `extract-css-chunks-webpack-plugin` supports hot reloading of actual css files in development. Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules. Below is an example configuration of `extract-css-chunks` for HMR use with CSS modules. While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names. `reloadAll` is an option that should only be enabled if HMR isn't working correctly. The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename. **webpack.config.js** ```js const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); module.exports = { plugins: [ new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', chunkFilename: '[id].css', }), ], module: { rules: [ { test: /\.css$/, use: [ { loader: ExtractCssChunks.loader, options: { // only enable hot in development hmr: process.env.NODE_ENV === 'development', // if hmr does not work, this is a forceful method. reloadAll: true, }, }, 'css-loader', ], }, ], }, }; ``` ### Minimizing For Production To minify the output, use a plugin like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin). Setting `optimization.minimizer` overrides the defaults provided by webpack, so make sure to also specify a JS minimizer: **webpack.config.js** ```js const TerserJSPlugin = require('terser-webpack-plugin'); const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); module.exports = { optimization: { minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})], }, plugins: [ new ExtractCssChunks({ filename: '[name].css', chunkFilename: '[id].css', }), ], module: { rules: [ { test: /\.css$/, use: [ExtractCssChunks.loader, 'css-loader'], }, ], }, }; ``` ### Using preloaded or inlined CSS The runtime code detects already added CSS via `` or `