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