1 |
|
2 |
|
3 |
|
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 |
|
15 |
|
16 |
|
17 |
|
18 | const cache = new WeakMap();
|
19 |
|
20 | const devtoolWarning = new RawSource(`/*
|
21 | * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
22 | * This devtool is neither made for production nor for readable output files.
|
23 | * It uses "eval()" calls to create a separate source file in the browser devtools.
|
24 | * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
25 | * or disable the default devtool with "devtool: false".
|
26 | * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
27 | */
|
28 | `);
|
29 |
|
30 | class EvalDevToolModulePlugin {
|
31 | constructor(options) {
|
32 | this.namespace = options.namespace || "";
|
33 | this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]";
|
34 | this.moduleFilenameTemplate =
|
35 | options.moduleFilenameTemplate ||
|
36 | "webpack://[namespace]/[resourcePath]?[loaders]";
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | apply(compiler) {
|
45 | compiler.hooks.compilation.tap("EvalDevToolModulePlugin", compilation => {
|
46 | const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
47 | hooks.renderModuleContent.tap(
|
48 | "EvalDevToolModulePlugin",
|
49 | (source, module, { runtimeTemplate, chunkGraph }) => {
|
50 | const cacheEntry = cache.get(source);
|
51 | if (cacheEntry !== undefined) return cacheEntry;
|
52 | if (module instanceof ExternalModule) {
|
53 | cache.set(source, source);
|
54 | return source;
|
55 | }
|
56 | const content = source.source();
|
57 | const str = ModuleFilenameHelpers.createFilename(
|
58 | module,
|
59 | {
|
60 | moduleFilenameTemplate: this.moduleFilenameTemplate,
|
61 | namespace: this.namespace
|
62 | },
|
63 | {
|
64 | requestShortener: runtimeTemplate.requestShortener,
|
65 | chunkGraph,
|
66 | hashFunction: compilation.outputOptions.hashFunction
|
67 | }
|
68 | );
|
69 | const footer =
|
70 | "\n" +
|
71 | this.sourceUrlComment.replace(
|
72 | /\[url\]/g,
|
73 | encodeURI(str)
|
74 | .replace(/%2F/g, "/")
|
75 | .replace(/%20/g, "_")
|
76 | .replace(/%5E/g, "^")
|
77 | .replace(/%5C/g, "\\")
|
78 | .replace(/^\//, "")
|
79 | );
|
80 | const result = new RawSource(
|
81 | `eval(${
|
82 | compilation.outputOptions.trustedTypes
|
83 | ? `${RuntimeGlobals.createScript}(${JSON.stringify(
|
84 | content + footer
|
85 | )})`
|
86 | : JSON.stringify(content + footer)
|
87 | });`
|
88 | );
|
89 | cache.set(source, result);
|
90 | return result;
|
91 | }
|
92 | );
|
93 | hooks.inlineInRuntimeBailout.tap(
|
94 | "EvalDevToolModulePlugin",
|
95 | () => "the eval devtool is used."
|
96 | );
|
97 | hooks.render.tap(
|
98 | "EvalDevToolModulePlugin",
|
99 | source => new ConcatSource(devtoolWarning, source)
|
100 | );
|
101 | hooks.chunkHash.tap("EvalDevToolModulePlugin", (chunk, hash) => {
|
102 | hash.update("EvalDevToolModulePlugin");
|
103 | hash.update("2");
|
104 | });
|
105 | if (compilation.outputOptions.trustedTypes) {
|
106 | compilation.hooks.additionalModuleRuntimeRequirements.tap(
|
107 | "EvalDevToolModulePlugin",
|
108 | (module, set, context) => {
|
109 | set.add(RuntimeGlobals.createScript);
|
110 | }
|
111 | );
|
112 | }
|
113 | });
|
114 | }
|
115 | }
|
116 |
|
117 | module.exports = EvalDevToolModulePlugin;
|