Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hmr): adding hot module reloading #334

Merged
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
0830301
feat(hmr): adding hot module reloading
evilebottnawi Dec 13, 2018
875f166
Merge remote-tracking branch 'origin/hot-module-reloading' into hot-m…
ScriptedAlchemy Jan 6, 2019
7f99f5f
fix: remove unused dependency
ScriptedAlchemy Jan 6, 2019
4e72531
refactor: refactors based on PR comments
ScriptedAlchemy Jan 13, 2019
50b3fd8
refactor: move hot loader into base loader
ScriptedAlchemy Jan 13, 2019
c2207af
refactor: get hmr working with new context
ScriptedAlchemy Jan 24, 2019
1700774
test: added hmr test case
ScriptedAlchemy Jan 27, 2019
3f975cd
revert: roll back package upgrades
ScriptedAlchemy Jan 27, 2019
4c0c742
fix: updating dependencies
ScriptedAlchemy Jan 27, 2019
edf19e1
fix: remove console log
ScriptedAlchemy Jan 27, 2019
bac3930
test: fixing tests
ScriptedAlchemy Jan 27, 2019
86134d0
fix: use older version of normalize
ScriptedAlchemy Jan 27, 2019
8e6f14c
fix: remove vulnerabilities in depencencies
ScriptedAlchemy Jan 27, 2019
3979add
test: adding snapshot for hmr
ScriptedAlchemy Jan 28, 2019
3157c84
fix: moved es checker to dev dependencies
ScriptedAlchemy Jan 30, 2019
01126f8
docs: updated readme to detail hmr
ScriptedAlchemy Mar 3, 2019
2cf31af
fix: hmr on global styles
ScriptedAlchemy Mar 13, 2019
e5a5afb
fix: lock webpack version to avoid npm acorn issue
ScriptedAlchemy Mar 15, 2019
9ed9a85
fix: patching issue with global css missing source
ScriptedAlchemy Mar 26, 2019
2f94eed
fix: renaming hot to hmr
ScriptedAlchemy Mar 27, 2019
969514f
fix: remove unused method and using locals for css modules
ScriptedAlchemy Mar 27, 2019
b06d817
test: updating tests and snapshots
ScriptedAlchemy Mar 27, 2019
0aa328c
fix: using older version of normalize for older node support
ScriptedAlchemy Mar 27, 2019
be929ae
fix: removing reloadAll option
ScriptedAlchemy Mar 27, 2019
06e7789
fix: auto detect locals and reloadAll
ScriptedAlchemy Apr 4, 2019
a6bdafd
feat: adding order warning option
ScriptedAlchemy Apr 4, 2019
150f858
fix: added default option for orderWarning
ScriptedAlchemy Apr 4, 2019
a91857c
test: updating test expectations
ScriptedAlchemy Apr 4, 2019
55b6158
docs: updating readme about order warning flags
ScriptedAlchemy Apr 4, 2019
d9ebdc5
docs: updating docs about filtering warnings warningsFilter
ScriptedAlchemy Apr 4, 2019
6e6642a
feat(hmr): adding hot module reloading
evilebottnawi Dec 13, 2018
5a9fc85
fix: remove unused dependency
ScriptedAlchemy Jan 6, 2019
50144a7
refactor: refactors based on PR comments
ScriptedAlchemy Jan 13, 2019
39dd711
refactor: move hot loader into base loader
ScriptedAlchemy Jan 13, 2019
6126e7e
refactor: get hmr working with new context
ScriptedAlchemy Jan 24, 2019
cd6df8e
test: added hmr test case
ScriptedAlchemy Jan 27, 2019
bf0f745
revert: roll back package upgrades
ScriptedAlchemy Jan 27, 2019
d1cbf81
fix: updating dependencies
ScriptedAlchemy Jan 27, 2019
31bfa71
fix: remove console log
ScriptedAlchemy Jan 27, 2019
a51d862
test: fixing tests
ScriptedAlchemy Jan 27, 2019
2bd6661
fix: use older version of normalize
ScriptedAlchemy Jan 27, 2019
9803db3
fix: remove vulnerabilities in depencencies
ScriptedAlchemy Jan 27, 2019
c190436
test: adding snapshot for hmr
ScriptedAlchemy Jan 28, 2019
0c93cf0
fix: moved es checker to dev dependencies
ScriptedAlchemy Jan 30, 2019
c111e9b
docs: updated readme to detail hmr
ScriptedAlchemy Mar 3, 2019
936b8d3
fix: lock webpack version to avoid npm acorn issue
ScriptedAlchemy Mar 15, 2019
b08a3ce
fix: renaming hot to hmr
ScriptedAlchemy Mar 27, 2019
9f16d99
fix: remove unused method and using locals for css modules
ScriptedAlchemy Mar 27, 2019
79001b8
test: updating tests and snapshots
ScriptedAlchemy Mar 27, 2019
59797cc
fix: using older version of normalize for older node support
ScriptedAlchemy Mar 27, 2019
c149cc2
fix: removing reloadAll option
ScriptedAlchemy Mar 27, 2019
33109c4
fix: auto detect locals and reloadAll
ScriptedAlchemy Apr 4, 2019
6981838
feat: adding order warning option
ScriptedAlchemy Apr 4, 2019
2708a67
fix: added default option for orderWarning
ScriptedAlchemy Apr 4, 2019
4d1c75d
test: updating test expectations
ScriptedAlchemy Apr 4, 2019
15dd658
docs: updating readme about order warning flags
ScriptedAlchemy Apr 4, 2019
7f4159e
docs: updating docs about filtering warnings warningsFilter
ScriptedAlchemy Apr 4, 2019
641bdce
Merge branch 'master' into hot-module-reloading
ScriptedAlchemy Apr 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Thumbs.db
.vscode
*.sublime-project
*.sublime-workspace
.idea
64 changes: 58 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ Compared to the extract-text-webpack-plugin:
* Easier to use
* Specific to CSS

TODO:

* HMR support

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

Expand Down Expand Up @@ -62,8 +59,9 @@ module.exports = {
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
// by default it uses publicPath in webpackOptions.output
publicPath: '../',
hmr: process.env.NODE_ENV === 'development'
}
},
"css-loader"
Expand Down Expand Up @@ -103,7 +101,12 @@ module.exports = {
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV === 'development'
}
},
'css-loader',
'postcss-loader',
'sass-loader',
Expand All @@ -114,6 +117,51 @@ module.exports = {
}
```

#### Hot Module Reloading (HMR)

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.


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**

```js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
plugins: [
new MiniCssExtractPlugin({
// 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: MiniCssExtractPlugin.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"
]
}
]
}
}
```


### Minimizing For Production

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:
Expand Down Expand Up @@ -272,6 +320,10 @@ module.exports = {

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

### Remove Order Warnings

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

### Media Query Plugin

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:
Expand Down
Loading