UNPKG

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