Skip to content

Commit 6758a30

Browse files
authored
Merge pull request webpack#3442 from webpack/fix/double-use-strict
Fix double "use strict"
2 parents 86aa0f4 + 3349e46 commit 6758a30

10 files changed

Lines changed: 69 additions & 8 deletions

File tree

lib/UseStrictPlugin.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ConstDependency = require("./dependencies/ConstDependency");
6-
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
76

87
var NullFactory = require("./NullFactory");
98

@@ -14,12 +13,20 @@ UseStrictPlugin.prototype.apply = function(compiler) {
1413
compiler.plugin("compilation", function(compilation, params) {
1514
params.normalModuleFactory.plugin("parser", function(parser) {
1615
parser.plugin("program", function(ast) {
17-
var body = ast.body[0]
18-
if(body &&
19-
body.type === "ExpressionStatement" &&
20-
body.expression.type === "Literal" &&
21-
body.expression.value === "use strict")
16+
var firstNode = ast.body[0];
17+
var dep;
18+
if(firstNode &&
19+
firstNode.type === "ExpressionStatement" &&
20+
firstNode.expression.type === "Literal" &&
21+
firstNode.expression.value === "use strict") {
22+
// Remove "use strict" expression. It will be added later by the renderer again.
23+
// This is necessary in order to not break the strict mode when webpack prepends code.
24+
// @see https://github.com/webpack/webpack/issues/1970
25+
dep = new ConstDependency("", firstNode.range);
26+
dep.loc = firstNode.loc;
27+
this.state.current.addDependency(dep);
2228
this.state.module.strict = true;
29+
}
2330
});
2431
})
2532
})

lib/dependencies/HarmonyImportDependencyParserPlugin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@
33
Author Tobias Koppers @sokra
44
*/
55
var AbstractPlugin = require("../AbstractPlugin");
6+
var HarmonyCompatiblilityDependency = require("./HarmonyCompatiblilityDependency");
67
var HarmonyImportDependency = require("./HarmonyImportDependency");
78
var HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
89
var HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
910
var HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
1011
var HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
1112

13+
function makeHarmonyModule(module, loc) {
14+
if(!module.meta.harmonyModule) {
15+
var dep = new HarmonyCompatiblilityDependency(module);
16+
dep.loc = loc;
17+
module.addDependency(dep);
18+
module.meta.harmonyModule = true;
19+
module.strict = true;
20+
}
21+
}
22+
1223
module.exports = AbstractPlugin.create({
1324
"import": function(statement, source) {
25+
makeHarmonyModule(this.state.module, statement.loc);
1426
var dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(this.state, source), statement.range);
1527
dep.loc = statement.loc;
1628
this.state.current.addDependency(dep);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
export default "a";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
import * as a from "./harmony-without-strict2";
3+
export default a;
4+
import "./harmony-with-strict3"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
export default "a";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "a";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "a";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
it("should include only one use strict per module", function() {
3+
require("./harmony-with-strict");
4+
require("./harmony-without-strict");
5+
require("./harmony-with-strict2");
6+
7+
var fs = require("fs");
8+
var source = fs.readFileSync(__filename, "utf-8");
9+
10+
var regExp = /\"use strict\";?\s*(.*)/g
11+
var match = regExp.exec(source);
12+
var matches = [];
13+
while(match) {
14+
matches.push(match[1]);
15+
match = regExp.exec(source);
16+
}
17+
18+
matches.should.be.eql([
19+
"Object.defineProperty(exports, \"__esModule\", { value: true });",
20+
"Object.defineProperty(exports, \"__esModule\", { value: true });",
21+
"Object.defineProperty(exports, \"__esModule\", { value: true });",
22+
"/* unused harmony default export */ var _unused_webpack_default_export = \"a\";",
23+
"Object.defineProperty(exports, \"__esModule\", { value: true });",
24+
"it(\"should include only one use strict per module\", function() {"
25+
]);
26+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
node: {
3+
__dirname: false,
4+
__filename: false
5+
}
6+
};

test/statsCases/tree-shaking/expected.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Hash: 84ac10223b6099395c33
1+
Hash: 37c14a3d270eb3a66a74
22
Time: Xms
33
Asset Size Chunks Chunk Names
4-
bundle.js 7.08 kB 0 [emitted] main
4+
bundle.js 7.14 kB 0 [emitted] main
55
chunk {0} bundle.js (main) 588 bytes [entry] [rendered]
66
[0] (webpack)/test/statsCases/tree-shaking/a.js 13 bytes {0} [built]
77
[exports: a]

0 commit comments

Comments
 (0)