EvalSourceMapDevToolPlugin.js 7.2 KB

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