2018-11-20 19:05:12 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RuntimeModule = require("../RuntimeModule");
|
2020-09-15 05:27:38 +08:00
|
|
|
const Template = require("../Template");
|
2020-09-18 15:10:10 +08:00
|
|
|
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
|
|
|
|
const { getUndoPath } = require("../util/identifier");
|
2018-11-20 19:05:12 +08:00
|
|
|
|
2020-09-17 04:07:06 +08:00
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").Output} OutputOptions */
|
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").PublicPath} PublicPathOptions */
|
2018-12-18 05:05:08 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
2020-09-17 04:07:06 +08:00
|
|
|
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
2018-12-18 05:05:08 +08:00
|
|
|
|
2018-11-20 19:05:12 +08:00
|
|
|
class PublicPathRuntimeModule extends RuntimeModule {
|
2020-09-15 05:27:38 +08:00
|
|
|
/**
|
|
|
|
* @returns {ReadonlyArray<string> | null} requirements
|
|
|
|
*/
|
2020-09-18 15:48:53 +08:00
|
|
|
getRuntimeRequirements() {
|
|
|
|
const { compilation } = this;
|
|
|
|
const { publicPath, scriptType } = compilation.outputOptions;
|
|
|
|
if (publicPath !== "auto" || scriptType === "module") return null;
|
2020-09-15 05:27:38 +08:00
|
|
|
|
|
|
|
return [RuntimeGlobals.global];
|
|
|
|
}
|
2020-09-18 15:48:53 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super("publicPath", 5);
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
generate() {
|
2020-09-18 15:48:53 +08:00
|
|
|
const { compilation } = this;
|
|
|
|
const {
|
|
|
|
publicPath,
|
|
|
|
scriptType,
|
|
|
|
importFunctionName
|
|
|
|
} = compilation.outputOptions;
|
2020-07-28 11:18:05 +08:00
|
|
|
if (publicPath === "auto") {
|
2020-09-18 15:48:53 +08:00
|
|
|
const chunkName = compilation.getPath(
|
|
|
|
JavascriptModulesPlugin.getChunkFilenameTemplate(
|
|
|
|
this.chunk,
|
|
|
|
compilation.outputOptions
|
|
|
|
),
|
|
|
|
{
|
|
|
|
chunk: this.chunk,
|
|
|
|
contentHashType: "javascript"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const undoPath = getUndoPath(chunkName, false);
|
|
|
|
return Template.asString([
|
|
|
|
"var scriptUrl;",
|
|
|
|
scriptType === "module"
|
|
|
|
? `if (typeof ${importFunctionName}.meta.url === "string") scriptUrl = ${importFunctionName}.meta.url`
|
|
|
|
: Template.asString([
|
|
|
|
`var document = ${RuntimeGlobals.global}.document;`,
|
|
|
|
"if (document) {",
|
|
|
|
Template.indent([
|
|
|
|
`if (document.currentScript)`,
|
|
|
|
Template.indent(`scriptUrl = document.currentScript.src`),
|
|
|
|
"if (!scriptUrl) {",
|
|
|
|
Template.indent([
|
|
|
|
'var scripts = document.getElementsByTagName("script");',
|
|
|
|
"if(scripts.length) scriptUrl = scripts[scripts.length - 1].src"
|
|
|
|
]),
|
|
|
|
"}"
|
|
|
|
]),
|
|
|
|
"}"
|
|
|
|
]),
|
|
|
|
"// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration",
|
|
|
|
'// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.',
|
|
|
|
'if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");',
|
|
|
|
'scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");',
|
|
|
|
!undoPath
|
|
|
|
? `${RuntimeGlobals.publicPath} = scriptUrl;`
|
|
|
|
: `${RuntimeGlobals.publicPath} = scriptUrl + ${JSON.stringify(
|
|
|
|
undoPath
|
|
|
|
)};`
|
|
|
|
]);
|
2020-07-28 11:18:05 +08:00
|
|
|
} else {
|
2020-09-15 05:27:38 +08:00
|
|
|
return `${RuntimeGlobals.publicPath} = ${this.definePath(publicPath)};`;
|
2020-07-28 11:18:05 +08:00
|
|
|
}
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
2020-09-15 05:27:38 +08:00
|
|
|
|
|
|
|
/**
|
2020-09-17 04:07:06 +08:00
|
|
|
* @param {PublicPathOptions} publicPath public path
|
2020-09-15 05:27:38 +08:00
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
definePath(publicPath) {
|
|
|
|
return JSON.stringify(
|
|
|
|
this.compilation.getPath(publicPath || "", {
|
|
|
|
hash: this.compilation.hash || "XXXX"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PublicPathRuntimeModule;
|