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. * 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; 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. * 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. * Include comments with information about the modules.
*/ */
export type Pathinfo = "verbose" | boolean; 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". * 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. * Options for library.
*/ */
library?: LibraryOptions; 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. * 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. * Options for library.
*/ */
library?: LibraryOptions; 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. * 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, runtime: desc.runtime,
layer: desc.layer, layer: desc.layer,
dependOn: desc.dependOn, dependOn: desc.dependOn,
publicPath: desc.publicPath,
chunkLoading: desc.chunkLoading, chunkLoading: desc.chunkLoading,
wasmLoading: desc.wasmLoading, wasmLoading: desc.wasmLoading,
library: desc.library library: desc.library

View File

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

View File

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

View File

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

View File

@ -470,6 +470,9 @@
"library": { "library": {
"$ref": "#/definitions/LibraryOptions" "$ref": "#/definitions/LibraryOptions"
}, },
"publicPath": {
"$ref": "#/definitions/PublicPath"
},
"runtime": { "runtime": {
"$ref": "#/definitions/EntryRuntime" "$ref": "#/definitions/EntryRuntime"
}, },
@ -518,6 +521,9 @@
"library": { "library": {
"$ref": "#/definitions/LibraryOptions" "$ref": "#/definitions/LibraryOptions"
}, },
"publicPath": {
"$ref": "#/definitions/PublicPath"
},
"runtime": { "runtime": {
"$ref": "#/definitions/EntryRuntime" "$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() { entry() {
return { return {
a: "./a", a: "./a",
b: "./b" b: "./b",
c: {
import: "./c",
publicPath: "/other/"
},
d: {
import: "./d",
publicPath: "/other/"
}
}; };
}, },
output: { output: {
filename: data => { 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]" assetModuleFilename: "[name][ext]"
}, },

10
types.d.ts vendored
View File

@ -2926,6 +2926,11 @@ declare interface EntryDescription {
*/ */
library?: LibraryOptions; 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. * 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; 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. * The name of the runtime chunk. If set a runtime chunk with this name is created or an existing entrypoint is used as runtime.
*/ */