1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const ConstDependency = require("./dependencies/ConstDependency");
|
9 |
|
10 |
|
11 |
|
12 | class UseStrictPlugin {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | apply(compiler) {
|
19 | compiler.hooks.compilation.tap(
|
20 | "UseStrictPlugin",
|
21 | (compilation, { normalModuleFactory }) => {
|
22 | const handler = parser => {
|
23 | parser.hooks.program.tap("UseStrictPlugin", ast => {
|
24 | const firstNode = ast.body[0];
|
25 | if (
|
26 | firstNode &&
|
27 | firstNode.type === "ExpressionStatement" &&
|
28 | firstNode.expression.type === "Literal" &&
|
29 | firstNode.expression.value === "use strict"
|
30 | ) {
|
31 |
|
32 |
|
33 |
|
34 | const dep = new ConstDependency("", firstNode.range);
|
35 | dep.loc = firstNode.loc;
|
36 | parser.state.module.addPresentationalDependency(dep);
|
37 | parser.state.module.buildInfo.strict = true;
|
38 | }
|
39 | });
|
40 | };
|
41 |
|
42 | normalModuleFactory.hooks.parser
|
43 | .for("javascript/auto")
|
44 | .tap("UseStrictPlugin", handler);
|
45 | normalModuleFactory.hooks.parser
|
46 | .for("javascript/dynamic")
|
47 | .tap("UseStrictPlugin", handler);
|
48 | normalModuleFactory.hooks.parser
|
49 | .for("javascript/esm")
|
50 | .tap("UseStrictPlugin", handler);
|
51 | }
|
52 | );
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = UseStrictPlugin;
|