Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

127 строки
4.1 KiB

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