EvalDevToolModulePlugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @typedef {object} EvalDevToolModulePluginOptions
  28. * @property {DevtoolNamespace=} namespace namespace
  29. * @property {string=} sourceUrlComment source url comment
  30. * @property {DevtoolModuleFilenameTemplate=} moduleFilenameTemplate module filename template
  31. */
  32. const PLUGIN_NAME = "EvalDevToolModulePlugin";
  33. class EvalDevToolModulePlugin {
  34. /**
  35. * @param {EvalDevToolModulePluginOptions=} options options
  36. */
  37. constructor(options = {}) {
  38. /** @type {DevtoolNamespace} */
  39. this.namespace = options.namespace || "";
  40. /** @type {string} */
  41. this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]";
  42. /** @type {DevtoolModuleFilenameTemplate} */
  43. this.moduleFilenameTemplate =
  44. options.moduleFilenameTemplate ||
  45. "webpack://[namespace]/[resourcePath]?[loaders]";
  46. }
  47. /**
  48. * Apply the plugin
  49. * @param {Compiler} compiler the compiler instance
  50. * @returns {void}
  51. */
  52. apply(compiler) {
  53. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  54. const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
  55. hooks.renderModuleContent.tap(
  56. PLUGIN_NAME,
  57. (source, module, { chunk, runtimeTemplate, chunkGraph }) => {
  58. const cacheEntry = cache.get(source);
  59. if (cacheEntry !== undefined) return cacheEntry;
  60. if (module instanceof ExternalModule) {
  61. cache.set(source, source);
  62. return source;
  63. }
  64. const content = source.source();
  65. const namespace = compilation.getPath(this.namespace, {
  66. chunk
  67. });
  68. const str = ModuleFilenameHelpers.createFilename(
  69. module,
  70. {
  71. moduleFilenameTemplate: this.moduleFilenameTemplate,
  72. namespace
  73. },
  74. {
  75. requestShortener: runtimeTemplate.requestShortener,
  76. chunkGraph,
  77. hashFunction: compilation.outputOptions.hashFunction
  78. }
  79. );
  80. const footer = `\n${this.sourceUrlComment.replace(
  81. /\[url\]/g,
  82. encodeURI(str)
  83. .replace(/%2F/g, "/")
  84. .replace(/%20/g, "_")
  85. .replace(/%5E/g, "^")
  86. .replace(/%5C/g, "\\")
  87. .replace(/^\//, "")
  88. )}`;
  89. const result = new RawSource(
  90. `eval(${
  91. compilation.outputOptions.trustedTypes
  92. ? `${RuntimeGlobals.createScript}(${JSON.stringify(
  93. `{${content + footer}\n}`
  94. )})`
  95. : JSON.stringify(`{${content + footer}\n}`)
  96. });`
  97. );
  98. cache.set(source, result);
  99. return result;
  100. }
  101. );
  102. hooks.inlineInRuntimeBailout.tap(
  103. PLUGIN_NAME,
  104. () => "the eval devtool is used."
  105. );
  106. hooks.render.tap(
  107. PLUGIN_NAME,
  108. (source) => new ConcatSource(devtoolWarning, source)
  109. );
  110. hooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash) => {
  111. hash.update(PLUGIN_NAME);
  112. hash.update("2");
  113. });
  114. if (compilation.outputOptions.trustedTypes) {
  115. compilation.hooks.additionalModuleRuntimeRequirements.tap(
  116. PLUGIN_NAME,
  117. (module, set, _context) => {
  118. set.add(RuntimeGlobals.createScript);
  119. }
  120. );
  121. }
  122. });
  123. }
  124. }
  125. module.exports = EvalDevToolModulePlugin;