Skip to content

Commit 552b185

Browse files
committed
feat: improve warning of conflict order
1 parent 50434b5 commit 552b185

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/index.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ class CssModule extends webpack.Module {
9090
}
9191

9292
class CssModuleFactory {
93-
create(
94-
{
95-
dependencies: [dependency],
96-
},
97-
callback
98-
) {
93+
create({ dependencies: [dependency] }, callback) {
9994
callback(null, new CssModule(dependency));
10095
}
10196
}
@@ -416,6 +411,9 @@ class MiniCssExtractPlugin {
416411
if (typeof chunkGroup.getModuleIndex2 === 'function') {
417412
// Store dependencies for modules
418413
const moduleDependencies = new Map(modules.map((m) => [m, new Set()]));
414+
const moduleDependenciesReasons = new Map(
415+
modules.map((m) => [m, new Map()])
416+
);
419417

420418
// Get ordered list of modules per chunk group
421419
// This loop also gathers dependencies from the ordered lists
@@ -435,9 +433,14 @@ class MiniCssExtractPlugin {
435433

436434
for (let i = 0; i < sortedModules.length; i++) {
437435
const set = moduleDependencies.get(sortedModules[i]);
436+
const reasons = moduleDependenciesReasons.get(sortedModules[i]);
438437

439438
for (let j = i + 1; j < sortedModules.length; j++) {
440-
set.add(sortedModules[j]);
439+
const module = sortedModules[j];
440+
set.add(module);
441+
const reason = reasons.get(module) || new Set();
442+
reason.add(cg);
443+
reasons.set(module, reason);
441444
}
442445
}
443446

@@ -453,6 +456,7 @@ class MiniCssExtractPlugin {
453456
let success = false;
454457
let bestMatch;
455458
let bestMatchDeps;
459+
let bestMatchDepsReasons;
456460

457461
// get first module where dependencies are fulfilled
458462
for (const list of modulesByChunkGroup) {
@@ -472,6 +476,7 @@ class MiniCssExtractPlugin {
472476
if (!bestMatchDeps || bestMatchDeps.length > failedDeps.length) {
473477
bestMatch = list;
474478
bestMatchDeps = failedDeps;
479+
bestMatchDepsReasons = moduleDependenciesReasons.get(module);
475480
}
476481

477482
if (failedDeps.length === 0) {
@@ -491,14 +496,21 @@ class MiniCssExtractPlugin {
491496
if (!this.options.ignoreOrder) {
492497
compilation.warnings.push(
493498
new Error(
494-
`chunk ${chunk.name || chunk.id} [${pluginName}]\n` +
495-
'Conflicting order between:\n' +
496-
` * ${fallbackModule.readableIdentifier(
497-
requestShortener
498-
)}\n` +
499-
`${bestMatchDeps
500-
.map((m) => ` * ${m.readableIdentifier(requestShortener)}`)
501-
.join('\n')}`
499+
[
500+
`chunk ${chunk.name || chunk.id} [${pluginName}]`,
501+
'Following module has been added:',
502+
` * ${fallbackModule.readableIdentifier(requestShortener)}`,
503+
"while this module as dependencies that haven't been added before:",
504+
...bestMatchDeps.map((m) =>
505+
[
506+
` * ${m.readableIdentifier(requestShortener)}`,
507+
`(used previous to added module in chunk ${Array.from(
508+
bestMatchDepsReasons.get(m),
509+
(cg) => cg.name
510+
).join(',')})`,
511+
].join(' ')
512+
),
513+
].join('\n')
502514
)
503515
);
504516
}

test/TestCases.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ function compareDirectory(actual, expected) {
2525
}
2626
}
2727

28+
function compareWarning(actual, expectedFile) {
29+
if (!fs.existsSync(expectedFile)) return;
30+
31+
const expected = require(expectedFile); // eslint-disable-line global-require,import/no-dynamic-require
32+
expect(actual).toBe(expected);
33+
}
34+
2835
describe('TestCases', () => {
2936
const casesDirectory = path.resolve(__dirname, 'cases');
3037
const outputDirectory = path.resolve(__dirname, 'js');
@@ -93,6 +100,13 @@ describe('TestCases', () => {
93100

94101
compareDirectory(outputDirectoryForCase, expectedDirectory);
95102

103+
const expectedWarning = path.resolve(directoryForCase, 'warnings.js');
104+
const actualWarning = stats.toString({
105+
all: false,
106+
warnings: true,
107+
});
108+
compareWarning(actualWarning, expectedWarning);
109+
96110
done();
97111
});
98112
}, 10000);
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const cssLoaderPath = require.resolve('css-loader');
2+
3+
module.exports = [
4+
'',
5+
'WARNING in chunk styles [mini-css-extract-plugin]',
6+
'Following module has been added:',
7+
` * css ${cssLoaderPath}!./e2.css`,
8+
"while this module as dependencies that haven't been added before:",
9+
` * css ${cssLoaderPath}!./e1.css (used previous to added module in chunk entry2)`,
10+
].join('\n');

0 commit comments

Comments
 (0)