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");
|
2018-11-20 19:05:12 +08:00
|
|
|
|
2020-09-15 05:27:38 +08:00
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").PublicPath} PublicPath */
|
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").ScriptType} ScriptType */
|
2018-12-18 05:05:08 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
|
|
|
|
2018-11-20 19:05:12 +08:00
|
|
|
class PublicPathRuntimeModule extends RuntimeModule {
|
2020-09-15 05:27:38 +08:00
|
|
|
/**
|
|
|
|
* @param {PublicPathRuntimeModule} module module
|
|
|
|
* @returns {ReadonlyArray<string> | null} requirements
|
|
|
|
*/
|
|
|
|
static getRuntimeRequirements(module) {
|
|
|
|
if (module.publicPath !== "auto" || module.scriptType === "module")
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return [RuntimeGlobals.global];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {PublicPath} publicPath public path
|
|
|
|
* @param {ScriptType} scriptType script type
|
|
|
|
*/
|
|
|
|
constructor(publicPath, scriptType) {
|
2018-11-20 19:05:12 +08:00
|
|
|
super("publicPath");
|
2020-09-15 05:27:38 +08:00
|
|
|
this.publicPath = publicPath;
|
|
|
|
this.scriptType = scriptType;
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
generate() {
|
2020-09-15 05:27:38 +08:00
|
|
|
const { compilation, publicPath, scriptType } = this;
|
|
|
|
const { runtimeTemplate } = compilation;
|
2020-07-28 11:18:05 +08:00
|
|
|
if (publicPath === "auto") {
|
2020-09-15 05:27:38 +08:00
|
|
|
if (scriptType === "module") {
|
|
|
|
return `${RuntimeGlobals.publicPath} = import.meta.url.replace(/[^\\/]+$/, "")`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${RuntimeGlobals.publicPath} = ${runtimeTemplate.iife(
|
|
|
|
"",
|
|
|
|
Template.indent([
|
|
|
|
`if ("document" in ${RuntimeGlobals.global} && "currentScript" in ${RuntimeGlobals.global}.document) `,
|
|
|
|
Template.indent(
|
|
|
|
`return ${RuntimeGlobals.global}.document.currentScript.src.replace(/[^\\/]+$/, "");`
|
|
|
|
),
|
|
|
|
" else ",
|
|
|
|
Template.indent(`return ${this.definePath("")};`)
|
|
|
|
])
|
|
|
|
)}`;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PublicPath} publicPath public path
|
|
|
|
* @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;
|