source: frontend/node_modules/webpack/lib/runtime/AutoPublicPathRuntimeModule.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7const RuntimeGlobals = require("../RuntimeGlobals");
8const RuntimeModule = require("../RuntimeModule");
9const Template = require("../Template");
10const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
11const { getUndoPath } = require("../util/identifier");
12
13/** @typedef {import("../Chunk")} Chunk */
14/** @typedef {import("../Compilation")} Compilation */
15
16class AutoPublicPathRuntimeModule extends RuntimeModule {
17 constructor() {
18 super("publicPath", RuntimeModule.STAGE_BASIC);
19 }
20
21 /**
22 * Generates runtime code for this runtime module.
23 * @returns {string | null} runtime code
24 */
25 generate() {
26 const compilation = /** @type {Compilation} */ (this.compilation);
27 const { scriptType, importMetaName, path, environment } =
28 compilation.outputOptions;
29 const chunk = /** @type {Chunk} */ (this.chunk);
30 const chunkName = compilation.getPath(
31 JavascriptModulesPlugin.getChunkFilenameTemplate(
32 chunk,
33 compilation.outputOptions
34 ),
35 {
36 chunk,
37 contentHashType: "javascript"
38 }
39 );
40 const undoPath = getUndoPath(
41 chunkName,
42 /** @type {string} */ (path),
43 false
44 );
45
46 const global = environment.globalThis
47 ? "globalThis"
48 : RuntimeGlobals.global;
49
50 return Template.asString([
51 "var scriptUrl;",
52 scriptType === "module"
53 ? `if (typeof ${importMetaName}.url === "string") scriptUrl = ${importMetaName}.url`
54 : Template.asString([
55 `if (${global}.importScripts) scriptUrl = ${global}.location + "";`,
56 `var document = ${global}.document;`,
57 "if (!scriptUrl && document) {",
58 Template.indent([
59 // Technically we could use `document.currentScript instanceof window.HTMLScriptElement`,
60 // but an attacker could try to inject `<script>HTMLScriptElement = HTMLImageElement</script>`
61 // and use `<img name="currentScript" src="https://attacker.controlled.server/"></img>`
62 "if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT')",
63 Template.indent("scriptUrl = document.currentScript.src;"),
64 "if (!scriptUrl) {",
65 Template.indent([
66 'var scripts = document.getElementsByTagName("script");',
67 "if(scripts.length) {",
68 Template.indent([
69 "var i = scripts.length - 1;",
70 "while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;"
71 ]),
72 "}"
73 ]),
74 "}"
75 ]),
76 "}"
77 ]),
78 "// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration",
79 '// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.',
80 'if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");',
81 'scriptUrl = scriptUrl.replace(/^blob:/, "").replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");',
82 !undoPath
83 ? `${RuntimeGlobals.publicPath} = scriptUrl;`
84 : `${RuntimeGlobals.publicPath} = scriptUrl + ${JSON.stringify(
85 undoPath
86 )};`
87 ]);
88 }
89}
90
91module.exports = AutoPublicPathRuntimeModule;
Note: See TracBrowser for help on using the repository browser.