allow to specify `publicPath` for each entrypoint

This commit is contained in:
Tobias Koppers 2021-04-14 16:38:01 +02:00
parent 31353e36c5
commit 38de0dbf4e
10 changed files with 71 additions and 22 deletions

View File

@ -122,6 +122,19 @@ export type LibraryType =
* If `output.libraryTarget` is set to umd and `output.library` is set, setting this to true will name the AMD module.
*/
export type UmdNamedDefine = boolean;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
export type PublicPath = "auto" | RawPublicPath;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
export type RawPublicPath =
| string
| ((
pathData: import("../lib/Compilation").PathData,
assetInfo?: import("../lib/Compilation").AssetInfo
) => string);
/**
* The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/
@ -537,19 +550,6 @@ export type Path = string;
* Include comments with information about the modules.
*/
export type Pathinfo = "verbose" | boolean;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
export type PublicPath = "auto" | RawPublicPath;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
export type RawPublicPath =
| string
| ((
pathData: import("../lib/Compilation").PathData,
assetInfo?: import("../lib/Compilation").AssetInfo
) => string);
/**
* This option enables loading async chunks via a custom script type, such as script type="module".
*/
@ -1021,6 +1021,10 @@ export interface EntryDescription {
* Options for library.
*/
library?: LibraryOptions;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: PublicPath;
/**
* The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/
@ -2672,6 +2676,10 @@ export interface EntryDescriptionNormalized {
* Options for library.
*/
library?: LibraryOptions;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: PublicPath;
/**
* The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/

View File

@ -62,6 +62,7 @@ class EntryOptionPlugin {
runtime: desc.runtime,
layer: desc.layer,
dependOn: desc.dependOn,
publicPath: desc.publicPath,
chunkLoading: desc.chunkLoading,
wasmLoading: desc.wasmLoading,
library: desc.library

View File

@ -176,14 +176,17 @@ class RuntimePlugin {
.for(RuntimeGlobals.publicPath)
.tap("RuntimePlugin", (chunk, set) => {
const { outputOptions } = compilation;
const { publicPath, scriptType } = outputOptions;
const { publicPath: globalPublicPath, scriptType } = outputOptions;
const entryOptions = chunk.getEntryOptions();
const publicPath =
(entryOptions && entryOptions.publicPath) || globalPublicPath;
if (publicPath === "auto") {
const module = new AutoPublicPathRuntimeModule();
if (scriptType !== "module") set.add(RuntimeGlobals.global);
compilation.addRuntimeModule(chunk, module);
} else {
const module = new PublicPathRuntimeModule();
const module = new PublicPathRuntimeModule(publicPath);
if (
typeof publicPath !== "string" ||

View File

@ -458,6 +458,7 @@ const getNormalizedEntryStatic = entry => {
filename: value.filename,
layer: value.layer,
runtime: value.runtime,
publicPath: value.publicPath,
chunkLoading: value.chunkLoading,
wasmLoading: value.wasmLoading,
dependOn:

View File

@ -8,20 +8,20 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
class PublicPathRuntimeModule extends RuntimeModule {
constructor() {
constructor(publicPath) {
super("publicPath", RuntimeModule.STAGE_BASIC);
this.publicPath = publicPath;
}
/**
* @returns {string} runtime code
*/
generate() {
const { compilation } = this;
const { publicPath } = compilation.outputOptions;
const { compilation, publicPath } = this;
return `${RuntimeGlobals.publicPath} = ${JSON.stringify(
this.compilation.getPath(publicPath || "", {
hash: this.compilation.hash || "XXXX"
compilation.getPath(publicPath || "", {
hash: compilation.hash || "XXXX"
})
)};`;
}

View File

@ -470,6 +470,9 @@
"library": {
"$ref": "#/definitions/LibraryOptions"
},
"publicPath": {
"$ref": "#/definitions/PublicPath"
},
"runtime": {
"$ref": "#/definitions/EntryRuntime"
},
@ -518,6 +521,9 @@
"library": {
"$ref": "#/definitions/LibraryOptions"
},
"publicPath": {
"$ref": "#/definitions/PublicPath"
},
"runtime": {
"$ref": "#/definitions/EntryRuntime"
},

View File

@ -0,0 +1,5 @@
import asset from "./asset.jpg";
it("should define public path", () => {
expect(asset).toBe("/other/inner1/inner2/../../asset.jpg");
});

View File

@ -0,0 +1,5 @@
import asset from "./asset.jpg";
it("should define public path", () => {
expect(asset).toBe("/other/asset.jpg");
});

View File

@ -5,12 +5,22 @@ module.exports = {
entry() {
return {
a: "./a",
b: "./b"
b: "./b",
c: {
import: "./c",
publicPath: "/other/"
},
d: {
import: "./d",
publicPath: "/other/"
}
};
},
output: {
filename: data => {
return data.chunk.name === "a" ? `inner1/inner2/[name].js` : "[name].js";
return /^[ac]$/.test(data.chunk.name)
? `inner1/inner2/[name].js`
: "[name].js";
},
assetModuleFilename: "[name][ext]"
},

10
types.d.ts vendored
View File

@ -2926,6 +2926,11 @@ declare interface EntryDescription {
*/
library?: LibraryOptions;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
/**
* The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/
@ -2971,6 +2976,11 @@ declare interface EntryDescriptionNormalized {
*/
library?: LibraryOptions;
/**
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
/**
* The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/