EvalDevToolModulePlugin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource, RawSource } = require("webpack-sources");
  7. const ExternalModule = require("./ExternalModule");
  8. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  9. const RuntimeGlobals = require("./RuntimeGlobals");
  10. const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("../declarations/WebpackOptions").DevtoolNamespace} DevtoolNamespace */
  13. /** @typedef {import("../declarations/WebpackOptions").DevtoolModuleFilenameTemplate} DevtoolModuleFilenameTemplate */
  14. /** @typedef {import("./Compiler")} Compiler */
  15. /** @type {WeakMap<Source, Source>} */
  16. const cache = new WeakMap();
  17. const devtoolWarning = new RawSource(`/*
  18. * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
  19. * This devtool is neither made for production nor for readable output files.
  20. * It uses "eval()" calls to create a separate source file in the browser devtools.
  21. * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
  22. * or disable the default devtool with "devtool: false".
  23. * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
  24. */
  25. `);
  26. /**
  27. * Defines the eval dev tool module plugin options type used by this module.
  28. * @typedef {object} EvalDevToolModulePluginOptions
  29. * @property {DevtoolNamespace=} namespace namespace
  30. * @property {string=} sourceUrlComment source url comment
  31. * @property {DevtoolModuleFilenameTemplate=} moduleFilenameTemplate module filename template
  32. */
  33. const PLUGIN_NAME = "EvalDevToolModulePlugin";
  34. class EvalDevToolModulePlugin {
  35. /**
  36. * Creates an instance of EvalDevToolModulePlugin.
  37. * @param {EvalDevToolModulePluginOptions=} options options
  38. */
  39. constructor(options = {}) {
  40. /** @type {DevtoolNamespace} */
  41. this.namespace = options.namespace || "";
  42. /** @type {string} */
  43. this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]";
  44. /** @type {DevtoolModuleFilenameTemplate} */
  45. this.moduleFilenameTemplate =
  46. options.moduleFilenameTemplate ||
  47. "webpack://[namespace]/[resourcePath]?[loaders]";
  48. }
  49. /**
  50. * Applies the plugin by registering its hooks on the compiler.
  51. * @param {Compiler} compiler the compiler instance
  52. * @returns {void}
  53. */
  54. apply(compiler) {
  55. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  56. const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
  57. hooks.renderModuleContent.tap(
  58. PLUGIN_NAME,
  59. (source, module, { chunk, runtimeTemplate, chunkGraph }) => {
  60. const cacheEntry = cache.get(source);
  61. if (cacheEntry !== undefined) return cacheEntry;
  62. if (module instanceof ExternalModule) {
  63. cache.set(source, source);
  64. return source;
  65. }
  66. const content = source.source();
  67. const namespace = compilation.getPath(this.namespace, {
  68. chunk
  69. });
  70. const str = ModuleFilenameHelpers.createFilename(
  71. module,
  72. {
  73. moduleFilenameTemplate: this.moduleFilenameTemplate,
  74. namespace
  75. },
  76. {
  77. requestShortener: runtimeTemplate.requestShortener,
  78. chunkGraph,
  79. hashFunction: compilation.outputOptions.hashFunction
  80. }
  81. );
  82. const footer = `\n${this.sourceUrlComment.replace(
  83. /\[url\]/g,
  84. encodeURI(str)
  85. .replace(/%2F/g, "/")
  86. .replace(/%20/g, "_")
  87. .replace(/%5E/g, "^")
  88. .replace(/%5C/g, "\\")
  89. .replace(/^\//, "")
  90. )}`;
  91. const result = new RawSource(
  92. `eval(${
  93. compilation.outputOptions.trustedTypes
  94. ? `${RuntimeGlobals.createScript}(${JSON.stringify(
  95. `{${content + footer}\n}`
  96. )})`
  97. : JSON.stringify(`{${content + footer}\n}`)
  98. });`
  99. );
  100. cache.set(source, result);
  101. return result;
  102. }
  103. );
  104. hooks.inlineInRuntimeBailout.tap(
  105. PLUGIN_NAME,
  106. () => "the eval devtool is used."
  107. );
  108. hooks.render.tap(
  109. PLUGIN_NAME,
  110. (source) => new ConcatSource(devtoolWarning, source)
  111. );
  112. hooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash) => {
  113. hash.update(PLUGIN_NAME);
  114. hash.update("2");
  115. });
  116. if (compilation.outputOptions.trustedTypes) {
  117. compilation.hooks.additionalModuleRuntimeRequirements.tap(
  118. PLUGIN_NAME,
  119. (module, set, _context) => {
  120. set.add(RuntimeGlobals.createScript);
  121. }
  122. );
  123. }
  124. });
  125. }
  126. }
  127. module.exports = EvalDevToolModulePlugin;