Skip to content

Commit 5326c24

Browse files
authored
Merge pull request webpack#3609 from webpack/feature/refactor_es6_use_strict
refactor(es6): Update UseStrictPlugin to es6 class [WIP]
2 parents dbf1592 + 852c5c1 commit 5326c24

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

‎lib/UseStrictPlugin.js‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,35 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var ConstDependency = require("./dependencies/ConstDependency");
5+
"use strict"
66

7-
var NullFactory = require("./NullFactory");
7+
const ConstDependency = require("./dependencies/ConstDependency");
8+
const NullFactory = require("./NullFactory");
89

9-
function UseStrictPlugin() {}
10-
module.exports = UseStrictPlugin;
11-
12-
UseStrictPlugin.prototype.apply = function(compiler) {
13-
compiler.plugin("compilation", function(compilation, params) {
14-
params.normalModuleFactory.plugin("parser", function(parser) {
15-
parser.plugin("program", function(ast) {
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);
28-
this.state.module.strict = true;
29-
}
30-
});
10+
class UseStrictPlugin {
11+
apply(compiler) {
12+
compiler.plugin("compilation", (compilation, params) => {
13+
params.normalModuleFactory.plugin("parser", (parser) => {
14+
let parserInstance = parser;
15+
parser.plugin("program", (ast) => {
16+
let firstNode = ast.body[0];
17+
let 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+
parserInstance.state.current.addDependency(dep);
28+
parserInstance.state.module.strict = true;
29+
}
30+
});
31+
})
3132
})
32-
})
33-
};
33+
}
34+
}
35+
36+
module.exports = UseStrictPlugin;

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
"extract-text-webpack-plugin": "^2.0.0-beta",
4242
"file-loader": "~0.9.0",
4343
"i18n-webpack-plugin": "^0.3.0",
44-
"istanbul": "^0.4.1",
44+
"istanbul": "^0.4.5",
4545
"jade": "^1.11.0",
4646
"jade-loader": "~0.8.0",
4747
"js-beautify": "^1.5.10",
4848
"less": "^2.5.1",
4949
"less-loader": "^2.0.0",
50-
"mocha": "^3.1.0",
50+
"mocha": "^3.2.0",
5151
"mocha-lcov-reporter": "^1.0.0",
5252
"nsp": "^2.6.1",
5353
"raw-loader": "~0.5.0",
@@ -92,7 +92,7 @@
9292
"lint": "eslint lib bin hot",
9393
"beautify-lint": "beautify-lint 'lib/**/*.js' 'hot/**/*.js' 'bin/**/*.js' 'benchmark/*.js' 'test/*.js'",
9494
"nsp": "nsp check --output summary",
95-
"cover": "istanbul cover -x '**/*.runtime.js' node_modules/mocha/bin/_mocha",
95+
"cover": "node --harmony ./node_modules/.bin/istanbul cover -x '**/*.runtime.js' node_modules/mocha/bin/_mocha",
9696
"beautify": "beautify-rewrite 'lib/**/*.js' 'hot/**/*.js' 'bin/**/*.js' 'benchmark/*.js' 'test/*.js'",
9797
"publish-patch": "npm run lint && npm run beautify-lint && mocha && npm version patch && git push && git push --tags && npm publish"
9898
}

0 commit comments

Comments
 (0)