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