UNPKG

3.23 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource, RawSource } = require("webpack-sources");
9const ExternalModule = require("./ExternalModule");
10const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
11const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
12
13/** @typedef {import("webpack-sources").Source} Source */
14/** @typedef {import("./Compiler")} Compiler */
15
16/** @type {WeakMap<Source, Source>} */
17const cache = new WeakMap();
18
19const devtoolWarning = new RawSource(`/*
20 * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
21 * This devtool is neither made for production nor for readable output files.
22 * It uses "eval()" calls to create a separate source file in the browser devtools.
23 * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
24 * or disable the default devtool with "devtool: false".
25 * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
26 */
27`);
28
29class EvalDevToolModulePlugin {
30 constructor(options) {
31 this.namespace = options.namespace || "";
32 this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]";
33 this.moduleFilenameTemplate =
34 options.moduleFilenameTemplate ||
35 "webpack://[namespace]/[resourcePath]?[loaders]";
36 }
37
38 /**
39 * Apply the plugin
40 * @param {Compiler} compiler the compiler instance
41 * @returns {void}
42 */
43 apply(compiler) {
44 compiler.hooks.compilation.tap("EvalDevToolModulePlugin", compilation => {
45 const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
46 hooks.renderModuleContent.tap(
47 "EvalDevToolModulePlugin",
48 (source, module, { runtimeTemplate, chunkGraph }) => {
49 const cacheEntry = cache.get(source);
50 if (cacheEntry !== undefined) return cacheEntry;
51 if (module instanceof ExternalModule) {
52 cache.set(source, source);
53 return source;
54 }
55 const content = source.source();
56 const str = ModuleFilenameHelpers.createFilename(
57 module,
58 {
59 moduleFilenameTemplate: this.moduleFilenameTemplate,
60 namespace: this.namespace
61 },
62 {
63 requestShortener: runtimeTemplate.requestShortener,
64 chunkGraph
65 }
66 );
67 const footer =
68 "\n" +
69 this.sourceUrlComment.replace(
70 /\[url\]/g,
71 encodeURI(str)
72 .replace(/%2F/g, "/")
73 .replace(/%20/g, "_")
74 .replace(/%5E/g, "^")
75 .replace(/%5C/g, "\\")
76 .replace(/^\//, "")
77 );
78 const result = new RawSource(
79 `eval(${JSON.stringify(content + footer)});`
80 );
81 cache.set(source, result);
82 return result;
83 }
84 );
85 hooks.inlineInRuntimeBailout.tap(
86 "EvalDevToolModulePlugin",
87 () => "the eval devtool is used."
88 );
89 hooks.render.tap(
90 "EvalDevToolModulePlugin",
91 source => new ConcatSource(devtoolWarning, source)
92 );
93 hooks.chunkHash.tap("EvalDevToolModulePlugin", (chunk, hash) => {
94 hash.update("EvalDevToolModulePlugin");
95 hash.update("2");
96 });
97 });
98 }
99}
100
101module.exports = EvalDevToolModulePlugin;