UNPKG

6.24 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 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 const result = r => {
82 cache.set(source, r);
83 return r;
84 };
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} */ (concatModule.rootModule);
95 if (!matchModule(module.resource)) {
96 return result(source);
97 }
98 } else {
99 return result(source);
100 }
101 } else {
102 return result(source);
103 }
104
105 /** @type {{ [key: string]: TODO; }} */
106 let sourceMap;
107 let content;
108 if (source.sourceAndMap) {
109 const sourceAndMap = source.sourceAndMap(options);
110 sourceMap = sourceAndMap.map;
111 content = sourceAndMap.source;
112 } else {
113 sourceMap = source.map(options);
114 content = source.source();
115 }
116 if (!sourceMap) {
117 return result(source);
118 }
119
120 // Clone (flat) the sourcemap to ensure that the mutations below do not persist.
121 sourceMap = { ...sourceMap };
122 const context = compiler.options.context;
123 const root = compiler.root;
124 const modules = sourceMap.sources.map(source => {
125 if (!source.startsWith("webpack://")) return source;
126 source = absolutify(context, source.slice(10), root);
127 const module = compilation.findModule(source);
128 return module || source;
129 });
130 let moduleFilenames = modules.map(module => {
131 return ModuleFilenameHelpers.createFilename(
132 module,
133 {
134 moduleFilenameTemplate: this.moduleFilenameTemplate,
135 namespace: this.namespace
136 },
137 {
138 requestShortener: runtimeTemplate.requestShortener,
139 chunkGraph
140 }
141 );
142 });
143 moduleFilenames = ModuleFilenameHelpers.replaceDuplicates(
144 moduleFilenames,
145 (filename, i, n) => {
146 for (let j = 0; j < n; j++) filename += "*";
147 return filename;
148 }
149 );
150 sourceMap.sources = moduleFilenames;
151 sourceMap.sourceRoot = options.sourceRoot || "";
152 const moduleId = chunkGraph.getModuleId(m);
153 sourceMap.file = `${moduleId}.js`;
154
155 const footer =
156 this.sourceMapComment.replace(
157 /\[url\]/g,
158 `data:application/json;charset=utf-8;base64,${Buffer.from(
159 JSON.stringify(sourceMap),
160 "utf8"
161 ).toString("base64")}`
162 ) + `\n//# sourceURL=webpack-internal:///${moduleId}\n`; // workaround for chrome bug
163
164 return result(
165 new RawSource(`eval(${JSON.stringify(content + footer)});`)
166 );
167 }
168 );
169 hooks.inlineInRuntimeBailout.tap(
170 "EvalDevToolModulePlugin",
171 () => "the eval-source-map devtool is used."
172 );
173 hooks.render.tap(
174 "EvalSourceMapDevToolPlugin",
175 source => new ConcatSource(devtoolWarning, source)
176 );
177 hooks.chunkHash.tap("EvalSourceMapDevToolPlugin", (chunk, hash) => {
178 hash.update("EvalSourceMapDevToolPlugin");
179 hash.update("2");
180 });
181 }
182 );
183 }
184}
185
186module.exports = EvalSourceMapDevToolPlugin;