1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const {
|
---|
9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
---|
11 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
12 | } = require("./ModuleTypeConstants");
|
---|
13 | const ConstDependency = require("./dependencies/ConstDependency");
|
---|
14 |
|
---|
15 | /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
16 | /** @typedef {import("./Compiler")} Compiler */
|
---|
17 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
18 | /** @typedef {import("./Module").BuildInfo} BuildInfo */
|
---|
19 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
20 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
---|
21 |
|
---|
22 | const PLUGIN_NAME = "UseStrictPlugin";
|
---|
23 |
|
---|
24 | class UseStrictPlugin {
|
---|
25 | /**
|
---|
26 | * Apply the plugin
|
---|
27 | * @param {Compiler} compiler the compiler instance
|
---|
28 | * @returns {void}
|
---|
29 | */
|
---|
30 | apply(compiler) {
|
---|
31 | compiler.hooks.compilation.tap(
|
---|
32 | PLUGIN_NAME,
|
---|
33 | (compilation, { normalModuleFactory }) => {
|
---|
34 | /**
|
---|
35 | * @param {JavascriptParser} parser the parser
|
---|
36 | * @param {JavascriptParserOptions} parserOptions the javascript parser options
|
---|
37 | */
|
---|
38 | const handler = (parser, parserOptions) => {
|
---|
39 | parser.hooks.program.tap(PLUGIN_NAME, ast => {
|
---|
40 | const firstNode = ast.body[0];
|
---|
41 | if (
|
---|
42 | firstNode &&
|
---|
43 | firstNode.type === "ExpressionStatement" &&
|
---|
44 | firstNode.expression.type === "Literal" &&
|
---|
45 | firstNode.expression.value === "use strict"
|
---|
46 | ) {
|
---|
47 | // Remove "use strict" expression. It will be added later by the renderer again.
|
---|
48 | // This is necessary in order to not break the strict mode when webpack prepends code.
|
---|
49 | // @see https://github.com/webpack/webpack/issues/1970
|
---|
50 | const dep = new ConstDependency(
|
---|
51 | "",
|
---|
52 | /** @type {Range} */ (firstNode.range)
|
---|
53 | );
|
---|
54 | dep.loc = /** @type {DependencyLocation} */ (firstNode.loc);
|
---|
55 | parser.state.module.addPresentationalDependency(dep);
|
---|
56 | /** @type {BuildInfo} */
|
---|
57 | (parser.state.module.buildInfo).strict = true;
|
---|
58 | }
|
---|
59 | if (parserOptions.overrideStrict) {
|
---|
60 | /** @type {BuildInfo} */
|
---|
61 | (parser.state.module.buildInfo).strict =
|
---|
62 | parserOptions.overrideStrict === "strict";
|
---|
63 | }
|
---|
64 | });
|
---|
65 | };
|
---|
66 |
|
---|
67 | normalModuleFactory.hooks.parser
|
---|
68 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
69 | .tap(PLUGIN_NAME, handler);
|
---|
70 | normalModuleFactory.hooks.parser
|
---|
71 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
72 | .tap(PLUGIN_NAME, handler);
|
---|
73 | normalModuleFactory.hooks.parser
|
---|
74 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
75 | .tap(PLUGIN_NAME, handler);
|
---|
76 | }
|
---|
77 | );
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | module.exports = UseStrictPlugin;
|
---|