Merge pull request #11530 from webpack/feature/chunk-filename-function

output.chunkFilename can be a function
This commit is contained in:
Tobias Koppers 2020-09-25 19:47:42 +02:00 committed by GitHub
commit ef9e623559
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 14 deletions

View File

@ -403,9 +403,14 @@ export type AssetModuleFilename =
*/
export type Charset = boolean;
/**
* The filename of non-entry chunks as relative path inside the `output.path` directory.
* The filename of non-initial chunks as relative path inside the `output.path` directory.
*/
export type ChunkFilename = string;
export type ChunkFilename =
| string
| ((
pathData: import("../lib/Compilation").PathData,
assetInfo?: import("../lib/Compilation").AssetInfo
) => string);
/**
* The format of chunks (formats included by default are 'array-push' (web/WebWorker), 'commonjs' (node.js), but others might be added by plugins).
*/
@ -1700,7 +1705,7 @@ export interface Output {
*/
charset?: Charset;
/**
* The filename of non-entry chunks as relative path inside the `output.path` directory.
* The filename of non-initial chunks as relative path inside the `output.path` directory.
*/
chunkFilename?: ChunkFilename;
/**
@ -2371,7 +2376,7 @@ export interface OutputNormalized {
*/
charset?: Charset;
/**
* The filename of non-entry chunks as relative path inside the `output.path` directory.
* The filename of non-initial chunks as relative path inside the `output.path` directory.
*/
chunkFilename?: ChunkFilename;
/**

View File

@ -201,11 +201,13 @@ class RuntimePlugin {
.for(RuntimeGlobals.getChunkScriptFilename)
.tap("RuntimePlugin", (chunk, set) => {
if (
typeof compilation.outputOptions.chunkFilename === "string" &&
/\[(full)?hash(:\d+)?\]/.test(
compilation.outputOptions.chunkFilename
)
)
) {
set.add(RuntimeGlobals.getFullHash);
}
compilation.addRuntimeModule(
chunk,
new GetChunkFilenameRuntimeModule(

View File

@ -74,9 +74,18 @@
"type": "boolean"
},
"ChunkFilename": {
"description": "The filename of non-entry chunks as relative path inside the `output.path` directory.",
"type": "string",
"absolutePath": false
"description": "The filename of non-initial chunks as relative path inside the `output.path` directory.",
"anyOf": [
{
"type": "string",
"absolutePath": false,
"minLength": 1
},
{
"instanceof": "Function",
"tsType": "((pathData: import(\"../lib/Compilation\").PathData, assetInfo?: import(\"../lib/Compilation\").AssetInfo) => string)"
}
]
},
"ChunkFormat": {
"description": "The format of chunks (formats included by default are 'array-push' (web/WebWorker), 'commonjs' (node.js), but others might be added by plugins).",

View File

@ -1987,13 +1987,13 @@ Object {
"output-chunk-filename": Object {
"configs": Array [
Object {
"description": "The filename of non-entry chunks as relative path inside the \`output.path\` directory.",
"description": "The filename of non-initial chunks as relative path inside the \`output.path\` directory.",
"multiple": false,
"path": "output.chunkFilename",
"type": "string",
},
],
"description": "The filename of non-entry chunks as relative path inside the \`output.path\` directory.",
"description": "The filename of non-initial chunks as relative path inside the \`output.path\` directory.",
"multiple": false,
"simpleType": "string",
},

View File

@ -0,0 +1,5 @@
it("should be able to load a chunk", async () => {
await expect(
import(/* webpackChunkName: "1" */ "./chunk1")
).resolves.toMatchObject({ default: 1 });
});

View File

@ -0,0 +1,5 @@
it("should be able to load a chunk", async () => {
await expect(
import(/* webpackChunkName: "2" */ "./chunk2")
).resolves.toMatchObject({ default: 2 });
});

View File

@ -0,0 +1 @@
export default 1;

View File

@ -0,0 +1 @@
export default 2;

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["11.js", "22.js", "aa.js", "bbb.js"];
}
};

View File

@ -0,0 +1,21 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
entry: {
a: "./a",
b: {
import: "./b",
filename: data => {
return data.chunk.name + data.chunk.name + data.chunk.name + ".js";
}
}
},
output: {
filename: data => {
return data.chunk.name + data.chunk.name + ".js";
},
chunkFilename: data => {
return data.chunk.name + data.chunk.name + ".js";
}
}
};

11
types.d.ts vendored
View File

@ -666,6 +666,9 @@ declare class Chunk {
filterFn?: (c: Chunk, chunkGraph: ChunkGraph) => boolean
): Record<string | number, Record<string, (string | number)[]>>;
}
type ChunkFilename =
| string
| ((pathData: PathData, assetInfo: AssetInfo) => string);
declare class ChunkGraph {
constructor(moduleGraph: ModuleGraph);
moduleGraph: ModuleGraph;
@ -5959,9 +5962,9 @@ declare interface Output {
charset?: boolean;
/**
* The filename of non-entry chunks as relative path inside the `output.path` directory.
* The filename of non-initial chunks as relative path inside the `output.path` directory.
*/
chunkFilename?: string;
chunkFilename?: ChunkFilename;
/**
* The format of chunks (formats included by default are 'array-push' (web/WebWorker), 'commonjs' (node.js), but others might be added by plugins).
@ -6208,9 +6211,9 @@ declare interface OutputNormalized {
charset?: boolean;
/**
* The filename of non-entry chunks as relative path inside the `output.path` directory.
* The filename of non-initial chunks as relative path inside the `output.path` directory.
*/
chunkFilename?: string;
chunkFilename?: ChunkFilename;
/**
* The format of chunks (formats included by default are 'array-push' (web/WebWorker), 'commonjs' (node.js), but others might be added by plugins).