Skip to content

Commit 4ed9c5a

Browse files
ScriptedAlchemyevilebottnawi
authored andcommitted
feat: adding hot module reloading (#334)
1 parent 7b1425a commit 4ed9c5a

20 files changed

+1009
-198
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Thumbs.db
1414
.vscode
1515
*.sublime-project
1616
*.sublime-workspace
17+
.idea

README.md

+58-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ Compared to the extract-text-webpack-plugin:
2424
* Easier to use
2525
* Specific to CSS
2626

27-
TODO:
28-
29-
* HMR support
3027

3128
<h2 align="center">Install</h2>
3229

@@ -69,8 +66,9 @@ module.exports = {
6966
loader: MiniCssExtractPlugin.loader,
7067
options: {
7168
// you can specify a publicPath here
72-
// by default it use publicPath in webpackOptions.output
73-
publicPath: '../'
69+
// by default it uses publicPath in webpackOptions.output
70+
publicPath: '../',
71+
hmr: process.env.NODE_ENV === 'development'
7472
}
7573
},
7674
"css-loader"
@@ -149,7 +147,12 @@ module.exports = {
149147
{
150148
test: /\.(sa|sc|c)ss$/,
151149
use: [
152-
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
150+
{
151+
loader: MiniCssExtractPlugin.loader,
152+
options: {
153+
hmr: process.env.NODE_ENV === 'development'
154+
}
155+
},
153156
'css-loader',
154157
'postcss-loader',
155158
'sass-loader',
@@ -160,6 +163,51 @@ module.exports = {
160163
}
161164
```
162165

166+
#### Hot Module Reloading (HMR)
167+
168+
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.
169+
170+
171+
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.
172+
173+
174+
175+
**webpack.config.js**
176+
177+
```js
178+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
179+
module.exports = {
180+
plugins: [
181+
new MiniCssExtractPlugin({
182+
// Options similar to the same options in webpackOptions.output
183+
// both options are optional
184+
filename: "[name].css",
185+
chunkFilename: "[id].css"
186+
})
187+
],
188+
module: {
189+
rules: [
190+
{
191+
test: /\.css$/,
192+
use: [
193+
{
194+
loader: MiniCssExtractPlugin.loader,
195+
options: {
196+
// only enable hot in development
197+
hmr: process.env.NODE_ENV === 'development',
198+
// if hmr does not work, this is a forceful method.
199+
reloadAll: true
200+
}
201+
},
202+
"css-loader"
203+
]
204+
}
205+
]
206+
}
207+
}
208+
```
209+
210+
163211
### Minimizing For Production
164212

165213
While webpack 5 is likely to come with a CSS minimizer built-in, with webpack 4 you need to bring your own. 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:
@@ -314,6 +362,10 @@ module.exports = {
314362

315363
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.
316364

365+
### Remove Order Warnings
366+
367+
If the terminal is getting bloated with chunk order warnings. You can filter by configuring [warningsFilter](https://webpack.js.org/configuration/stats/) withing the webpack stats option
368+
317369
### Media Query Plugin
318370

319371
If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:

0 commit comments

Comments
 (0)