Skip to content

Commit cf9c3ea

Browse files
feat: Add ignoreOrder option (faceyspacey#184)
* feat: Add ignoreOrder option * fix: do not attempt to reload unrequestable urls * fix: fix publicPath regression * fix: enable using plugin without defining options * fix: downgrading normalize-url * fix: hmr do not crash on link without href * fix: hmr reload with invalid link url * feat: add moduleFilename option * chore: remove PR template * docs: updated documentation
1 parent b7045c5 commit cf9c3ea

File tree

8 files changed

+1180
-92
lines changed

8 files changed

+1180
-92
lines changed

.github/PULL_REQUEST_TEMPLATE.md

-4
This file was deleted.

README.md

+179-25
Original file line numberDiff line numberDiff line change
@@ -69,38 +69,37 @@ yarn add --dev extract-css-chunks-webpack-plugin
6969

7070
*webpack.config.js:*
7171
```js
72-
const ExtractCssChunks = require("extract-css-chunks-webpack-plugin")
73-
72+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
7473
module.exports = {
74+
plugins: [
75+
new MiniCssExtractPlugin({
76+
// Options similar to the same options in webpackOptions.output
77+
// all options are optional
78+
filename: '[name].css',
79+
chunkFilename: '[id].css',
80+
ignoreOrder: false, // Enable to remove warnings about conflicting order
81+
}),
82+
],
7583
module: {
7684
rules: [
7785
{
7886
test: /\.css$/,
7987
use: [
80-
{
81-
loader:ExtractCssChunks.loader,
82-
options: {
83-
hot: true, // if you want HMR
84-
reloadAll: true, // when desperation kicks in - this is a brute force HMR flag
85-
}
86-
},
87-
"css-loader"
88-
]
89-
}
90-
]
88+
{
89+
loader: MiniCssExtractPlugin.loader,
90+
options: {
91+
// you can specify a publicPath here
92+
// by default it uses publicPath in webpackOptions.output
93+
publicPath: '../',
94+
hot: process.env.NODE_ENV === 'development',
95+
},
96+
},
97+
'css-loader',
98+
],
99+
},
100+
],
91101
},
92-
plugins: [
93-
new ExtractCssChunks(
94-
{
95-
// Options similar to the same options in webpackOptions.output
96-
// both options are optional
97-
filename: "[name].css",
98-
chunkFilename: "[id].css",
99-
orderWarning: true, // Disable to remove warnings about conflicting order between imports
100-
}
101-
),
102-
]
103-
}
102+
};
104103
```
105104

106105
*webpack.server.config.js*
@@ -113,6 +112,161 @@ new webpack.optimize.LimitChunkCountPlugin({
113112
})
114113
```
115114

115+
#### `publicPath` function example
116+
117+
**webpack.config.js**
118+
119+
```js
120+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
121+
module.exports = {
122+
plugins: [
123+
new MiniCssExtractPlugin({
124+
// Options similar to the same options in webpackOptions.output
125+
// both options are optional
126+
filename: '[name].css',
127+
chunkFilename: '[id].css',
128+
}),
129+
],
130+
module: {
131+
rules: [
132+
{
133+
test: /\.css$/,
134+
use: [
135+
{
136+
loader: MiniCssExtractPlugin.loader,
137+
options: {
138+
publicPath: (resourcePath, context) => {
139+
// publicPath is the relative path of the resource to the context
140+
// e.g. for ./css/admin/main.css the publicPath will be ../../
141+
// while for ./css/main.css the publicPath will be ../
142+
return path.relative(path.dirname(resourcePath), context) + '/';
143+
},
144+
},
145+
},
146+
'css-loader',
147+
],
148+
},
149+
],
150+
},
151+
};
152+
```
153+
154+
#### Advanced configuration example
155+
156+
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`.
157+
158+
Here is an example to have both HMR in `development` and your styles extracted in a file for `production` builds.
159+
160+
(Loaders options left out for clarity, adapt accordingly to your needs.)
161+
162+
**webpack.config.js**
163+
164+
```js
165+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
166+
const devMode = process.env.NODE_ENV !== 'production';
167+
168+
module.exports = {
169+
plugins: [
170+
new MiniCssExtractPlugin({
171+
// Options similar to the same options in webpackOptions.output
172+
// both options are optional
173+
filename: devMode ? '[name].css' : '[name].[hash].css',
174+
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
175+
}),
176+
],
177+
module: {
178+
rules: [
179+
{
180+
test: /\.(sa|sc|c)ss$/,
181+
use: [
182+
{
183+
loader: MiniCssExtractPlugin.loader,
184+
options: {
185+
hot: process.env.NODE_ENV === 'development',
186+
},
187+
},
188+
'css-loader',
189+
'postcss-loader',
190+
'sass-loader',
191+
],
192+
},
193+
],
194+
},
195+
};
196+
```
197+
198+
#### Hot Module Reloading (HMR)
199+
200+
extract-mini-css-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 mini-css for HMR use with CSS modules.
201+
202+
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.
203+
204+
**webpack.config.js**
205+
206+
```js
207+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
208+
module.exports = {
209+
plugins: [
210+
new MiniCssExtractPlugin({
211+
// Options similar to the same options in webpackOptions.output
212+
// both options are optional
213+
filename: '[name].css',
214+
chunkFilename: '[id].css',
215+
}),
216+
],
217+
module: {
218+
rules: [
219+
{
220+
test: /\.css$/,
221+
use: [
222+
{
223+
loader: MiniCssExtractPlugin.loader,
224+
options: {
225+
// only enable hot in development
226+
hot: process.env.NODE_ENV === 'development',
227+
// if hmr does not work, this is a forceful method.
228+
reloadAll: true,
229+
},
230+
},
231+
'css-loader',
232+
],
233+
},
234+
],
235+
},
236+
};
237+
```
238+
239+
### Minimizing For Production
240+
241+
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:
242+
243+
**webpack.config.js**
244+
245+
```js
246+
const TerserJSPlugin = require('terser-webpack-plugin');
247+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
248+
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
249+
module.exports = {
250+
optimization: {
251+
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
252+
},
253+
plugins: [
254+
new MiniCssExtractPlugin({
255+
filename: '[name].css',
256+
chunkFilename: '[id].css',
257+
}),
258+
],
259+
module: {
260+
rules: [
261+
{
262+
test: /\.css$/,
263+
use: [MiniCssExtractPlugin.loader, 'css-loader'],
264+
},
265+
],
266+
},
267+
};
268+
```
269+
116270

117271
### What about Webpack 3?
118272
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.

package.json

+25-29
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
],
3333
"main": "dist/cjs.js",
3434
"engines": {
35-
"node": ">= 6.9.0 <7.0.0 || >= 8.9.0"
35+
"node": ">= 6.9.0"
3636
},
3737
"scripts": {
3838
"start": "npm run build -- -w",
@@ -57,9 +57,7 @@
5757
"ci:coverage": "npm run test:coverage -- --runInBand",
5858
"defaults": "webpack-defaults",
5959
"semantic-release": "npx semantic-release",
60-
"travis": "npm run ci:coverage",
61-
"snyk-protect": "snyk protect",
62-
"prepublish": "npm run snyk-protect"
60+
"travis": "npm run ci:coverage"
6361
},
6462
"files": [
6563
"dist"
@@ -71,48 +69,46 @@
7169
"loader-utils": "^1.1.0",
7270
"normalize-url": "1.9.1",
7371
"schema-utils": "^1.0.0",
74-
"webpack-external-import": "^0.0.1-beta.16",
75-
"webpack-sources": "^1.1.0"
72+
"webpack-sources": "^1.1.0",
73+
"webpack-external-import": "^0.0.1-beta.19"
7674
},
7775
"devDependencies": {
78-
"@babel/cli": "^7.4.4",
79-
"@babel/core": "^7.4.4",
80-
"@babel/preset-env": "^7.4.4",
81-
"@commitlint/cli": "^7.6.1",
82-
"@commitlint/config-conventional": "^7.6.0",
83-
"@webpack-contrib/defaults": "^4.0.1",
76+
"@babel/cli": "^7.5.0",
77+
"@babel/core": "^7.5.4",
78+
"@babel/preset-env": "^7.5.4",
79+
"@commitlint/cli": "^8.1.0",
80+
"@commitlint/config-conventional": "^8.1.0",
81+
"@webpack-contrib/defaults": "^5.0.2",
8482
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
85-
"acorn": "^6.1.1",
86-
"babel-eslint": "^10.0.1",
83+
"babel-eslint": "^10.0.2",
8784
"babel-jest": "^24.8.0",
88-
"commitlint-azure-pipelines-cli": "^1.0.1",
85+
"commitlint-azure-pipelines-cli": "^1.0.2",
8986
"cross-env": "^5.2.0",
90-
"css-loader": "^2.1.1",
87+
"css-loader": "^3.0.0",
9188
"del": "^4.1.1",
9289
"del-cli": "^1.1.0",
9390
"es-check": "^5.0.0",
94-
"eslint": "^5.16.0",
95-
"eslint-plugin-import": "^2.17.2",
96-
"eslint-plugin-prettier": "^3.1.0",
97-
"file-loader": "^3.0.1",
98-
"husky": "^2.2.0",
91+
"eslint": "^6.0.1",
92+
"eslint-config-prettier": "^6.0.0",
93+
"eslint-plugin-import": "^2.18.0",
94+
"file-loader": "^4.0.0",
95+
"husky": "^3.0.0",
9996
"jest": "^24.8.0",
10097
"jest-junit": "^6.4.0",
101-
"lint-staged": "^8.1.6",
98+
"lint-staged": "^9.2.0",
10299
"memory-fs": "^0.4.1",
103-
"prettier": "^1.17.0",
104-
"snyk": "^1.189.0",
100+
"npm-run-all": "^4.1.5",
101+
"prettier": "^1.18.2",
105102
"standard-version": "^6.0.1",
106-
"webpack": "^4.31.0",
107-
"webpack-cli": "^3.3.2",
108-
"webpack-dev-server": "^3.3.1"
103+
"webpack": "^4.35.3",
104+
"webpack-cli": "^3.3.6",
105+
"webpack-dev-server": "^3.7.2"
109106
},
110107
"pre-commit": "lint-staged",
111108
"lint-staged": {
112109
"*.js": [
113110
"eslint --fix",
114111
"git add"
115112
]
116-
},
117-
"snyk": true
113+
}
118114
}

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class ExtractCssChunksPlugin {
125125
{
126126
filename: DEFAULT_FILENAME,
127127
moduleFilename: () => this.options.filename || DEFAULT_FILENAME,
128+
ignoreOrder: false,
128129
},
129130
options
130131
);
@@ -530,7 +531,7 @@ class ExtractCssChunksPlugin {
530531
// use list with fewest failed deps
531532
// and emit a warning
532533
const fallbackModule = bestMatch.pop();
533-
if (this.options.orderWarning) {
534+
if (!this.options.ignoreOrder) {
534535
compilation.warnings.push(
535536
new Error(
536537
`chunk ${chunk.name || chunk.id} [${pluginName}]\n` +
@@ -544,6 +545,7 @@ class ExtractCssChunksPlugin {
544545
)
545546
);
546547
}
548+
547549
usedModules.add(fallbackModule);
548550
}
549551
}

test/cases/css-modules/expected/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
/* 0 */
8989
/***/ (function(module, exports, __webpack_require__) {
9090

91-
// extracted by extract-css-chunks-webpack-plugin
91+
// extracted by mini-css-extract-plugin
9292
module.exports = {"a-module":"index-a-module","b-module":"index-b-module"};
9393

9494
/***/ })

0 commit comments

Comments
 (0)