EvalDevToolModulePlugin.js 4.1 KB

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