diff --git a/.editorconfig b/.editorconfig
index 4d3466a5..9f89f364 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,17 +1,13 @@
-# EditorConfig is awesome: http://EditorConfig.org
+# editorconfig.org
-# top-most EditorConfig file
-root = true
-
-# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
+indent_style = space
+indent_size = 2
end_of_line = lf
insert_final_newline = true
-indent_style = tab
-indent_size = 4
+trim_trailing_whitespace = true
-# Matches the exact files either package.json or .travis.yml
-[{package.json,.travis.yml}]
-indent_style = space
-indent_size = 2
+[*.md]
+insert_final_newline = true
+trim_trailing_whitespace = false
diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 00000000..8477d2ab
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1,6 @@
+/coverage
+/dist
+/node_modules
+/test/fixtures
+/test/cases/*/expected
+/test/js
diff --git a/.eslintrc b/.eslintrc
deleted file mode 100644
index e325bf44..00000000
--- a/.eslintrc
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "env": {
- "node": true
- },
- "rules": {
- "strict": 0,
- "curly": 0,
- "quotes": 0,
- "no-shadow": 0,
- "no-underscore-dangle": 0
- }
-}
diff --git a/.eslintrc.js b/.eslintrc.js
new file mode 100644
index 00000000..c897f8ee
--- /dev/null
+++ b/.eslintrc.js
@@ -0,0 +1,5 @@
+module.exports = {
+ root: true,
+ parser: 'babel-eslint',
+ extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
+};
diff --git a/.gitattributes b/.gitattributes
index 176a458f..c76f8a99 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,5 @@
+package-lock.json -diff
* text=auto
+test/cases/* eol=lf
+bin/* eol=lf
+yarn.lock -diff
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
new file mode 100644
index 00000000..57da4905
--- /dev/null
+++ b/.github/CODEOWNERS
@@ -0,0 +1,2 @@
+# extract-css-chunks-webpack-plugin maintainers
+* @ScriptedAlchemy
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
deleted file mode 100644
index f0c0b39c..00000000
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ /dev/null
@@ -1,4 +0,0 @@
-1. [Read and sign the CLA](https://cla.js.foundation/webpack/webpack.js.org). This needs to be done only once. PRs that haven't signed it won't be accepted.
-2. Check out the [development guide](https://webpack.js.org/development/) for the API and development guidelines.
-3. Read through the PR diff carefully as sometimes this can reveal issues. The work will be reviewed, but this can save some effort.
-4. Remove these instructions from your PR as they are for your eyes only.
diff --git a/.gitignore b/.gitignore
index e8c5dd92..bc4f57a7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,18 @@
-/node_modules
-
-/example/assets
-
-/test/js
+logs
+*.log
+npm-debug.log*
+.eslintcache
/coverage
-
-/.idea
-
+/dist
+/test/js
+/local
+/reports
+/node_modules
.DS_Store
-npm-debug.log
+Thumbs.db
+.idea
+.vscode
+*.sublime-project
+*.sublime-workspace
+.idea
+*.iml
diff --git a/.npmignore b/.npmignore
index 61a3bec4..3ef3ec08 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,3 +1,6 @@
example/
test/
.gitattributes
+.git
+.github
+coverage/
diff --git a/.prettierignore b/.prettierignore
new file mode 100644
index 00000000..41ff7724
--- /dev/null
+++ b/.prettierignore
@@ -0,0 +1,7 @@
+/coverage
+/dist
+/node_modules
+/test/fixtures
+/test/cases/*/expected
+/test/js
+CHANGELOG.md
diff --git a/.prettierrc b/.prettierrc
new file mode 100644
index 00000000..e537c8ad
--- /dev/null
+++ b/.prettierrc
@@ -0,0 +1,5 @@
+{
+ "singleQuote": true,
+ "trailingComma": "es5",
+ "arrowParens": "always"
+}
diff --git a/.prettierrc.js b/.prettierrc.js
new file mode 100644
index 00000000..1934550a
--- /dev/null
+++ b/.prettierrc.js
@@ -0,0 +1,5 @@
+module.exports = {
+ singleQuote: true,
+ trailingComma: 'es5',
+ arrowParens: 'always',
+};
diff --git a/.snyk b/.snyk
new file mode 100644
index 00000000..5c89b3d5
--- /dev/null
+++ b/.snyk
@@ -0,0 +1,12 @@
+# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
+version: v1.13.5
+ignore: {}
+# patches apply the minimum changes required to fix a vulnerability
+patch:
+ SNYK-JS-LODASH-450202:
+ - webpack-external-import > babel-traverse > lodash:
+ patched: '2019-07-04T06:20:01.150Z'
+ - webpack-external-import > @babel/helper-module-imports > @babel/types > lodash:
+ patched: '2019-07-04T06:20:01.150Z'
+ - webpack-external-import > babel-traverse > babel-types > lodash:
+ patched: '2019-07-04T06:20:01.150Z'
diff --git a/.travis.yml b/.travis.yml
index 5c74368e..2969668d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,11 +1,23 @@
sudo: false
language: node_js
node_js:
- - stable
+ - v10
+before_script:
+ - nvm use v8
+ - yarn --ignore-engines
script:
- - echo success
+ - yarn lint
+ - yarn test
after_success:
- - yarn run semantic-release
+ - npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
+notifications:
+ email: false
+ webhooks:
+ urls:
+ - https://webhooks.gitter.im/e/5156be73e058008e1ed2
+ on_success: always # options: [always|never|change] default: always
+ on_failure: always # options: [always|never|change] default: always
+ on_start: never # options: [always|never|change] default: always
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f0064cc0..1baef1c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,59 +1,151 @@
-# Change Log
+# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
-
-# [2.1.0](https://github.com/webpack/extract-text-webpack-plugin/compare/v2.0.0...v2.1.0) (2017-03-05)
+## [4.9.0](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/compare/v4.8.0...v4.9.0) (2020-12-07)
+
+
+### Features
+
+* adding federated stats ([#303](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/issues/303)) ([c17d755](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/commit/c17d75547a5efb5971d03555163ea4d685080706))
+* adding federated stats ([#304](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/issues/304)) ([1eab2d0](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/commit/1eab2d0b8868837de8f89f68c1ad14be741f3e78))
+
+
+### Bug Fixes
+
+* Check if bestMatch exists before pop() ([#294](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/issues/294)) ([ef82da9](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/commit/ef82da9068bd699344774d39f61b7ead8cf682cb))
+
+### [0.9.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.2...v0.9.0) (2019-12-20)
+
### Features
-* The plugin **filename** accepts a function now. [c9a19ad](https://github.com/webpack-contrib/extract-text-webpack-plugin/commit/c9a19ad), closes [#423](https://github.com/webpack-contrib/extract-text-webpack-plugin/pull/423)
+* new `esModule` option ([#475](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/475)) ([596e47a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/596e47a8aead53f9cc0e2b1e09a2c20e455e45c1))
+
+### [0.8.2](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.1...v0.8.2) (2019-12-17)
-
-# [2.0.0](https://github.com/webpack/extract-text-webpack-plugin/compare/v2.0.0-rc.3...v2.0.0) (2017-02-24)
-
-# [2.0.0-rc.2](https://github.com/webpack/extract-text-webpack-plugin/compare/v2.0.0-rc.1...v2.0.0-rc.2) (2017-01-28)
+### Bug Fixes
+
+* context for dependencies ([#474](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/474)) ([0269860](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0269860adb0eaad477901188eea66693fedf7769))
+
+### [0.8.1](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.0...v0.8.1) (2019-12-17)
### Bug Fixes
-* **schema:** allow `extract` to accept omit/remove flags ([8ce93d5](https://github.com/webpack/extract-text-webpack-plugin/commit/8ce93d5)), closes [#371](https://github.com/webpack/extract-text-webpack-plugin/issues/371)
-* **schema:** connect loader schema with the code properly ([03bb4aa](https://github.com/webpack/extract-text-webpack-plugin/commit/03bb4aa))
-* **schema:** emit proper error messages ([70cbd4b](https://github.com/webpack/extract-text-webpack-plugin/commit/70cbd4b))
+* use filename mutated after instantiation ([#430](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/430)) ([0bacfac](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0bacfac7ef4a06b4810fbc140875f7a038caa5bc))
+* improve warning of conflict order ([#465](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/465)) ([357d073](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/357d073bf0259f2c44e613ad4dfcbcc8354e4be3))
+* support ES module syntax ([#472](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/472)) ([2f72e1a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/2f72e1aa267de23f121441714e88406f579e77b2))
+
+## [0.8.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.7.0...v0.8.0) (2019-07-16)
### Features
-* **errors:** show nicer errors if there are extra fields ([76a171d](https://github.com/webpack/extract-text-webpack-plugin/commit/76a171d))
+* Add ignoreOrder option ([#422](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/422)) ([4ad3373](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/4ad3373))
-
-# [2.0.0-rc.1](https://github.com/webpack/extract-text-webpack-plugin/compare/v2.0.0-rc.0...v2.0.0-rc.1) (2017-01-28)
+## [0.7.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.6.0...v0.7.0) (2019-05-27)
### Bug Fixes
-* **options:** pass proper loader options to children ([#266](https://github.com/webpack/extract-text-webpack-plugin/issues/266)) ([6abf42d](https://github.com/webpack/extract-text-webpack-plugin/commit/6abf42d))
+* do not attempt to reload unrequestable urls ([#378](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/378)) ([44d00ea](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/44d00ea))
+* fix `publicPath` regression ([#384](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/384)) ([582ebfe](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/582ebfe))
+* enable using plugin without defining options ([#393](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/393)) ([a7dee8c](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/a7dee8c))
+* downgrading normalize-url ([#399](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/399)) ([0dafaf6](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0dafaf6))
+* hmr do not crash on link without href ([#400](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/400)) ([aa9b541](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/aa9b541))
+* hmr reload with invalid link url ([#402](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/402)) ([30a19b0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/30a19b0))
+### Features
-
-# [2.0.0-rc.0](https://github.com/webpack/extract-text-webpack-plugin/compare/v2.0.0-beta.5...v2.0.0-rc.0) (2017-01-26)
+* add `moduleFilename` option ([#381](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/381)) ([13e9cbf](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/13e9cbf))
-### Bug Fixes
-* **readme:** Incorrect loader configuration ([e477cc7](https://github.com/webpack/extract-text-webpack-plugin/commit/e477cc7))
+
+# [0.6.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.5.0...v0.6.0) (2019-04-10)
### Features
-* **extract:** return an array of loader objects ([#343](https://github.com/webpack/extract-text-webpack-plugin/issues/343)) ([74b86e0](https://github.com/webpack/extract-text-webpack-plugin/commit/74b86e0))
+* added error code to chunk load Error ([#347](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/347)) ([b653641](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/b653641))
+* adding hot module reloading ([#334](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/334)) ([4ed9c5a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/4ed9c5a))
+* publicPath can be a function ([#373](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/373)) ([7b1425a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/7b1425a))
+
+
+
+
+# [0.5.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.5...v0.5.0) (2018-12-07)
+
+
+### Features
+
+* add crossOriginLoading option support ([#313](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/313)) ([ffb0d87](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/ffb0d87))
+
+
+
+
+## [0.4.5](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.4...v0.4.5) (2018-11-21)
+
+
+### Bug Fixes
+
+* **index:** allow requesting failed async css files ([#292](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/292)) ([2eb0af5](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/2eb0af5))
+
+
+
+
+## [0.4.4](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.3...v0.4.4) (2018-10-10)
+
+
+### Bug Fixes
+
+* **index:** assign empty `module.id` to prevent `contenthash` from changing unnecessarily ([#284](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/284)) ([d7946d0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/d7946d0))
+
+
+
+
+## [0.4.3](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.2...v0.4.3) (2018-09-18)
+
+
+### Bug Fixes
+
+* **loader:** pass `emitFile` to the child compilation (`loaderContext.emitFile`) ([#177](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/177)) ([18c066e](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/18c066e))
+
+
+
+
+## [0.4.2](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.0...v0.4.2) (2018-08-21)
+
+
+### Bug Fixes
+
+* use correct order when multiple chunk groups are merged ([#246](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/246)) ([c3b363d](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/c3b363d))
+
+
+
+
+## [0.4.1](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.0...v0.4.1) (2018-06-29)
+
+
+### Bug Fixes
+
+* CSS ordering with multiple entry points ([#130](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/130)) ([79373eb](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/79373eb))
# Change Log
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
+
+x.x.x /
+
+ Hot Module Replacement Hot Module Replacement + CSS modules
+ Lazy CSS: Must be red, but turn green when
+ .
+
+ But turn orange, when
+ . Additional
+ clicks have no effect.
+
+ Refresh and press buttons in reverse order: This should turn green
+ instead.
+
+ Lazy CSS: Turn off the network and
+ .
+ An error should have appeared.
+ Now if you turn the network back on and click it again, it should turn
+ aqua.
+ Preloaded CSS: Must be green.
+ displays
+ an alert and should turn red.
+ Preloaded inlined CSS: Must be green.
+ displays
+ an alert and should turn red.
+ CrossOriginLoading Option: Must be red.
+ loads chunks with crossorigin
+ attribute and should turn green.
+
+
+
+
+
+
-*Note: this is a companion package to:*
-- [webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks)
-- [react-loadable](https://github.com/thejameskyle/react-loadable)
+
+
+
-For a complete usage example, see the [Flush Chunks Boilerplate](https://github.com/faceyspacey/flush-chunks-boilerplate).
+
+
+
+
+
+
+
+
πΎπΎπΎ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',
+ ],
+ },
+ ],
+ },
+};
```
-If you use [webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks), it will scoop up the exact stylesheets to embed in your response string for you.
+#### `Function`
+**webpack.config.js**
-## Perks:
-- **HMR:** It also has first-class support for **Hot Module Replacement** across ALL those css files/chunks!!!
+```js
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
-- **2 VERSIONS OF YOUR JS CHUNKS:** In addition to generating CSS chunks, this plugin in fact creates 2 javascript chunks instead of 1. It leaves untouched your typical chunk that injects styles via `style-loader` and creates another chunk named `name.no_css.js`, which has all CSS removed, thereby greatly reducing its file size. This allows for future asynchronously-loaded bundles (which will be loaded *without* a css file) to also have their corresponding styles, ***while reducing the size of your initially served javascript chunks as much as possible*** π€
+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**
-## Installation
+```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',
+ ],
+ },
+ ],
+ },
+};
```
-yarn add --dev extract-css-chunks-webpack-plugin
+
+## 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**
-## Usage
```js
-const ExtractCssChunks = require("extract-css-chunks-webpack-plugin")
+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: ExtractCssChunks.extract({
- use: 'css-loader?modules&localIdentName=[name]__[local]--[hash:base64:5]',
- })
- }
- ]
+ 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,
- ]
-}
+ 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',
+ ],
+ },
+ ],
+ },
+};
```
-## API
-You can pass the same options as `extract-text-webpack-plugin` to `new ExtractCssChunks`, such as:
+### 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.
-```javascript
-new ExtractCssChunk({
- filename: '[name].[contenthash].css'
-})
+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',
+ ],
+ },
+ ],
+ },
+};
```
-Keep in mind we've added sensible defaults, specifically: `[name].css` is used when `process.env.NODE_ENV === 'deveopment'` and `[name].[contenthash].css` during production, so you can likely forget about having to pass anything.
+### Minimizing For Production
-The 2 exceptions are: `allChunks` will no longer do anything, and `fallback` will no longer do anything when passed to to `extract`. Basically just worry about passing your `css-loader` string and `localIdentName` π€
+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**
-## How It Works
+```js
+const TerserJSPlugin = require('terser-webpack-plugin');
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
+const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
-Just like your JS, it moves all the the required CSS into corresponding css chunk files. So entry chunks might be named: `main.12345.css` and dynamic split chunks would be named: `0.123456.css`, `1.123456.css`, etc. You will however now have 2 files for each javascript chunk: `0.no_css.js` and `0.js`. The former is what you should serve in the initial request (and what [webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks) in conjunction with [react-loadable](https://github.com/thejameskyle/react-loadable) will automatically serve). The latter, *which DOES contain css injection via style-loader*, is what will asyncronously be loaded in future async requests. This solves the fact that they otherwise would be missing CSS, since the webpack async loading mechanism isn't built to serve both a JS and CSS file. In total, 3 files are created for each named entry chunk and 3 files for each dynamically split chunk, e.g:
+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'],
+ },
+ ],
+ },
+};
+```
-**entry chunk:**
-- `main.js`
-- `main.no.js`
-- `main.css`
+### Using preloaded or inlined CSS
-**dynamic chunks**:
+The runtime code detects already added CSS via `` or `
+
+
+
+
+