Skip to content

Commit ae0f5b1

Browse files
fix: CSS HMR Issues (faceyspacey#114)
* fix: Fixed HMR issue when oneOf is used When using oneOf, HRM is never activated. This patch improved the lookup system used for determining if CSS can and should be reloaded. There have been some other tweaks for css modules and HMR fix faceyspacey#98,faceyspacey#80 * fix: Applying rollback fixes to updateWebpackConfig, search loader and use object keys * fix: Adding back exported out hot loader * docs(readme): Adding example of manual hotloader implementation In the event users cannot get it to work, theres an example in the docs to reference * fix: handle functions as loaders Do not break when searching through `use` or `loader` that is a function * fix(index.js): Support function loaders (faceyspacey#116) * docs(readme): Tone of Voice clairification on what hot:true does (faceyspacey#113) * fix(index.js): Support function loaders updateWebpackConfig previously expected `use` entries to be strings or arrays, and would throw `needle.includes is not a function` errors when given a function that returns a loader object. * Update README.md * test: Fixing function-loader tests * Add reloadAll and cssModules to typings (faceyspacey#115) * docs(readme): Tone of Voice clairification on what hot:true does (faceyspacey#113) * Add reloadAll and cssModules to typings
1 parent c644b00 commit ae0f5b1

File tree

10 files changed

+77
-3
lines changed

10 files changed

+77
-3
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
'prefer-destructuring': 0,
2222
'array-callback-return': 0,
2323
'prefer-template': 0,
24-
'class-methods-use-this': 0
24+
'class-methods-use-this': 0,
25+
'no-plusplus': 0
2526
}
2627
};

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,30 @@ new ExtractCssChunk({
281281

282282
>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.
283283
284+
### HMR Troubleshooting
285+
**Blank array in HMR script?**
286+
287+
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
288+
289+
```js
290+
rules: [
291+
{
292+
test: /\.css$/,
293+
use: [
294+
ExtractCssChunks.hotLoader, // for those who want to manually force hotLoading
295+
ExtractCssChunks.loader,
296+
{
297+
loader: 'css-loader',
298+
options: {
299+
modules: true,
300+
localIdentName: '[name]__[local]--[hash:base64:5]',
301+
},
302+
},
303+
],
304+
},
305+
]
306+
```
307+
284308
### HMR Pitfall
285309

286310
The most common workflow when working with webpack is to write a "development" / "production" value in to the

index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ declare module 'extract-css-chunks-webpack-plugin' {
1818
* we try to automatically inject hot reloading, but if it's not working, use this config
1919
*/
2020
hot?: boolean;
21+
reloadAll?: boolean;
22+
cssModules?: boolean;
2123
}
2224
}
2325

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "extract-css-chunks-webpack-plugin",
3-
"version": "3.2.0",
3+
"version": "3.2.1-alpha.1",
44
"author": "James Gillmore <james@faceyspacey.com>",
55
"contributors": [
66
"Zack Jackson <zack@ScriptedAlchemy.com> (https://github.com/ScriptedAlchemy)"

src/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,29 @@ class ExtractCssChunks {
427427
if (node && node.use && Array.isArray(node.use)) {
428428
const isMiniCss = node.use.some((l) => {
429429
const needle = l.loader || l;
430+
if (typeof l === 'function') {
431+
return false;
432+
}
430433
return needle.includes(pluginName);
431434
});
432435
if (isMiniCss) {
433436
node.use.unshift(this.hotLoaderObject);
434437
}
435438
}
439+
if (node && node.loader && Array.isArray(node.loader)) {
440+
const isMiniCss = node.loader.some((l) => {
441+
const needle = l.loader || l;
442+
if (typeof l === 'function') {
443+
return false;
444+
}
445+
return needle.includes(pluginName);
446+
});
447+
if (isMiniCss) {
448+
node.loader.unshift(this.hotLoaderObject);
449+
}
450+
}
436451
});
452+
437453
rules.push(rule);
438454

439455
return rules;
@@ -592,5 +608,6 @@ class ExtractCssChunks {
592608
}
593609

594610
ExtractCssChunks.loader = require.resolve('./loader');
611+
ExtractCssChunks.hotLoader = require.resolve('./hotLoader');
595612

596613
export default ExtractCssChunks;

src/loader.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const pluginName = 'extract-css-chunks-webpack-plugin';
1212

1313
const exec = (loaderContext, code, filename) => {
1414
const module = new NativeModule(filename, loaderContext);
15-
module.paths = NativeModule._nodeModulePaths(loaderContext.context); // eslint-disable-line no-underscore-dangle
15+
// eslint-disable-line no-underscore-dangle
16+
module.paths = NativeModule._nodeModulePaths(loaderContext.context);
1617
module.filename = filename;
1718
module._compile(code, filename); // eslint-disable-line no-underscore-dangle
1819
return module.exports;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body { background: red; }
2+

test/cases/function-loader/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './main.css';

test/cases/function-loader/main.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: red; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
const Self = require('../../../');
4+
5+
module.exports = {
6+
entry: './index.js',
7+
module: {
8+
rules: [
9+
{
10+
test: /\.css$/,
11+
use: [
12+
() => [
13+
Self.loader,
14+
'css-loader',
15+
],
16+
],
17+
},
18+
],
19+
},
20+
plugins: [
21+
new Self({
22+
filename: '[name].css',
23+
}),
24+
],
25+
};

0 commit comments

Comments
 (0)