EvalSourceMapDevToolPlugin.js 7.4 KB

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