Skip to content

Commit 351c993

Browse files
committed
fixup reasons when redirecting dependencies for side-effects
show reason explanation in stats fixes webpack#7428
1 parent 67fa81f commit 351c993

File tree

15 files changed

+117
-2
lines changed

15 files changed

+117
-2
lines changed

lib/Stats.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ class Stats {
513513
? reason.module.readableIdentifier(requestShortener)
514514
: null,
515515
type: reason.dependency ? reason.dependency.type : null,
516+
explanation: reason.explanation,
516517
userRequest: reason.dependency
517518
? reason.dependency.userRequest
518519
: null
@@ -1058,6 +1059,10 @@ class Stats {
10581059
colors.normal(" ");
10591060
colors.normal(reason.loc);
10601061
}
1062+
if (reason.explanation) {
1063+
colors.normal(" ");
1064+
colors.cyan(reason.explanation);
1065+
}
10611066
newline();
10621067
}
10631068
}

lib/optimize/ModuleConcatenationPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class ModuleConcatenationPlugin {
313313
module.dependencies
314314

315315
// Only harmony Dependencies
316-
.filter(dep => dep instanceof HarmonyImportDependency && dep.module)
316+
.filter(dep => dep instanceof HarmonyImportDependency)
317317

318318
// Get reference info for this dependency
319319
.map(dep => dep.getReference())

lib/optimize/SideEffectsFlagPlugin.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class SideEffectsFlagPlugin {
105105
for (const pair of reexportMaps) {
106106
const module = pair[0];
107107
const map = pair[1];
108-
for (const reason of module.reasons) {
108+
for (let i = 0; i < module.reasons.length; i++) {
109+
const reason = module.reasons[i];
109110
const dep = reason.dependency;
110111
if (
111112
dep instanceof HarmonyImportSpecifierDependency &&
@@ -115,6 +116,15 @@ class SideEffectsFlagPlugin {
115116
if (mapping) {
116117
dep.redirectedModule = mapping.module;
117118
dep.redirectedId = mapping.exportName;
119+
module.reasons.splice(i--, 1);
120+
mapping.module.addReason(
121+
reason.module,
122+
dep,
123+
reason.explanation
124+
? reason.explanation +
125+
" (skipped side-effect-free modules)"
126+
: "(skipped side-effect-free modules)"
127+
);
118128
}
119129
}
120130
}

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,55 @@ Child
20622062
ModuleConcatenation bailout: Module is referenced from these modules with unsupported syntax: ./first.js (referenced with import())"
20632063
`;
20642064

2065+
exports[`StatsTestCases should print correct stats for side-effects-issue-7428 1`] = `
2066+
"Hash: 9d82ff1a0201e9c2ffa4
2067+
Time: Xms
2068+
Built at: Thu Jan 01 1970 00:00:00 GMT
2069+
Asset Size Chunks Chunk Names
2070+
0.js 481 bytes 0 [emitted]
2071+
main.js 8.31 KiB 1 [emitted] main
2072+
Entrypoint main = main.js
2073+
[0] ./components/src/CompAB/utils.js 97 bytes {1} [built]
2074+
harmony side effect evaluation ./utils [1] ./main.js + 1 modules 1:0-30
2075+
harmony import specifier ./utils [1] ./main.js + 1 modules 5:2-5
2076+
harmony side effect evaluation ./utils [4] ./components/src/CompAB/CompA.js 1:0-35
2077+
harmony import specifier ./utils [4] ./components/src/CompAB/CompA.js 5:5-12
2078+
[1] ./main.js + 1 modules 231 bytes {1} [built]
2079+
single entry ./main.js main
2080+
| ./main.js 144 bytes [built]
2081+
| single entry ./main.js main
2082+
| ./components/src/CompAB/CompB.js 77 bytes [built]
2083+
| [only some exports used: default]
2084+
| harmony import specifier ./components ./main.js 4:15-20 (skipped side-effect-free modules)
2085+
| harmony side effect evaluation ./CompB [7] ./components/src/CompAB/index.js 2:0-43
2086+
| harmony export imported specifier ./CompB [7] ./components/src/CompAB/index.js 2:0-43
2087+
[2] ./components/src/index.js 84 bytes [built]
2088+
[no exports used]
2089+
harmony side effect evaluation ./components [1] ./main.js + 1 modules 1:0-44
2090+
harmony side effect evaluation ./components [3] ./foo.js 1:0-37
2091+
[3] ./foo.js 101 bytes {0} [built]
2092+
import() ./foo ./main.js 6:0-15
2093+
[4] ./components/src/CompAB/CompA.js 89 bytes {1} [built]
2094+
[only some exports used: default]
2095+
harmony import specifier ./components ./main.js 3:15-20 (skipped side-effect-free modules)
2096+
harmony import specifier ./components [3] ./foo.js 3:20-25 (skipped side-effect-free modules)
2097+
harmony side effect evaluation ./CompA [7] ./components/src/CompAB/index.js 1:0-43
2098+
harmony export imported specifier ./CompA [7] ./components/src/CompAB/index.js 1:0-43
2099+
[5] ./components/src/CompC/CompC.js 33 bytes [built]
2100+
[no exports used]
2101+
harmony side effect evaluation ./CompC [6] ./components/src/CompC/index.js 1:0-34
2102+
harmony export imported specifier ./CompC [6] ./components/src/CompC/index.js 1:0-34
2103+
[6] ./components/src/CompC/index.js 34 bytes [built]
2104+
[no exports used]
2105+
harmony side effect evaluation ./CompC [2] ./components/src/index.js 2:0-43
2106+
harmony export imported specifier ./CompC [2] ./components/src/index.js 2:0-43
2107+
[7] ./components/src/CompAB/index.js 87 bytes [built]
2108+
[no exports used]
2109+
harmony side effect evaluation ./CompAB [2] ./components/src/index.js 1:0-40
2110+
harmony export imported specifier ./CompAB [2] ./components/src/index.js 1:0-40
2111+
harmony export imported specifier ./CompAB [2] ./components/src/index.js 1:0-40"
2112+
`;
2113+
20652114
exports[`StatsTestCases should print correct stats for side-effects-simple-unused 1`] = `
20662115
"Hash: 0cb74fac5f3c1b7f2150
20672116
Time: Xms
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "components",
3+
"version": "1.0.0",
4+
"main": "src/index.js",
5+
"sideEffects": false
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as methods from './utils';
2+
3+
export default {
4+
name: 'CompA',
5+
...methods,
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fnB } from './utils';
2+
3+
export default {
4+
name: 'CompB',
5+
fnB,
6+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as CompA } from './CompA';
2+
export { default as CompB } from './CompB';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const fnA = () => { console.log('fnA') };
2+
export const fnB = () => { console.log('fnB') };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default { name: 'CompC' };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './CompC';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { CompA, CompB } from './CompAB';
2+
export { default as CompC } from './CompC';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { CompA } from './components';
2+
3+
export default { ...CompA, fnB: () => { console.log('hi') } }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CompA, CompB } from './components';
2+
3+
window.CompA = CompA;
4+
window.CompB = CompB;
5+
6+
import('./foo').then((m) => {
7+
m.default.fnB();
8+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
mode: "none",
3+
entry: "./main.js",
4+
optimization: {
5+
usedExports: true,
6+
sideEffects: true,
7+
concatenateModules: true
8+
},
9+
stats: {
10+
nestedModules: true,
11+
usedExports: true,
12+
reasons: true
13+
}
14+
};

0 commit comments

Comments
 (0)