UNPKG

6.83 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource, RawSource } = require("webpack-sources");
9const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
10const NormalModule = require("./NormalModule");
11const RuntimeGlobals = require("./RuntimeGlobals");
12const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
13const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
14const ConcatenatedModule = require("./optimize/ConcatenatedModule");
15const { makePathsAbsolute } = require("./util/identifier");
16
17/** @typedef {import("webpack-sources").Source} Source */
18/** @typedef {import("../declarations/WebpackOptions").DevTool} DevToolOptions */
19/** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
20/** @typedef {import("./Compiler")} Compiler */
21
22/** @type {WeakMap<Source, Source>} */
23const cache = new WeakMap();
24
25const devtoolWarning = new RawSource(`/*
26 * ATTENTION: An "eval-source-map" devtool has been used.
27 * This devtool is neither made for production nor for readable output files.
28 * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
29 * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
30 * or disable the default devtool with "devtool: false".
31 * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
32 */
33`);
34
35class EvalSourceMapDevToolPlugin {
36 /**
37 * @param {SourceMapDevToolPluginOptions|string} inputOptions Options object
38 */
39 constructor(inputOptions) {
40 /** @type {SourceMapDevToolPluginOptions} */
41 let options;
42 if (typeof inputOptions === "string") {
43 options = {
44 append: inputOptions
45 };
46 } else {
47 options = inputOptions;
48 }
49 this.sourceMapComment =
50 options.append || "//# 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 /**
59 * Apply the plugin
60 * @param {Compiler} compiler the compiler instance
61 * @returns {void}
62 */
63 apply(compiler) {
64 const options = this.options;
65 compiler.hooks.compilation.tap(
66 "EvalSourceMapDevToolPlugin",
67 compilation => {
68 const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
69 new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
70 const matchModule = ModuleFilenameHelpers.matchObject.bind(
71 ModuleFilenameHelpers,
72 options
73 );
74 hooks.renderModuleContent.tap(
75 "EvalSourceMapDevToolPlugin",
76 (source, m, { runtimeTemplate, chunkGraph }) => {
77 const cachedSource = cache.get(source);
78 if (cachedSource !== undefined) {
79 return cachedSource;
80 }
81
82 const result = r => {
83 cache.set(source, r);
84 return r;
85 };
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
108 /** @type {{ [key: string]: TODO; }} */
109 let sourceMap;
110 let content;
111 if (source.sourceAndMap) {
112 const sourceAndMap = source.sourceAndMap(options);
113 sourceMap = sourceAndMap.map;
114 content = sourceAndMap.source;
115 } else {
116 sourceMap = source.map(options);
117 content = source.source();
118 }
119 if (!sourceMap) {
120 return result(source);
121 }
122
123 // Clone (flat) the sourcemap to ensure that the mutations below do not persist.
124 sourceMap = { ...sourceMap };
125 const context = 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 return ModuleFilenameHelpers.createFilename(
135 module,
136 {
137 moduleFilenameTemplate: this.moduleFilenameTemplate,
138 namespace: this.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 sourceMap.sourceRoot = options.sourceRoot || "";
156 const moduleId = chunkGraph.getModuleId(m);
157 sourceMap.file = `${moduleId}.js`;
158
159 const footer =
160 this.sourceMapComment.replace(
161 /\[url\]/g,
162 `data:application/json;charset=utf-8;base64,${Buffer.from(
163 JSON.stringify(sourceMap),
164 "utf8"
165 ).toString("base64")}`
166 ) + `\n//# sourceURL=webpack-internal:///${moduleId}\n`; // workaround for chrome bug
167
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
206module.exports = EvalSourceMapDevToolPlugin;