Skip to content

Commit 8988dc8

Browse files
authored
Merge pull request webpack#6144 from jaketodaro/templated-string-functions
support functions for templated string config properties
2 parents b500de2 + fb40704 commit 8988dc8

File tree

10 files changed

+103
-9
lines changed

10 files changed

+103
-9
lines changed

lib/TemplatedPathPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const replacePathVariables = (path, data) => {
6060
const chunkHash = chunk && (chunk.renderedHash || chunk.hash);
6161
const chunkHashWithLength = chunk && chunk.hashWithLength;
6262

63+
if(typeof path === "function") {
64+
path = path(data);
65+
}
66+
6367
if(data.noChunkHash && REGEXP_CHUNKHASH_FOR_TEST.test(path)) {
6468
throw new Error(`Cannot use [chunkhash] for chunk in '${path}' (use [hash] instead)`);
6569
}

schemas/webpackOptionsSchema.json

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,14 @@
296296
},
297297
"chunkFilename": {
298298
"description": "The filename of non-entry chunks as relative path inside the `output.path` directory.",
299-
"type": "string",
299+
"anyOf": [
300+
{
301+
"type": "string"
302+
},
303+
{
304+
"instanceof": "Function"
305+
}
306+
],
300307
"absolutePath": false
301308
},
302309
"crossOriginLoading": {
@@ -348,7 +355,14 @@
348355
},
349356
"filename": {
350357
"description": "Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
351-
"type": "string",
358+
"anyOf": [
359+
{
360+
"type": "string"
361+
},
362+
{
363+
"instanceof": "Function"
364+
}
365+
],
352366
"absolutePath": false
353367
},
354368
"hashDigest": {
@@ -376,7 +390,14 @@
376390
},
377391
"hotUpdateChunkFilename": {
378392
"description": "The filename of the Hot Update Chunks. They are inside the output.path directory.",
379-
"type": "string",
393+
"anyOf": [
394+
{
395+
"type": "string"
396+
},
397+
{
398+
"instanceof": "Function"
399+
}
400+
],
380401
"absolutePath": false
381402
},
382403
"hotUpdateFunction": {
@@ -385,7 +406,14 @@
385406
},
386407
"hotUpdateMainFilename": {
387408
"description": "The filename of the Hot Update Main File. It is inside the `output.path` directory.",
388-
"type": "string",
409+
"anyOf": [
410+
{
411+
"type": "string"
412+
},
413+
{
414+
"instanceof": "Function"
415+
}
416+
],
389417
"absolutePath": false
390418
},
391419
"jsonpFunction": {
@@ -464,7 +492,14 @@
464492
},
465493
"publicPath": {
466494
"description": "The `publicPath` specifies the public URL address of the output files when referenced in a browser.",
467-
"type": "string"
495+
"anyOf": [
496+
{
497+
"type": "string"
498+
},
499+
{
500+
"instanceof": "Function"
501+
}
502+
]
468503
},
469504
"sourceMapFilename": {
470505
"description": "The filename of the SourceMaps for the JavaScript files. They are inside the `output.path` directory.",

test/Validation.test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ describe("Validation", () => {
128128
" -> A non-empty string",
129129
" * configuration.entry should be an instance of function",
130130
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things.",
131-
" - configuration.output.filename should be a string.",
132-
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files."
131+
" - configuration.output.filename should be one of these:",
132+
" string | function",
133+
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
134+
" Details:",
135+
" * configuration.output.filename should be a string.",
136+
" * configuration.output.filename should be an instance of function"
133137
]
134138
}, {
135139
name: "multiple configurations",
@@ -154,8 +158,12 @@ describe("Validation", () => {
154158
" -> A non-empty string",
155159
" * configuration[0].entry should be an instance of function",
156160
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things.",
157-
" - configuration[1].output.filename should be a string.",
158-
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files."
161+
" - configuration[1].output.filename should be one of these:",
162+
" string | function",
163+
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
164+
" Details:",
165+
" * configuration[1].output.filename should be a string.",
166+
" * configuration[1].output.filename should be an instance of function",
159167
]
160168
}, {
161169
name: "deep error",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should output correctly with a function for output.filename", (done) => {
2+
done();
3+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should output correctly with a function for output.filename", (done) => {
2+
done();
3+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
findBundle: function() {
3+
return [
4+
"./a.js",
5+
"./b.js"
6+
];
7+
}
8+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
entry() {
3+
return {
4+
a: "./a",
5+
b: "./b"
6+
};
7+
},
8+
output: {
9+
filename: (data) => {
10+
return data.chunk.name === "a" ? `${data.chunk.name}.js` : "[name].js";
11+
}
12+
}
13+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should output correctly with a string for output.filename", (done) => {
2+
done();
3+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
findBundle: function() {
3+
return [
4+
"./a.js"
5+
];
6+
}
7+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
entry() {
3+
return {
4+
a: "./a"
5+
};
6+
},
7+
output: {
8+
filename: "[name].js"
9+
}
10+
};

0 commit comments

Comments
 (0)