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