| [9af201e] | 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 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 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 | * Handles the hook callback for this code path.
|
|---|
| 36 | * @param {JavascriptParser} parser the parser
|
|---|
| 37 | * @param {JavascriptParserOptions} parserOptions the javascript parser options
|
|---|
| 38 | */
|
|---|
| 39 | const handler = (parser, parserOptions) => {
|
|---|
| 40 | parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
|
|---|
| 41 | const firstNode = ast.body[0];
|
|---|
| 42 | if (
|
|---|
| 43 | firstNode &&
|
|---|
| 44 | firstNode.type === "ExpressionStatement" &&
|
|---|
| 45 | firstNode.expression.type === "Literal" &&
|
|---|
| 46 | firstNode.expression.value === "use strict"
|
|---|
| 47 | ) {
|
|---|
| 48 | // Remove "use strict" expression. It will be added later by the renderer again.
|
|---|
| 49 | // This is necessary in order to not break the strict mode when webpack prepends code.
|
|---|
| 50 | // @see https://github.com/webpack/webpack/issues/1970
|
|---|
| 51 | const dep = new ConstDependency(
|
|---|
| 52 | "",
|
|---|
| 53 | /** @type {Range} */ (firstNode.range)
|
|---|
| 54 | );
|
|---|
| 55 | dep.loc = /** @type {DependencyLocation} */ (firstNode.loc);
|
|---|
| 56 | parser.state.module.addPresentationalDependency(dep);
|
|---|
| 57 | /** @type {BuildInfo} */
|
|---|
| 58 | (parser.state.module.buildInfo).strict = true;
|
|---|
| 59 | }
|
|---|
| 60 | if (parserOptions.overrideStrict) {
|
|---|
| 61 | /** @type {BuildInfo} */
|
|---|
| 62 | (parser.state.module.buildInfo).strict =
|
|---|
| 63 | parserOptions.overrideStrict === "strict";
|
|---|
| 64 | }
|
|---|
| 65 | });
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | normalModuleFactory.hooks.parser
|
|---|
| 69 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 70 | .tap(PLUGIN_NAME, handler);
|
|---|
| 71 | normalModuleFactory.hooks.parser
|
|---|
| 72 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 73 | .tap(PLUGIN_NAME, handler);
|
|---|
| 74 | normalModuleFactory.hooks.parser
|
|---|
| 75 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
|---|
| 76 | .tap(PLUGIN_NAME, handler);
|
|---|
| 77 | }
|
|---|
| 78 | );
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | module.exports = UseStrictPlugin;
|
|---|