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