[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { ConcatSource, RawSource } = require("webpack-sources");
|
---|
| 9 | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
|
---|
| 10 | const NormalModule = require("./NormalModule");
|
---|
| 11 | const RuntimeGlobals = require("./RuntimeGlobals");
|
---|
| 12 | const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
|
---|
| 13 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
---|
| 14 | const ConcatenatedModule = require("./optimize/ConcatenatedModule");
|
---|
| 15 | const generateDebugId = require("./util/generateDebugId");
|
---|
| 16 | const { makePathsAbsolute } = require("./util/identifier");
|
---|
| 17 |
|
---|
| 18 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 19 | /** @typedef {import("../declarations/WebpackOptions").DevTool} DevToolOptions */
|
---|
| 20 | /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
|
---|
| 21 | /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
|
---|
| 22 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 23 | /** @typedef {import("./NormalModule").SourceMap} SourceMap */
|
---|
| 24 |
|
---|
| 25 | /** @type {WeakMap<Source, Source>} */
|
---|
| 26 | const cache = new WeakMap();
|
---|
| 27 |
|
---|
| 28 | const devtoolWarning = new RawSource(`/*
|
---|
| 29 | * ATTENTION: An "eval-source-map" devtool has been used.
|
---|
| 30 | * This devtool is neither made for production nor for readable output files.
|
---|
| 31 | * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
|
---|
| 32 | * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
---|
| 33 | * or disable the default devtool with "devtool: false".
|
---|
| 34 | * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
---|
| 35 | */
|
---|
| 36 | `);
|
---|
| 37 |
|
---|
| 38 | class EvalSourceMapDevToolPlugin {
|
---|
| 39 | /**
|
---|
| 40 | * @param {SourceMapDevToolPluginOptions|string} inputOptions Options object
|
---|
| 41 | */
|
---|
| 42 | constructor(inputOptions) {
|
---|
| 43 | /** @type {SourceMapDevToolPluginOptions} */
|
---|
| 44 | let options;
|
---|
| 45 | if (typeof inputOptions === "string") {
|
---|
| 46 | options = {
|
---|
| 47 | append: inputOptions
|
---|
| 48 | };
|
---|
| 49 | } else {
|
---|
| 50 | options = inputOptions;
|
---|
| 51 | }
|
---|
| 52 | this.sourceMapComment =
|
---|
| 53 | options.append && typeof options.append !== "function"
|
---|
| 54 | ? options.append
|
---|
| 55 | : "//# sourceURL=[module]\n//# sourceMappingURL=[url]";
|
---|
| 56 | this.moduleFilenameTemplate =
|
---|
| 57 | options.moduleFilenameTemplate ||
|
---|
| 58 | "webpack://[namespace]/[resource-path]?[hash]";
|
---|
| 59 | this.namespace = options.namespace || "";
|
---|
| 60 | this.options = options;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Apply the plugin
|
---|
| 65 | * @param {Compiler} compiler the compiler instance
|
---|
| 66 | * @returns {void}
|
---|
| 67 | */
|
---|
| 68 | apply(compiler) {
|
---|
| 69 | const options = this.options;
|
---|
| 70 | compiler.hooks.compilation.tap(
|
---|
| 71 | "EvalSourceMapDevToolPlugin",
|
---|
| 72 | compilation => {
|
---|
| 73 | const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
---|
| 74 | new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
|
---|
| 75 | const matchModule = ModuleFilenameHelpers.matchObject.bind(
|
---|
| 76 | ModuleFilenameHelpers,
|
---|
| 77 | options
|
---|
| 78 | );
|
---|
| 79 | hooks.renderModuleContent.tap(
|
---|
| 80 | "EvalSourceMapDevToolPlugin",
|
---|
| 81 | (source, m, { chunk, runtimeTemplate, chunkGraph }) => {
|
---|
| 82 | const cachedSource = cache.get(source);
|
---|
| 83 | if (cachedSource !== undefined) {
|
---|
| 84 | return cachedSource;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * @param {Source} r result
|
---|
| 89 | * @returns {Source} result
|
---|
| 90 | */
|
---|
| 91 | const result = r => {
|
---|
| 92 | cache.set(source, r);
|
---|
| 93 | return r;
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | if (m instanceof NormalModule) {
|
---|
| 97 | const module = /** @type {NormalModule} */ (m);
|
---|
| 98 | if (!matchModule(module.resource)) {
|
---|
| 99 | return result(source);
|
---|
| 100 | }
|
---|
| 101 | } else if (m instanceof ConcatenatedModule) {
|
---|
| 102 | const concatModule = /** @type {ConcatenatedModule} */ (m);
|
---|
| 103 | if (concatModule.rootModule instanceof NormalModule) {
|
---|
| 104 | const module = /** @type {NormalModule} */ (
|
---|
| 105 | concatModule.rootModule
|
---|
| 106 | );
|
---|
| 107 | if (!matchModule(module.resource)) {
|
---|
| 108 | return result(source);
|
---|
| 109 | }
|
---|
| 110 | } else {
|
---|
| 111 | return result(source);
|
---|
| 112 | }
|
---|
| 113 | } else {
|
---|
| 114 | return result(source);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | const namespace = compilation.getPath(this.namespace, {
|
---|
| 118 | chunk
|
---|
| 119 | });
|
---|
| 120 | /** @type {SourceMap} */
|
---|
| 121 | let sourceMap;
|
---|
| 122 | let content;
|
---|
| 123 | if (source.sourceAndMap) {
|
---|
| 124 | const sourceAndMap = source.sourceAndMap(options);
|
---|
| 125 | sourceMap = /** @type {SourceMap} */ (sourceAndMap.map);
|
---|
| 126 | content = sourceAndMap.source;
|
---|
| 127 | } else {
|
---|
| 128 | sourceMap = /** @type {SourceMap} */ (source.map(options));
|
---|
| 129 | content = source.source();
|
---|
| 130 | }
|
---|
| 131 | if (!sourceMap) {
|
---|
| 132 | return result(source);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // Clone (flat) the sourcemap to ensure that the mutations below do not persist.
|
---|
| 136 | sourceMap = { ...sourceMap };
|
---|
| 137 | const context = /** @type {string} */ (compiler.options.context);
|
---|
| 138 | const root = compiler.root;
|
---|
| 139 | const modules = sourceMap.sources.map(source => {
|
---|
| 140 | if (!source.startsWith("webpack://")) return source;
|
---|
| 141 | source = makePathsAbsolute(context, source.slice(10), root);
|
---|
| 142 | const module = compilation.findModule(source);
|
---|
| 143 | return module || source;
|
---|
| 144 | });
|
---|
| 145 | let moduleFilenames = modules.map(module =>
|
---|
| 146 | ModuleFilenameHelpers.createFilename(
|
---|
| 147 | module,
|
---|
| 148 | {
|
---|
| 149 | moduleFilenameTemplate: this.moduleFilenameTemplate,
|
---|
| 150 | namespace
|
---|
| 151 | },
|
---|
| 152 | {
|
---|
| 153 | requestShortener: runtimeTemplate.requestShortener,
|
---|
| 154 | chunkGraph,
|
---|
| 155 | hashFunction: compilation.outputOptions.hashFunction
|
---|
| 156 | }
|
---|
| 157 | )
|
---|
| 158 | );
|
---|
| 159 | moduleFilenames = ModuleFilenameHelpers.replaceDuplicates(
|
---|
| 160 | moduleFilenames,
|
---|
| 161 | (filename, i, n) => {
|
---|
| 162 | for (let j = 0; j < n; j++) filename += "*";
|
---|
| 163 | return filename;
|
---|
| 164 | }
|
---|
| 165 | );
|
---|
| 166 | sourceMap.sources = moduleFilenames;
|
---|
| 167 | if (options.noSources) {
|
---|
| 168 | sourceMap.sourcesContent = undefined;
|
---|
| 169 | }
|
---|
| 170 | sourceMap.sourceRoot = options.sourceRoot || "";
|
---|
| 171 | const moduleId =
|
---|
| 172 | /** @type {ModuleId} */
|
---|
| 173 | (chunkGraph.getModuleId(m));
|
---|
| 174 | sourceMap.file =
|
---|
| 175 | typeof moduleId === "number" ? `${moduleId}.js` : moduleId;
|
---|
| 176 |
|
---|
| 177 | if (options.debugIds) {
|
---|
| 178 | sourceMap.debugId = generateDebugId(content, sourceMap.file);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | const footer = `${this.sourceMapComment.replace(
|
---|
| 182 | /\[url\]/g,
|
---|
| 183 | `data:application/json;charset=utf-8;base64,${Buffer.from(
|
---|
| 184 | JSON.stringify(sourceMap),
|
---|
| 185 | "utf8"
|
---|
| 186 | ).toString("base64")}`
|
---|
| 187 | )}\n//# sourceURL=webpack-internal:///${moduleId}\n`; // workaround for chrome bug
|
---|
| 188 |
|
---|
| 189 | return result(
|
---|
| 190 | new RawSource(
|
---|
| 191 | `eval(${
|
---|
| 192 | compilation.outputOptions.trustedTypes
|
---|
| 193 | ? `${RuntimeGlobals.createScript}(${JSON.stringify(
|
---|
| 194 | content + footer
|
---|
| 195 | )})`
|
---|
| 196 | : JSON.stringify(content + footer)
|
---|
| 197 | });`
|
---|
| 198 | )
|
---|
| 199 | );
|
---|
| 200 | }
|
---|
| 201 | );
|
---|
| 202 | hooks.inlineInRuntimeBailout.tap(
|
---|
| 203 | "EvalDevToolModulePlugin",
|
---|
| 204 | () => "the eval-source-map devtool is used."
|
---|
| 205 | );
|
---|
| 206 | hooks.render.tap(
|
---|
| 207 | "EvalSourceMapDevToolPlugin",
|
---|
| 208 | source => new ConcatSource(devtoolWarning, source)
|
---|
| 209 | );
|
---|
| 210 | hooks.chunkHash.tap("EvalSourceMapDevToolPlugin", (chunk, hash) => {
|
---|
| 211 | hash.update("EvalSourceMapDevToolPlugin");
|
---|
| 212 | hash.update("2");
|
---|
| 213 | });
|
---|
| 214 | if (compilation.outputOptions.trustedTypes) {
|
---|
| 215 | compilation.hooks.additionalModuleRuntimeRequirements.tap(
|
---|
| 216 | "EvalSourceMapDevToolPlugin",
|
---|
| 217 | (module, set, context) => {
|
---|
| 218 | set.add(RuntimeGlobals.createScript);
|
---|
| 219 | }
|
---|
| 220 | );
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | );
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | module.exports = EvalSourceMapDevToolPlugin;
|
---|