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
|
|
|
/**
|
|
|
|
* @param {PublicPathRuntimeModule} module module
|
|
|
|
* @returns {ReadonlyArray<string> | null} requirements
|
|
|
|
*/
|
|
|
|
static getRuntimeRequirements(module) {
|
|
|
|
if (module.publicPath !== "auto" || module.scriptType === "module")
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return [RuntimeGlobals.global];
|
|
|
|
}
|
|
|
|
/**
|
2020-09-17 04:07:06 +08:00
|
|
|
* @param {OutputOptions} outputOptions output options
|
2020-09-15 05:27:38 +08:00
|
|
|
*/
|
2020-09-18 15:10:10 +08:00
|
|
|
constructor(outputOptions) {
|
2018-11-20 19:05:12 +08:00
|
|
|
super("publicPath");
|
2020-09-17 04:07:06 +08:00
|
|
|
const { publicPath, scriptType, importFunctionName } = outputOptions;
|
2020-09-15 05:27:38 +08:00
|
|
|
this.publicPath = publicPath;
|
|
|
|
this.scriptType = scriptType;
|
2020-09-17 04:07:06 +08:00
|
|
|
this.importName = importFunctionName;
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
generate() {
|
2020-09-16 02:03:05 +08:00
|
|
|
const { compilation, importName, publicPath, scriptType } = this;
|
2020-09-15 05:27:38 +08:00
|
|
|
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") {
|
2020-09-17 04:07:06 +08:00
|
|
|
return `${RuntimeGlobals.publicPath} = ${this.applyUndoPath(
|
|
|
|
`${importName}.meta.url.replace(/[^\\/]+$/, "")`,
|
|
|
|
runtimeTemplate
|
|
|
|
)};`;
|
2020-09-15 05:27:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${RuntimeGlobals.publicPath} = ${runtimeTemplate.iife(
|
|
|
|
"",
|
|
|
|
Template.indent([
|
|
|
|
`if ("document" in ${RuntimeGlobals.global} && "currentScript" in ${RuntimeGlobals.global}.document) `,
|
|
|
|
Template.indent(
|
2020-09-17 04:07:06 +08:00
|
|
|
`return ${this.applyUndoPath(
|
|
|
|
`${RuntimeGlobals.global}.document.currentScript.src.replace(/[^\\/]+$/, "")`,
|
|
|
|
runtimeTemplate
|
|
|
|
)};`
|
2020-09-15 05:27:38 +08:00
|
|
|
),
|
|
|
|
" 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
|
|
|
|
|
|
|
/**
|
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"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-09-17 04:07:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} code code
|
|
|
|
* @param {RuntimeTemplate} runtimeTemplate runtime template
|
|
|
|
* @returns {string} generated code
|
|
|
|
*/
|
|
|
|
applyUndoPath(code, runtimeTemplate) {
|
2020-09-18 15:10:10 +08:00
|
|
|
const chunkName = this.compilation.getPath(
|
|
|
|
JavascriptModulesPlugin.getChunkFilenameTemplate(
|
|
|
|
this.chunk,
|
|
|
|
this.compilation.outputOptions
|
|
|
|
),
|
|
|
|
{
|
|
|
|
chunk: this.chunk,
|
|
|
|
contentHashType: "javascript"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const undoPath = getUndoPath(chunkName, false);
|
|
|
|
|
2020-09-17 04:07:06 +08:00
|
|
|
if (!undoPath) return code;
|
|
|
|
|
|
|
|
if (runtimeTemplate.supportTemplateLiteral()) {
|
2020-09-18 15:10:10 +08:00
|
|
|
return `\`$\{${code}}$\{${JSON.stringify(undoPath)}}\``;
|
2020-09-17 04:07:06 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:10:10 +08:00
|
|
|
return `${code} + ${JSON.stringify(undoPath)}`;
|
2020-09-17 04:07:06 +08:00
|
|
|
}
|
2018-11-20 19:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PublicPathRuntimeModule;
|