Skip to content

Commit

Permalink
feat: automating module detection (#163)
Browse files Browse the repository at this point in the history
* feat: automating module detection

* test: updating tests

* docs: updating readme
  • Loading branch information
ScriptedAlchemy authored Apr 4, 2019
1 parent e79875e commit 00e329d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ module.exports = {
options: {
hot: true, // if you want HMR - we try to automatically inject hot reloading but if it's not working, add it to the config
reloadAll: true, // when desperation kicks in - this is a brute force HMR flag

}
},
"css-loader"
Expand Down Expand Up @@ -298,29 +297,6 @@ new ExtractCssChunk({

>Keep in mind, by default `[name].css` is used when `process.env.NODE_ENV === 'development'` and `[name].[contenthash].css` during production, so you can likely forget about having to pass anything.
### HMR Troubleshooting
**Blank array in HMR script?**

If you have issues with HMR, but enabled the loader, plugin, and already tried `hot: true, reloadAll:true`, then your webpack config might be more abstract than my simple lookup functions. In this case, Ive exported out the actual hot loader, you can add this manually and as long as you've got the plugin and loader -- it'll work

```js
rules: [
{
test: /\.css$/,
use: [
ExtractCssChunks.hotLoader, // for those who want to manually force hotLoading
ExtractCssChunks.loader,
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[name]__[local]--[hash:base64:5]',
},
},
],
},
]
```

### HMR Pitfall

Expand Down
9 changes: 8 additions & 1 deletion src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ module.exports = function(moduleId, options) {
function update() {
var src = getScriptSrc(options.filename);
var reloaded = reloadStyle(src);

if (options.locals) {
console.log('[HMR] Detected local css modules. Reload all css');
reloadAll();
return;
}

if (reloaded && !options.reloadAll) {
console.log('[HMR] css reload %s', src.join(' '));
} else {
Expand All @@ -162,5 +169,5 @@ module.exports = function(moduleId, options) {
}
}

return debounce(update, 10);
return debounce(update, 50);
};
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ class ExtractCssChunksPlugin {
if (this.options.orderWarning) {
compilation.warnings.push(
new Error(
`chunk ${chunk.name ||
chunk.id} [extract-css-chunks-webpack-plugin]\n` +
`chunk ${chunk.name || chunk.id} [${pluginName}]\n` +
'Conflicting order between:\n' +
` * ${fallbackModule.readableIdentifier(
requestShortener
Expand Down
5 changes: 4 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function hotLoader(content, context) {
var cssReload = require(${loaderUtils.stringifyRequest(
context.context,
path.join(__dirname, 'hmr/hotModuleReplacement.js')
)})(module.id, ${JSON.stringify(context.query)});
)})(module.id, ${JSON.stringify({
...context.query,
locals: !!context.locals,
})});
module.hot.dispose(cssReload);
${accept}
}
Expand Down
9 changes: 8 additions & 1 deletion test/__snapshots__/TestCases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ module.exports = function(moduleId, options) {
function update() {
var src = getScriptSrc(options.filename);
var reloaded = reloadStyle(src);
if (options.locals) {
console.log('[HMR] Detected local css modules. Reload all css');
reloadAll();
return;
}
if (reloaded && !options.reloadAll) {
console.log('[HMR] css reload %s', src.join(' '));
} else {
Expand All @@ -165,7 +172,7 @@ module.exports = function(moduleId, options) {
}
}
return debounce(update, 10);
return debounce(update, 50);
};
"
`;

0 comments on commit 00e329d

Please sign in to comment.