UseStrictPlugin.js 2.6 KB

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