Skip to content

Commit 5ef1635

Browse files
docs(readme): Updating code examples for universal implementations
Adding code samples for dev and prod implementations of the new css-chunk plugin and how it works with Universal
1 parent 910b0e3 commit 5ef1635

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

README.md

+45-4
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">
@@ -99,6 +99,15 @@ module.exports = {
9999
}
100100
```
101101

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+
```
102111

103112

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

119128

120-
## Recommended Installation
129+
## Recommended Installation For Universal
121130
```
122131
yarn add react-universal-component webpack-flush-chunks
123132
yarn add --dev extract-css-chunks-webpack-plugin babel-plugin-universal-import
@@ -129,6 +138,8 @@ yarn add --dev extract-css-chunks-webpack-plugin babel-plugin-universal-import
129138
"plugins": ["universal-import"]
130139
}
131140
```
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
132143

133144
*webpack.config.js:*
134145
```js
@@ -152,8 +163,38 @@ module.exports = {
152163
},
153164
],
154165
},
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+
},
155196
plugins: [
156-
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
157198
]
158199
};
159200
```
@@ -257,7 +298,7 @@ For example, when running the build using some form of npm script:
257298
```json
258299
{
259300
"scripts": {
260-
"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"
261302
}
262303
}
263304
```

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
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",

0 commit comments

Comments
 (0)