Skip to content

feat: automating module detection #163

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

Merged
merged 3 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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);
};
"
`;