You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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