Skip to content

Commit 1e56ccf

Browse files
Merge pull request faceyspacey#73 from faceyspacey/hmr-fix
fix: Manual HMR Option and updated Readme
2 parents c542197 + 5ef1635 commit 1e56ccf

File tree

4 files changed

+79
-27
lines changed

4 files changed

+79
-27
lines changed

README.md

+47-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</a>
2020

2121
<a href="https://www.npmjs.com/package/extract-css-chunks-webpack-plugin">
22-
<img src="https://img.shields.io/npm/dw/extract-css-chunks-webpack-plugin.svg" alt="License" />
22+
<img src="https://img.shields.io/npm/dm/extract-css-chunks-webpack-plugin.svg" alt="License" />
2323
</a>
2424

2525
<a href="https://www.npmjs.com/package/extract-css-chunks-webpack-plugin">
@@ -91,13 +91,23 @@ module.exports = {
9191
// Options similar to the same options in webpackOptions.output
9292
// both options are optional
9393
filename: "[name].css",
94-
chunkFilename: "[id].css"
94+
chunkFilename: "[id].css",
95+
hot: true // optional is the plguin cannot automatically detect if you are using HOT, not for production use
9596
}
9697
),
9798
]
9899
}
99100
```
100101

102+
*webpack.server.config.js*
103+
104+
The server needs to be handeled differently, we still want one chunks. Luckily webpack 4 supports **LimitChunkCountPlugin**
105+
106+
```js
107+
new webpack.optimize.LimitChunkCountPlugin({
108+
maxChunks: 1
109+
})
110+
```
101111

102112

103113
### What about Webpack 3?
@@ -116,7 +126,7 @@ If you do need Webpack 3, make sure to stick with the latest `v2.x.x` release. `
116126
- [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)
117127

118128

119-
## Recommended Installation
129+
## Recommended Installation For Universal
120130
```
121131
yarn add react-universal-component webpack-flush-chunks
122132
yarn add --dev extract-css-chunks-webpack-plugin babel-plugin-universal-import
@@ -128,6 +138,8 @@ yarn add --dev extract-css-chunks-webpack-plugin babel-plugin-universal-import
128138
"plugins": ["universal-import"]
129139
}
130140
```
141+
The main thing is you need to cater for the new chunking system of webpack!
142+
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
131143

132144
*webpack.config.js:*
133145
```js
@@ -151,8 +163,38 @@ module.exports = {
151163
},
152164
],
153165
},
166+
optimization: {
167+
// FOR PRODUCTION
168+
minimizer: [
169+
new UglifyJSPlugin({
170+
uglifyOptions: {
171+
output: {
172+
comments: false,
173+
ascii_only: true
174+
},
175+
compress: {
176+
comparisons: false
177+
}
178+
}
179+
})
180+
],
181+
// END
182+
// NEEDED BOTH IN PROD AND DEV BUILDS
183+
runtimeChunk: {
184+
name: 'bootstrap'
185+
},
186+
splitChunks: {
187+
chunks: 'initial', // <-- The key to this
188+
cacheGroups: {
189+
vendors: {
190+
test: /[\\/]node_modules[\\/]/,
191+
name: 'vendor'
192+
}
193+
}
194+
}
195+
},
154196
plugins: [
155-
new ExtractCssChunks(),
197+
new ExtractCssChunks({hot:true}), //if you want HMR - we try to automatically inject hot reloading but if its not working, add it to the config
156198
]
157199
};
158200
```
@@ -256,7 +298,7 @@ For example, when running the build using some form of npm script:
256298
```json
257299
{
258300
"scripts": {
259-
"build": "cross-env NODE_ENV=development webpack --config build/webpack.config.js"
301+
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files"
260302
}
261303
}
262304
```

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@
1313
"files": [
1414
"dist"
1515
],
16+
"keywords": [
17+
"css",
18+
"chunks",
19+
"code splitting",
20+
"mini-css",
21+
"hot",
22+
"hmr",
23+
"universal",
24+
"webpack",
25+
"webpack 4"
26+
],
1627
"license": "MIT",
1728
"scripts": {
1829
"start": "npm run build -- -w",
1930
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files",
2031
"clean": "del-cli dist",
21-
"lint": "eslint --cache --fix src test",
32+
"lint": "eslint --cache --fix src",
2233
"prebuild": "npm run clean",
2334
"prepublish": "npm run build",
2435
"security": "nsp check",

src/index.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,29 @@ class ExtractCssChunks {
146146
}
147147

148148
apply(compiler) {
149-
if (process.env.NODE_ENV === 'development') {
150-
try {
151-
const isHOT = isHMR(compiler);
152-
153-
if (isHOT && compiler.options.module && compiler.options.module.rules) {
154-
const updatedRules = compiler.options.module.rules.reduce((rules, rule) => {
155-
if (rule.use && Array.isArray(rule.use)) {
156-
const isMiniCss = rule.use.some((l) => {
157-
const needle = l.loader || l;
158-
return needle.includes(pluginName);
159-
});
160-
if (isMiniCss) {
161-
rule.use.unshift(hotLoader);
162-
}
149+
try {
150+
const isHOT = this.options.hot ? true : isHMR(compiler);
151+
152+
if (isHOT && compiler.options.module && compiler.options.module.rules) {
153+
const updatedRules = compiler.options.module.rules.reduce((rules, rule) => {
154+
if (rule.use && Array.isArray(rule.use)) {
155+
const isMiniCss = rule.use.some((l) => {
156+
const needle = l.loader || l;
157+
return needle.includes(pluginName);
158+
});
159+
if (isMiniCss) {
160+
rule.use.unshift(hotLoader);
163161
}
164-
rules.push(rule);
162+
}
163+
rules.push(rule);
165164

166-
return rules;
167-
}, []);
165+
return rules;
166+
}, []);
168167

169-
compiler.options.module.rules = updatedRules;
170-
}
171-
} catch (e) {
172-
console.error('Something went wrong: contact the author', JSON.stringify(e)); // eslint-disable-line no-console
168+
compiler.options.module.rules = updatedRules;
173169
}
170+
} catch (e) {
171+
console.error('Something went wrong: contact the author', JSON.stringify(e)); // eslint-disable-line no-console
174172
}
175173

176174
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {

test/manual/webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const config = {
2525
new Self({
2626
filename: '[name].css',
2727
chunkFilename: '[contenthash].css',
28+
hot: false
2829
}),
2930

3031
new webpack.NamedModulesPlugin(),

0 commit comments

Comments
 (0)