| [9af201e] | 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 ExternalModule = require("./ExternalModule");
|
|---|
| 10 | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
|
|---|
| 11 | const RuntimeGlobals = require("./RuntimeGlobals");
|
|---|
| 12 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("webpack-sources").Source} Source */
|
|---|
| 15 | /** @typedef {import("../declarations/WebpackOptions").DevtoolNamespace} DevtoolNamespace */
|
|---|
| 16 | /** @typedef {import("../declarations/WebpackOptions").DevtoolModuleFilenameTemplate} DevtoolModuleFilenameTemplate */
|
|---|
| 17 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 18 |
|
|---|
| 19 | /** @type {WeakMap<Source, Source>} */
|
|---|
| 20 | const cache = new WeakMap();
|
|---|
| 21 |
|
|---|
| 22 | const devtoolWarning = new RawSource(`/*
|
|---|
| 23 | * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
|---|
| 24 | * This devtool is neither made for production nor for readable output files.
|
|---|
| 25 | * It uses "eval()" calls to create a separate source file in the browser devtools.
|
|---|
| 26 | * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
|---|
| 27 | * or disable the default devtool with "devtool: false".
|
|---|
| 28 | * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
|---|
| 29 | */
|
|---|
| 30 | `);
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Defines the eval dev tool module plugin options type used by this module.
|
|---|
| 34 | * @typedef {object} EvalDevToolModulePluginOptions
|
|---|
| 35 | * @property {DevtoolNamespace=} namespace namespace
|
|---|
| 36 | * @property {string=} sourceUrlComment source url comment
|
|---|
| 37 | * @property {DevtoolModuleFilenameTemplate=} moduleFilenameTemplate module filename template
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | const PLUGIN_NAME = "EvalDevToolModulePlugin";
|
|---|
| 41 |
|
|---|
| 42 | class EvalDevToolModulePlugin {
|
|---|
| 43 | /**
|
|---|
| 44 | * Creates an instance of EvalDevToolModulePlugin.
|
|---|
| 45 | * @param {EvalDevToolModulePluginOptions=} options options
|
|---|
| 46 | */
|
|---|
| 47 | constructor(options = {}) {
|
|---|
| 48 | /** @type {DevtoolNamespace} */
|
|---|
| 49 | this.namespace = options.namespace || "";
|
|---|
| 50 | /** @type {string} */
|
|---|
| 51 | this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]";
|
|---|
| 52 | /** @type {DevtoolModuleFilenameTemplate} */
|
|---|
| 53 | this.moduleFilenameTemplate =
|
|---|
| 54 | options.moduleFilenameTemplate ||
|
|---|
| 55 | "webpack://[namespace]/[resourcePath]?[loaders]";
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 60 | * @param {Compiler} compiler the compiler instance
|
|---|
| 61 | * @returns {void}
|
|---|
| 62 | */
|
|---|
| 63 | apply(compiler) {
|
|---|
| 64 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 65 | const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
|---|
| 66 | hooks.renderModuleContent.tap(
|
|---|
| 67 | PLUGIN_NAME,
|
|---|
| 68 | (source, module, { chunk, runtimeTemplate, chunkGraph }) => {
|
|---|
| 69 | const cacheEntry = cache.get(source);
|
|---|
| 70 | if (cacheEntry !== undefined) return cacheEntry;
|
|---|
| 71 | if (module instanceof ExternalModule) {
|
|---|
| 72 | cache.set(source, source);
|
|---|
| 73 | return source;
|
|---|
| 74 | }
|
|---|
| 75 | const content = source.source();
|
|---|
| 76 | const namespace = compilation.getPath(this.namespace, {
|
|---|
| 77 | chunk
|
|---|
| 78 | });
|
|---|
| 79 | const str = ModuleFilenameHelpers.createFilename(
|
|---|
| 80 | module,
|
|---|
| 81 | {
|
|---|
| 82 | moduleFilenameTemplate: this.moduleFilenameTemplate,
|
|---|
| 83 | namespace
|
|---|
| 84 | },
|
|---|
| 85 | {
|
|---|
| 86 | requestShortener: runtimeTemplate.requestShortener,
|
|---|
| 87 | chunkGraph,
|
|---|
| 88 | hashFunction: compilation.outputOptions.hashFunction
|
|---|
| 89 | }
|
|---|
| 90 | );
|
|---|
| 91 | const footer = `\n${this.sourceUrlComment.replace(
|
|---|
| 92 | /\[url\]/g,
|
|---|
| 93 | encodeURI(str)
|
|---|
| 94 | .replace(/%2F/g, "/")
|
|---|
| 95 | .replace(/%20/g, "_")
|
|---|
| 96 | .replace(/%5E/g, "^")
|
|---|
| 97 | .replace(/%5C/g, "\\")
|
|---|
| 98 | .replace(/^\//, "")
|
|---|
| 99 | )}`;
|
|---|
| 100 | const result = new RawSource(
|
|---|
| 101 | `eval(${
|
|---|
| 102 | compilation.outputOptions.trustedTypes
|
|---|
| 103 | ? `${RuntimeGlobals.createScript}(${JSON.stringify(
|
|---|
| 104 | `{${content + footer}\n}`
|
|---|
| 105 | )})`
|
|---|
| 106 | : JSON.stringify(`{${content + footer}\n}`)
|
|---|
| 107 | });`
|
|---|
| 108 | );
|
|---|
| 109 | cache.set(source, result);
|
|---|
| 110 | return result;
|
|---|
| 111 | }
|
|---|
| 112 | );
|
|---|
| 113 | hooks.inlineInRuntimeBailout.tap(
|
|---|
| 114 | PLUGIN_NAME,
|
|---|
| 115 | () => "the eval devtool is used."
|
|---|
| 116 | );
|
|---|
| 117 | hooks.render.tap(
|
|---|
| 118 | PLUGIN_NAME,
|
|---|
| 119 | (source) => new ConcatSource(devtoolWarning, source)
|
|---|
| 120 | );
|
|---|
| 121 | hooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash) => {
|
|---|
| 122 | hash.update(PLUGIN_NAME);
|
|---|
| 123 | hash.update("2");
|
|---|
| 124 | });
|
|---|
| 125 | if (compilation.outputOptions.trustedTypes) {
|
|---|
| 126 | compilation.hooks.additionalModuleRuntimeRequirements.tap(
|
|---|
| 127 | PLUGIN_NAME,
|
|---|
| 128 | (module, set, _context) => {
|
|---|
| 129 | set.add(RuntimeGlobals.createScript);
|
|---|
| 130 | }
|
|---|
| 131 | );
|
|---|
| 132 | }
|
|---|
| 133 | });
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | module.exports = EvalDevToolModulePlugin;
|
|---|