diff --git a/.babelrc b/.babelrc
deleted file mode 100644
index 0b054131..00000000
--- a/.babelrc
+++ /dev/null
@@ -1,35 +0,0 @@
-{
- "presets": [
- [
- "env",
- {
- "useBuiltIns": true,
- "targets": {
- "node": "6.11.5"
- },
- "exclude": [
- "transform-async-to-generator",
- "transform-regenerator"
- ]
- }
- ]
- ],
- "plugins": [
- [
- "transform-object-rest-spread",
- {
- "useBuiltIns": true
- }
- ]
- ],
- "env": {
- "test": {
- "presets": [
- "env"
- ],
- "plugins": [
- "transform-object-rest-spread"
- ]
- }
- }
-}
diff --git a/.editorconfig b/.editorconfig
index 79664df2..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 = space
-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
index 4a868dc8..8477d2ab 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -1 +1,6 @@
-test/js/**/*.js
+/coverage
+/dist
+/node_modules
+/test/fixtures
+/test/cases/*/expected
+/test/js
diff --git a/.eslintrc.js b/.eslintrc.js
index 12a376a4..c897f8ee 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -1,26 +1,5 @@
module.exports = {
- parser: 'babel-eslint',
- parserOptions: {
- ecmaFeatures: {
- generators: true,
- experimentalObjectRestSpread: true
- },
- sourceType: 'module',
- allowImportExportEverywhere: false
- },
- extends: ['airbnb'],
- env: {
- 'browser': true,
- },
- rules: {
- 'no-param-reassign': 0,
- 'func-names': 0,
- 'no-underscore-dangle': 0,
- 'no-restricted-syntax': 0,
- 'prefer-arrow-callback': 0,
- 'prefer-destructuring': 0,
- 'array-callback-return': 0,
- 'prefer-template': 0,
- 'class-methods-use-this': 0
- }
+ 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/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 345c30b8..bc4f57a7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,12 +1,18 @@
-/node_modules
-
-/test/js
+logs
+*.log
+npm-debug.log*
+.eslintcache
/coverage
/dist
-
-.idea
-.eslintcache
-
+/test/js
+/local
+/reports
+/node_modules
.DS_Store
-*.log
-.eslintcache
+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 1a8fb8d2..2969668d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,11 +1,15 @@
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+$/
@@ -14,6 +18,6 @@ notifications:
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
+ 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 e34f23d5..1baef1c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,11 +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.
-
-# [3.2.0](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/compare/v3.1.2...v3.2.0) (2018-10-08)
+## [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
+
+* 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)
+
+
+### 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
+
+* 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
+
+* 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))
+
+
+
+## [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
+
+* 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
+
+* 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))
+
+
+
+
+# [0.6.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.5.0...v0.6.0) (2019-04-10)
+
+
+### Features
+
+* 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
-* **readme:** Documenting new options that have been added ([#112](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/issues/112)) ([94ee96e](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/commit/94ee96e))
+* 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 / --
+==================
+
+ * Bug fix -
+ * Feature -
+ * Chore -
+ * Docs -
diff --git a/LICENSE b/LICENSE
index 8c11fc72..dfc572fb 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
Copyright JS Foundation and other contributors
+Copyright Zack Jackson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/README.md b/README.md
index 549a7951..112bbdc2 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,4 @@
-
-
-
-
+If you like our work, check out our Redux-based router redux-first-router or its sucessor which, Rudy
# extract-css-chunks-webpack-plugin
@@ -9,7 +6,7 @@
-
+
@@ -29,359 +26,594 @@
πΎπΎπΎ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!
+> **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`? What does it offer?
+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.
+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!)
-
-Additionally, if you are already a user of the universal family -- we will be waving goodbye to the mandatory ```window.__CSS_CHUNKS__```.
+
+- **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 4 Standalone Installation:
+**webpack.config.js**
-If you are just looking for something that works like `mini-css-extract-plugin` but with HMR, then look no further.
+```js
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
-NOTE: We have aligned out loader implementation to be the same as `mini-css-extract-plugin`
+module.exports = {
+ plugins: [new ExtractCssChunks()],
+ module: {
+ rules: [
+ {
+ test: /\.css$/i,
+ use: [ExtractCssChunks.loader, 'css-loader'],
+ },
+ ],
+ },
+};
+```
-**If you already use `mini-css-extract-plugin`, then you can just change the `require` statement - it's that easy**
+## Options
-```
-yarn add --dev extract-css-chunks-webpack-plugin
-```
+### `publicPath`
+
+Type: `String|Function`
+Default: the `publicPath` in `webpackOptions.output`
+
+Specifies a custom public path for the target file(s).
+
+#### `String`
+
+**webpack.config.js**
-*webpack.config.js:*
```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.loader,
- "css-loader"
- ]
- }
- ]
+ {
+ loader: ExtractCssChunks.loader,
+ options: {
+ publicPath: '/public/path/to/',
+ },
+ },
+ 'css-loader',
+ ],
+ },
+ ],
},
- plugins: [
- new ExtractCssChunks(
- {
- // Options similar to the same options in webpackOptions.output
- // both options are optional
- filename: "[name].css",
- chunkFilename: "[id].css",
- hot: true, // if you want HMR - we try to automatically inject hot reloading but if it's not working, add it to the config
- orderWarning: true, // Disable to remove warnings about conflicting order between imports
- reloadAll: true, // when desperation kicks in - this is a brute force HMR flag
- cssModules: true // if you use cssModules, this can help.
- }
- ),
- ]
-}
+};
```
-*webpack.server.config.js*
+#### `Function`
-The server needs to be handled differently, we still want one chunk. Luckily webpack 4 supports **LimitChunkCountPlugin**
+**webpack.config.js**
```js
-new webpack.optimize.LimitChunkCountPlugin({
- maxChunks: 1
-})
+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`
-### What about Webpack 3?
-This is a breaking change. The entire loader has been fundamentally rewritten specifically for Webpack 4. Aiming to support our existing user base, allowing them to upgrade their infrastructure to support Webpack 4 based universally code-split server-side rendered react applications.
+Type: `Boolean`
+Default: `false`
-There have been some challenges along the way since the release of webpack 4. Ultimately the only remaining hurdle is code split, async style loading.
+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/).
-If you do need Webpack 3, make sure to stick with the latest `v2.x.x` release. `v3.x.x` is only intended for users with Webpack 4
+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',
+ ],
+ },
+ ],
+ },
+};
+```
-*Note: this is a companion package to:*
-- [webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks)
-- [react-universal-component](https://github.com/faceyspacey/react-universal-component)
-- [babel-plugin-universal-import](https://github.com/faceyspacey/babel-plugin-universal-import) ***or*** [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import)
+## Examples
+### Minimal example
-## Recommended Installation For Universal
-```
-yarn add react-universal-component webpack-flush-chunks
-yarn add --dev extract-css-chunks-webpack-plugin babel-plugin-universal-import
-```
+**webpack.config.js**
-*.babelrc:*
```js
-{
- "plugins": ["universal-import"]
-}
+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 main thing is you need to cater to the new chunking system of webpack!
-With **webpack.optimize.CommonsChunkPlugin** plugin no longer part of Webpack 4, we need another way to define the code-splitting. Luckily we have `optimization` configs built into webpack now
-> There are more aggressive ways to code split, thanks to webpack 4. Check out the RUC Readme if you are looking for a more aggressive code split optimization configuration. These are just examples, you should be able to configure them as you see fit
+### The `publicPath` option as function
+
+**webpack.config.js**
-*webpack.config.js:*
```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.loader,
{
- loader: 'css-loader',
+ loader: ExtractCssChunks.loader,
options: {
- modules: true,
- localIdentName: '[name]__[local]--[hash:base64:5]',
+ 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',
],
},
],
},
- optimization: {
- // FOR PRODUCTION
- minimizer: [
- new UglifyJSPlugin({
- uglifyOptions: {
- output: {
- comments: false,
- ascii_only: true
- },
- compress: {
- comparisons: false
- }
- }
- })
- ],
- // END
- // NEEDED BOTH IN PROD AND DEV BUILDS
- runtimeChunk: {
- name: 'bootstrap'
- },
- splitChunks: {
- chunks: 'initial',
- cacheGroups: {
- vendors: {
- test: /[\\/]node_modules[\\/]/,
- name: 'vendor'
- }
- }
- }
- },
- plugins: [
- new ExtractCssChunks({hot:true, cssModules: true}), //if you want HMR - we try to automatically inject hot reloading but if it's not working, add it to the config
- ]
};
```
-## Desired Output
-Here's the sort of CSS you can expect to serve:
-
-```
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-```
+### Advanced configuration example
-If you need to resolve your stylesheets on the client side, for whatever reason.
+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`.
-[webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks) will scoop up the exact stylesheets to embed in your response. It will give you an object which can be embedded on page
+Here is an example to have both HMR in `development` and your styles extracted in a file for `production` builds.
-Here's how you do it:
+(Loaders options left out for clarity, adapt accordingly to your needs.)
+**webpack.config.js**
-*src/components/App.js:*
```js
-const UniversalComponent = universal(props => import(`./${props.page}`))
-
-
-```
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
+const devMode = process.env.NODE_ENV !== 'production';
-*server/render.js:*
-```js
-import { flushChunkNames } from 'react-universal-component/server'
-import flushChunks from 'webpack-flush-chunks'
-
-const app = ReactDOMServer.renderToString()
-const { js, styles, cssHash } = flushChunks(webpackStats, {
- chunkNames: flushChunkNames()
-})
-
-res.send(`
-
-
-
- ${styles}
-
-
-
${app}
-
- ${cssHash}
-
- ${js}
-
-
-`)
+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',
+ ],
+ },
+ ],
+ },
+};
```
-***As for asynchronous calls to `import()` on user navigation,*** [babel-plugin-universal-import](https://github.com/faceyspacey/babel-plugin-universal-import) is required if you're using [react-universal-component](https://github.com/faceyspacey/react-universal-component). And if you aren't, you must use: [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import).
+### 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.
-These babel plugins request both your js + your css. *Very Nice!* This is the new feature of the 2.0. Read *Sokra's* (author of webpack) article on how [on how this is the future of CSS for webpack](https://medium.com/webpack/the-new-css-workflow-step-1-79583bd107d7). Use this and be in the future today.
+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**
-## API
-You can pass the same options as `mini-css-extract` to `new ExtractCssChunks`, such as:
+```js
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
-```javascript
-new ExtractCssChunk({
- filename: devMode ? '[name].css' : '[name].[hash].css',
- chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
-})
+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, by default `[name].css` is used when `process.env.NODE_ENV === 'development'` and `[name].[contenthash].css` during production, so you can likely forget about having to pass anything.
+### 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:
-### HMR Pitfall
+**webpack.config.js**
-The most common workflow when working with webpack is to write a "development" / "production" value in to the
-'process.env.NODE_ENV' namespace, typically, using webpack's built-in "DefinePlugin" plugin. e.g:
+```js
+const TerserJSPlugin = require('terser-webpack-plugin');
+const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
+const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
-```javascript
-new webpack.DefinePlugin( {
- 'process.env': {
- NODE_ENV: `"${config.devMode ? 'development' : 'production'}"`,
+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'],
},
-} )
+ ],
+ },
+};
```
-The value set by the aforementioned plugin will only be available in the runtime ( when webpack's javascript output is executed ).
-In order for this plugin to work with hot module reloading, The npm script / gulp task / grunt task etc, should be invoked with the same environment variable as shown above.
-For example, when running the build using some form of npm script:
+### Using preloaded or inlined CSS
-```json
-{
- "scripts": {
- "build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files"
- }
-}
+The runtime code detects already added CSS via `` or `
+
+
+
+
+
+ Initial CSS: Must be green
+
+
+
Hot Module Replacement
+
RED
+
GREEN
+
BLUE
+
+
+
Hot Module Replacement + CSS modules
+
RED
+
GREEN
+
BLUE
+
+
+
+ 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.
+