|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var ConstDependency = require("./dependencies/ConstDependency"); |
| 5 | +const ConstDependency = require("./dependencies/ConstDependency"); |
| 6 | +const NullFactory = require("./NullFactory"); |
6 | 7 |
|
7 | | -var NullFactory = require("./NullFactory"); |
| 8 | +class UseStrictPlugin { |
| 9 | + apply(compiler) { |
| 10 | + compiler.plugin("compilation", (compilation, params) => { |
| 11 | + params.normalModuleFactory.plugin("parser", (parser) => { |
| 12 | + let parserInstance = parser; |
| 13 | + parser.plugin("program", (ast) => { |
| 14 | + let firstNode = ast.body[0]; |
| 15 | + let dep; |
| 16 | + if(firstNode && |
| 17 | + firstNode.type === "ExpressionStatement" && |
| 18 | + firstNode.expression.type === "Literal" && |
| 19 | + firstNode.expression.value === "use strict") { |
| 20 | + // Remove "use strict" expression. It will be added later by the renderer again. |
| 21 | + // This is necessary in order to not break the strict mode when webpack prepends code. |
| 22 | + // @see https://github.com/webpack/webpack/issues/1970 |
| 23 | + dep = new ConstDependency("", firstNode.range); |
| 24 | + dep.loc = firstNode.loc; |
| 25 | + parserInstance.state.current.addDependency(dep); |
| 26 | + parserInstance.state.module.strict = true; |
| 27 | + } |
| 28 | + }); |
| 29 | + }) |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
8 | 33 |
|
9 | | -function UseStrictPlugin() {} |
10 | 34 | 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 | | - }); |
31 | | - }) |
32 | | - }) |
33 | | -}; |
|
0 commit comments