EvalSourceMapDevToolPlugin.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  8. const NormalModule = require("./NormalModule");
  9. const RuntimeGlobals = require("./RuntimeGlobals");
  10. const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
  11. const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
  12. const ConcatenatedModule = require("./optimize/ConcatenatedModule");
  13. const { makePathsAbsolute } = require("./util/identifier");
  14. /** @typedef {import("webpack-sources").Source} Source */
  15. /** @typedef {import("../declarations/WebpackOptions").DevTool} DevToolOptions */
  16. /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
  17. /** @typedef {import("./Compiler")} Compiler */
  18. /** @typedef {import("./NormalModule").SourceMap} SourceMap */
  19. /** @type {WeakMap<Source, Source>} */
  20. const cache = new WeakMap();
  21. const devtoolWarning = new RawSource(`/*
  22. * ATTENTION: An "eval-source-map" devtool has been used.
  23. * This devtool is neither made for production nor for readable output files.
  24. * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
  25. * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
  26. * or disable the default devtool with "devtool: false".
  27. * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
  28. */
  29. `);
  30. class EvalSourceMapDevToolPlugin {
  31. /**
  32. * @param {SourceMapDevToolPluginOptions|string} inputOptions Options object
  33. */
  34. constructor(inputOptions) {
  35. /** @type {SourceMapDevToolPluginOptions} */
  36. let options;
  37. if (typeof inputOptions === "string") {
  38. options = {
  39. append: inputOptions
  40. };
  41. } else {
  42. options = inputOptions;
  43. }
  44. this.sourceMapComment =
  45. options.append || "//# sourceURL=[module]\n//# sourceMappingURL=[url]";
  46. this.moduleFilenameTemplate =
  47. options.moduleFilenameTemplate ||
  48. "webpack://[namespace]/[resource-path]?[hash]";
  49. this.namespace = options.namespace || "";
  50. this.options = options;
  51. }
  52. /**
  53. * Apply the plugin
  54. * @param {Compiler} compiler the compiler instance
  55. * @returns {void}
  56. */
  57. apply(compiler) {
  58. const options = this.options;
  59. compiler.hooks.compilation.tap(
  60. "EvalSourceMapDevToolPlugin",
  61. compilation => {
  62. const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
  63. new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
  64. const matchModule = ModuleFilenameHelpers.matchObject.bind(
  65. ModuleFilenameHelpers,
  66. options
  67. );
  68. hooks.renderModuleContent.tap(
  69. "EvalSourceMapDevToolPlugin",
  70. (source, m, { runtimeTemplate, chunkGraph }) => {
  71. const cachedSource = cache.get(source);
  72. if (cachedSource !== undefined) {
  73. return cachedSource;
  74. }
  75. const result = r => {
  76. cache.set(source, r);
  77. return r;
  78. };
  79. if (m instanceof NormalModule) {
  80. const module = /** @type {NormalModule} */ (m);
  81. if (!matchModule(module.resource)) {
  82. return result(source);
  83. }
  84. } else if (m instanceof ConcatenatedModule) {
  85. const concatModule = /** @type {ConcatenatedModule} */ (m);
  86. if (concatModule.rootModule instanceof NormalModule) {
  87. const module = /** @type {NormalModule} */ (
  88. concatModule.rootModule
  89. );
  90. if (!matchModule(module.resource)) {
  91. return result(source);
  92. }
  93. } else {
  94. return result(source);
  95. }
  96. } else {
  97. return result(source);
  98. }
  99. /** @type {SourceMap} */
  100. let sourceMap;
  101. let content;
  102. if (source.sourceAndMap) {
  103. const sourceAndMap = source.sourceAndMap(options);
  104. sourceMap = /** @type {SourceMap} */ (sourceAndMap.map);
  105. content = sourceAndMap.source;
  106. } else {
  107. sourceMap = /** @type {SourceMap} */ (source.map(options));
  108. content = source.source();
  109. }
  110. if (!sourceMap) {
  111. return result(source);
  112. }
  113. // Clone (flat) the sourcemap to ensure that the mutations below do not persist.
  114. sourceMap = { ...sourceMap };
  115. const context = compiler.options.context;
  116. const root = compiler.root;
  117. const modules = sourceMap.sources.map(source => {
  118. if (!source.startsWith("webpack://")) return source;
  119. source = makePathsAbsolute(context, source.slice(10), root);
  120. const module = compilation.findModule(source);
  121. return module || source;
  122. });
  123. let moduleFilenames = modules.map(module => {
  124. return ModuleFilenameHelpers.createFilename(
  125. module,
  126. {
  127. moduleFilenameTemplate: this.moduleFilenameTemplate,
  128. namespace: this.namespace
  129. },
  130. {
  131. requestShortener: runtimeTemplate.requestShortener,
  132. chunkGraph,
  133. hashFunction: compilation.outputOptions.hashFunction
  134. }
  135. );
  136. });
  137. moduleFilenames = ModuleFilenameHelpers.replaceDuplicates(
  138. moduleFilenames,
  139. (filename, i, n) => {
  140. for (let j = 0; j < n; j++) filename += "*";
  141. return filename;
  142. }
  143. );
  144. sourceMap.sources = moduleFilenames;
  145. if (options.noSources) {
  146. sourceMap.sourcesContent = undefined;
  147. }
  148. sourceMap.sourceRoot = options.sourceRoot || "";
  149. const moduleId = chunkGraph.getModuleId(m);
  150. sourceMap.file = `${moduleId}.js`;
  151. const footer =
  152. this.sourceMapComment.replace(
  153. /\[url\]/g,
  154. `data:application/json;charset=utf-8;base64,${Buffer.from(
  155. JSON.stringify(sourceMap),
  156. "utf8"
  157. ).toString("base64")}`
  158. ) + `\n//# sourceURL=webpack-internal:///${moduleId}\n`; // workaround for chrome bug
  159. return result(
  160. new RawSource(
  161. `eval(${
  162. compilation.outputOptions.trustedTypes
  163. ? `${RuntimeGlobals.createScript}(${JSON.stringify(
  164. content + footer
  165. )})`
  166. : JSON.stringify(content + footer)
  167. });`
  168. )
  169. );
  170. }
  171. );
  172. hooks.inlineInRuntimeBailout.tap(
  173. "EvalDevToolModulePlugin",
  174. () => "the eval-source-map devtool is used."
  175. );
  176. hooks.render.tap(
  177. "EvalSourceMapDevToolPlugin",
  178. source => new ConcatSource(devtoolWarning, source)
  179. );
  180. hooks.chunkHash.tap("EvalSourceMapDevToolPlugin", (chunk, hash) => {
  181. hash.update("EvalSourceMapDevToolPlugin");
  182. hash.update("2");
  183. });
  184. if (compilation.outputOptions.trustedTypes) {
  185. compilation.hooks.additionalModuleRuntimeRequirements.tap(
  186. "EvalSourceMapDevToolPlugin",
  187. (module, set, context) => {
  188. set.add(RuntimeGlobals.createScript);
  189. }
  190. );
  191. }
  192. }
  193. );
  194. }
  195. }
  196. module.exports = EvalSourceMapDevToolPlugin;