Skip to content

Commit 569449e

Browse files
fix(index, loader): Fixing HMR from assorted config build, exported loader
In rare cases, on large project such as React Static, webpack configs can end up using random series of keys and structures. In this case, there was a loader key inside a oneOf key, causing the recursive lookup to fail. Also exported the hot loader directly for hopeless cases fix faceyspacey#110
1 parent 8b7ddef commit 569449e

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

src/index.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class ExtractCssChunks {
168168
compiler.options.module.rules = this.updateWebpackConfig(compiler.options.module.rules);
169169
}
170170
} catch (e) {
171-
throw new Error(`Something went wrong: contact the author: ${JSON.stringify(e)}`);
171+
throw new Error(e);
172172
}
173173

174174
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
@@ -406,34 +406,47 @@ class ExtractCssChunks {
406406
});
407407
}
408408

409-
traverseDepthFirst(root, visit) {
410-
let nodesToVisit = [root];
409+
searchObject(node) {
410+
if (!node) {
411+
return false;
412+
}
411413

412-
while (nodesToVisit.length > 0) {
413-
const currentNode = nodesToVisit.shift();
414+
if (typeof node === 'string' && node === pluginName) {
415+
return true;
416+
}
414417

415-
if (currentNode !== null && typeof currentNode === 'object') {
416-
const children = Object.values(currentNode);
417-
nodesToVisit = [...children, ...nodesToVisit];
418+
return node.some((l) => {
419+
const needle = l.loader || l;
420+
if (needle === pluginName) {
421+
return true;
418422
}
419-
420-
visit(currentNode);
421-
}
423+
return needle.includes(pluginName);
424+
});
422425
}
423426

424427
updateWebpackConfig(rulez) {
428+
let isExtract = null;
425429
return rulez.reduce((rules, rule) => {
426-
this.traverseDepthFirst(rule, (node) => {
427-
if (node && node.use && Array.isArray(node.use)) {
428-
const isMiniCss = node.use.some((l) => {
429-
const needle = l.loader || l;
430-
return needle.includes(pluginName);
431-
});
432-
if (isMiniCss) {
433-
node.use.unshift(this.hotLoaderObject);
434-
}
430+
if (rule.oneOf) {
431+
rule.oneOf = this.updateWebpackConfig(rule.oneOf);
432+
}
433+
434+
// redundant but works
435+
if (rule.loader && Array.isArray(rule.loader)) {
436+
isExtract = this.searchObject(rule.loader);
437+
438+
if (isExtract) {
439+
rule.loader.unshift(hotLoader);
435440
}
436-
});
441+
}
442+
443+
if (rule.use && Array.isArray(rule.use)) {
444+
isExtract = this.searchObject(rule.use);
445+
446+
if (isExtract) {
447+
rule.use.unshift(hotLoader);
448+
}
449+
}
437450
rules.push(rule);
438451

439452
return rules;
@@ -592,5 +605,6 @@ class ExtractCssChunks {
592605
}
593606

594607
ExtractCssChunks.loader = require.resolve('./loader');
608+
ExtractCssChunks.hotLoader = require.resolve('./hotLoader');
595609

596610
export default ExtractCssChunks;

0 commit comments

Comments
 (0)