Skip to content

Commit 5edc7ef

Browse files
authored
Merge pull request webpack#6143 from ooflorent/module_id_bailout
Bail out concatenation if module uses module.id or module.loaded
2 parents 97880f1 + fb2fe26 commit 5edc7ef

File tree

13 files changed

+67
-9
lines changed

13 files changed

+67
-9
lines changed

lib/NodeStuffPlugin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ class NodeStuffPlugin {
7070
"expression require.extensions",
7171
ParserHelpers.expressionIsUnsupported(parser, "require.extensions is not supported by webpack. Use a loader instead.")
7272
);
73-
parser.plugin("expression module.loaded", ParserHelpers.toConstantDependency(parser, "module.l"));
74-
parser.plugin("expression module.id", ParserHelpers.toConstantDependency(parser, "module.i"));
73+
parser.plugin("expression module.loaded", expr => {
74+
parser.state.module.buildMeta.moduleConcatenationBailout = "module.loaded";
75+
return ParserHelpers.toConstantDependency(parser, "module.l")(expr);
76+
});
77+
parser.plugin("expression module.id", expr => {
78+
parser.state.module.buildMeta.moduleConcatenationBailout = "module.id";
79+
return ParserHelpers.toConstantDependency(parser, "module.i")(expr);
80+
});
7581
parser.plugin("expression module.exports", () => {
7682
const module = parser.state.module;
7783
const isHarmony = module.buildMeta && module.buildMeta.harmonyModule;

lib/optimize/ModuleConcatenationPlugin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ModuleConcatenationPlugin {
2727
}) => {
2828
const handler = (parser, parserOptions) => {
2929
parser.plugin("call eval", () => {
30-
parser.state.module.buildMeta.hasEval = true;
30+
// Because of variable renaming we can't use modules with eval.
31+
parser.state.module.buildMeta.moduleConcatenationBailout = "eval()";
3132
});
3233
};
3334

@@ -58,9 +59,11 @@ class ModuleConcatenationPlugin {
5859
continue;
5960
}
6061

61-
// Because of variable renaming we can't use modules with eval
62-
if(module.buildMeta && module.buildMeta.hasEval) {
63-
setBailoutReason(module, "Module uses eval()");
62+
// Some expressions are not compatible with module concatenation
63+
// because they may produce unexpected results. The plugin bails out
64+
// if some were detected upfront.
65+
if(module.buildMeta && module.buildMeta.moduleConcatenationBailout) {
66+
setBailoutReason(module, `Module uses ${module.buildMeta.moduleConcatenationBailout}`);
6467
continue;
6568
}
6669

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("./ref-from-cjs");
2+
3+
module.exports = "cjs module";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "another entry";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default eval("using eval");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Hash: 841dd2f7e2da346f7bf1
2+
Time: Xms
3+
[0] ./entry.js 32 bytes {0} {1} [built]
4+
ModuleConcatenation bailout: Module is an entry point
5+
[1] ./ref-from-cjs.js 45 bytes {0} [built]
6+
ModuleConcatenation bailout: Module is referenced from these modules with unsupported syntax: ./cjs.js (referenced with cjs require)
7+
[2] ./index.js 150 bytes {0} [built]
8+
ModuleConcatenation bailout: Module is an entry point
9+
[3] ./cjs.js 59 bytes {0} [built]
10+
ModuleConcatenation bailout: Module is not an ECMAScript module
11+
[4] ./eval.js 35 bytes {0} [built]
12+
ModuleConcatenation bailout: Module uses eval()
13+
[5] ./injected-vars.js 40 bytes {0} [built]
14+
ModuleConcatenation bailout: Module uses injected variables (__dirname, __filename)
15+
[6] ./module-id.js 26 bytes {0} [built]
16+
ModuleConcatenation bailout: Module uses module.id
17+
[7] ./module-loaded.js 30 bytes {0} [built]
18+
ModuleConcatenation bailout: Module uses module.loaded
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./cjs";
2+
import "./entry";
3+
import "./eval";
4+
import "./injected-vars";
5+
import "./module-id";
6+
import "./module-loaded";
7+
import "./ref-from-cjs";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [__dirname, __filename];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default module.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default module.loaded;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "referenced by a CJS module";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
mode: "production",
3+
entry: {
4+
index: "./index.js",
5+
entry: "./entry.js"
6+
},
7+
target: "web",
8+
output: {
9+
filename: "[name].js"
10+
},
11+
stats: {
12+
assets: false,
13+
optimizationBailout: true
14+
}
15+
};

test/statsCases/separate-css-bundle/expected.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Hash: aa1b352e571b0ba5e7f5d37c33df2ddf3d79c4f9
1+
Hash: 6324063243d1151e39ba483784dda778b9890e57
22
Child
3-
Hash: aa1b352e571b0ba5e7f5
3+
Hash: 6324063243d1151e39ba
44
Time: Xms
55
Asset Size Chunks Chunk Names
66
d6c1b876fc64139d8324.js 2.74 KiB 0 [emitted] main
@@ -15,7 +15,7 @@ Child
1515
[0] (webpack)/node_modules/css-loader!./a/file.css 199 bytes {0} [built]
1616
[1] (webpack)/node_modules/css-loader/lib/css-base.js 2.21 KiB {0} [built]
1717
Child
18-
Hash: d37c33df2ddf3d79c4f9
18+
Hash: 483784dda778b9890e57
1919
Time: Xms
2020
Asset Size Chunks Chunk Names
2121
d6c1b876fc64139d8324.js 2.74 KiB 0 [emitted] main

0 commit comments

Comments
 (0)