split auto publicPath into separate module
This commit is contained in:
parent
b5719c57e2
commit
aa74705297
|
@ -8,6 +8,7 @@
|
||||||
const RuntimeGlobals = require("./RuntimeGlobals");
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
||||||
const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
|
const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
|
||||||
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
||||||
|
const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
|
||||||
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
|
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
|
||||||
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
|
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
|
||||||
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
|
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
|
||||||
|
@ -157,24 +158,25 @@ class RuntimePlugin {
|
||||||
});
|
});
|
||||||
compilation.hooks.runtimeRequirementInTree
|
compilation.hooks.runtimeRequirementInTree
|
||||||
.for(RuntimeGlobals.publicPath)
|
.for(RuntimeGlobals.publicPath)
|
||||||
.tap("RuntimePlugin", (chunk, runtimeRequirements) => {
|
.tap("RuntimePlugin", (chunk, set) => {
|
||||||
const { outputOptions } = compilation;
|
const { outputOptions } = compilation;
|
||||||
const { publicPath } = outputOptions;
|
const { publicPath, scriptType } = outputOptions;
|
||||||
const module = new PublicPathRuntimeModule();
|
|
||||||
|
|
||||||
if (
|
if (publicPath === "auto") {
|
||||||
typeof publicPath !== "string" ||
|
const module = new AutoPublicPathRuntimeModule();
|
||||||
/\[(full)?hash\]/.test(publicPath)
|
if (scriptType !== "module") set.add(RuntimeGlobals.global);
|
||||||
) {
|
compilation.addRuntimeModule(chunk, module);
|
||||||
module.fullHash = true;
|
} else {
|
||||||
}
|
const module = new PublicPathRuntimeModule();
|
||||||
|
|
||||||
compilation.addRuntimeModule(chunk, module);
|
if (
|
||||||
const moduleRuntimeRequirements = module.getRuntimeRequirements();
|
typeof publicPath !== "string" ||
|
||||||
|
/\[(full)?hash\]/.test(publicPath)
|
||||||
|
) {
|
||||||
|
module.fullHash = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (moduleRuntimeRequirements) {
|
compilation.addRuntimeModule(chunk, module);
|
||||||
for (const req of moduleRuntimeRequirements)
|
|
||||||
runtimeRequirements.add(req);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
||||||
|
const RuntimeModule = require("../RuntimeModule");
|
||||||
|
const Template = require("../Template");
|
||||||
|
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
|
||||||
|
const { getUndoPath } = require("../util/identifier");
|
||||||
|
|
||||||
|
class AutoPublicPathRuntimeModule extends RuntimeModule {
|
||||||
|
constructor() {
|
||||||
|
super("publicPath", 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {string} runtime code
|
||||||
|
*/
|
||||||
|
generate() {
|
||||||
|
const { compilation } = this;
|
||||||
|
const { scriptType, importFunctionName } = compilation.outputOptions;
|
||||||
|
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
|
||||||
|
)};`
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = AutoPublicPathRuntimeModule;
|
|
@ -6,27 +6,8 @@
|
||||||
|
|
||||||
const RuntimeGlobals = require("../RuntimeGlobals");
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
||||||
const RuntimeModule = require("../RuntimeModule");
|
const RuntimeModule = require("../RuntimeModule");
|
||||||
const Template = require("../Template");
|
|
||||||
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
|
|
||||||
const { getUndoPath } = require("../util/identifier");
|
|
||||||
|
|
||||||
/** @typedef {import("../../declarations/WebpackOptions").Output} OutputOptions */
|
|
||||||
/** @typedef {import("../../declarations/WebpackOptions").PublicPath} PublicPathOptions */
|
|
||||||
/** @typedef {import("../Compilation")} Compilation */
|
|
||||||
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
|
||||||
|
|
||||||
class PublicPathRuntimeModule extends RuntimeModule {
|
class PublicPathRuntimeModule extends RuntimeModule {
|
||||||
/**
|
|
||||||
* @returns {ReadonlyArray<string> | null} requirements
|
|
||||||
*/
|
|
||||||
getRuntimeRequirements() {
|
|
||||||
const { compilation } = this;
|
|
||||||
const { publicPath, scriptType } = compilation.outputOptions;
|
|
||||||
if (publicPath !== "auto" || scriptType === "module") return null;
|
|
||||||
|
|
||||||
return [RuntimeGlobals.global];
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super("publicPath", 5);
|
super("publicPath", 5);
|
||||||
}
|
}
|
||||||
|
@ -36,67 +17,13 @@ class PublicPathRuntimeModule extends RuntimeModule {
|
||||||
*/
|
*/
|
||||||
generate() {
|
generate() {
|
||||||
const { compilation } = this;
|
const { compilation } = this;
|
||||||
const {
|
const { publicPath } = compilation.outputOptions;
|
||||||
publicPath,
|
|
||||||
scriptType,
|
|
||||||
importFunctionName
|
|
||||||
} = compilation.outputOptions;
|
|
||||||
if (publicPath === "auto") {
|
|
||||||
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
|
|
||||||
)};`
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
return `${RuntimeGlobals.publicPath} = ${this.definePath(publicPath)};`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return `${RuntimeGlobals.publicPath} = ${JSON.stringify(
|
||||||
* @param {PublicPathOptions} publicPath public path
|
|
||||||
* @returns {string} runtime code
|
|
||||||
*/
|
|
||||||
definePath(publicPath) {
|
|
||||||
return JSON.stringify(
|
|
||||||
this.compilation.getPath(publicPath || "", {
|
this.compilation.getPath(publicPath || "", {
|
||||||
hash: this.compilation.hash || "XXXX"
|
hash: this.compilation.hash || "XXXX"
|
||||||
})
|
})
|
||||||
);
|
)};`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue