[6a3a178] | 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 ConstDependency = require("./dependencies/ConstDependency");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 11 |
|
---|
| 12 | class UseStrictPlugin {
|
---|
| 13 | /**
|
---|
| 14 | * Apply the plugin
|
---|
| 15 | * @param {Compiler} compiler the compiler instance
|
---|
| 16 | * @returns {void}
|
---|
| 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 | // Remove "use strict" expression. It will be added later by the renderer again.
|
---|
| 32 | // This is necessary in order to not break the strict mode when webpack prepends code.
|
---|
| 33 | // @see https://github.com/webpack/webpack/issues/1970
|
---|
| 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;
|
---|