Skip to content

Commit 6ebdc6b

Browse files
committed
Truncate generated chunk name if too long
Fixes webpack#6426
1 parent ec8ece4 commit 6ebdc6b

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

lib/optimize/SplitChunksPlugin.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
*/
55
"use strict";
66

7+
const crypto = require("crypto");
78
const SortableSet = require("../util/SortableSet");
89
const GraphHelpers = require("../GraphHelpers");
910

11+
const hashFilename = (name) => {
12+
return crypto.createHash("md5").update(name).digest("hex").slice(0, 8);
13+
};
14+
1015
const sortByIdentifier = (a, b) => {
1116
if(a.identifier() > b.identifier()) return 1;
1217
if(a.identifier() < b.identifier()) return -1;
@@ -85,7 +90,15 @@ module.exports = class SplitChunksPlugin {
8590
const names = chunks.map(c => c.name);
8691
if(!names.every(Boolean)) return;
8792
names.sort();
88-
const name = (cacheGroup && cacheGroup !== "default" ? cacheGroup + "~" : "") + names.join("~");
93+
let name = (cacheGroup && cacheGroup !== "default" ? cacheGroup + "~" : "") + names.join("~");
94+
// Filenames and paths can't be too long otherwise an
95+
// ENAMETOOLONG error is raised. If the generated name if too
96+
// long, it is truncated and a hash is appended. The limit has
97+
// been set to 100 to prevent `[name].[chunkhash].[ext]` from
98+
// generating a 256+ character string.
99+
if(name.length > 100) {
100+
name = name.slice(0, 100) + "~" + hashFilename(name);
101+
}
89102
return name;
90103
};
91104
return fn;

test/statsCases/split-chunks/expected.txt

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,73 @@ Child manual:
191191
> ./c c
192192
[0] ./d.js 20 bytes {2} {3} {4} {6} {7} {8} [built]
193193
[1] ./f.js 20 bytes {1} {3} {4} {7} {8} [built]
194-
[5] ./c.js 72 bytes {4} {8} [built]
194+
[5] ./c.js 72 bytes {4} {8} [built]
195+
Child name-too-long:
196+
Entrypoint main = main.js
197+
Entrypoint aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be.js aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.js aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.js
198+
Entrypoint bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be.js aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.js bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.js
199+
Entrypoint cccccccccccccccccccccccccccccc = vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be.js vendors~async-c~cccccccccccccccccccccccccccccc.js cccccccccccccccccccccccccccccc.js
200+
chunk {0} vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be.js (vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be) 20 bytes <{9}> ={4}= ={12}= ={2}= ={11}= ={10}= ={1}= ={3}= ={8}= ={7}= ={6}= >{1}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccc~716093be)
201+
> ./c cccccccccccccccccccccccccccccc
202+
> ./b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
203+
> ./a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
204+
> ./c [8] ./index.js 3:0-47
205+
> ./b [8] ./index.js 2:0-47
206+
> ./a [8] ./index.js 1:0-47
207+
[1] ./node_modules/x.js 20 bytes {0} {3} [built]
208+
chunk {1} async-b~async-c~async-g~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccccccccccccccccccc.js (async-b~async-c~async-g~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccccccccccccccccccc) 20 bytes <{0}> <{2}> <{10}> <{3}> <{6}> <{9}> ={5}= ={0}= ={4}= ={3}= ={8}= ={2}= ={7}= [rendered] split chunk (cache group: default) (name: async-b~async-c~async-g~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccccccccccccccccccc)
209+
> ./g [] 6:0-47
210+
> ./g [] 6:0-47
211+
> ./c [8] ./index.js 3:0-47
212+
> ./b [8] ./index.js 2:0-47
213+
[2] ./f.js 20 bytes {1} {11} {12} [built]
214+
chunk {2} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.js (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) 20 bytes <{9}> ={0}= ={11}= ={10}= ={1}= ={7}= ={3}= ={6}= >{1}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
215+
> ./b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
216+
> ./a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
217+
> ./b [8] ./index.js 2:0-47
218+
> ./a [8] ./index.js 1:0-47
219+
[3] ./node_modules/y.js 20 bytes {2} [built]
220+
chunk {3} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccc~e7d53185.js (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccc~e7d53185) 40 bytes <{9}> ={0}= ={4}= ={1}= ={8}= ={2}= ={6}= >{1}< >{5}< [rendered] split chunk (cache group: default) (name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa~async-a~async-b~async-c~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb~cccccccccccccc~e7d53185)
221+
> ./c [8] ./index.js 3:0-47
222+
> ./a [8] ./index.js 1:0-47
223+
[0] ./d.js 20 bytes {3} {7} {10} {11} {12} [built]
224+
[1] ./node_modules/x.js 20 bytes {0} {3} [built]
225+
chunk {4} vendors~async-c~cccccccccccccccccccccccccccccc.js (vendors~async-c~cccccccccccccccccccccccccccccc) 20 bytes <{9}> ={0}= ={12}= ={1}= ={3}= ={8}= [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-c~cccccccccccccccccccccccccccccc)
226+
> ./c cccccccccccccccccccccccccccccc
227+
> ./c [8] ./index.js 3:0-47
228+
[7] ./node_modules/z.js 20 bytes {4} [built]
229+
chunk {5} async-g.js (async-g) 34 bytes <{0}> <{2}> <{10}> <{3}> <{6}> ={1}= [rendered]
230+
> ./g [] 6:0-47
231+
> ./g [] 6:0-47
232+
[9] ./g.js 34 bytes {5} [built]
233+
chunk {6} async-a.js (async-a) 156 bytes <{9}> ={0}= ={2}= ={3}= >{1}< >{5}< [rendered]
234+
> ./a [8] ./index.js 1:0-47
235+
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
236+
| ./a.js 121 bytes [built]
237+
| ./e.js 20 bytes [built]
238+
chunk {7} async-b.js (async-b) 92 bytes <{9}> ={0}= ={2}= ={1}= [rendered]
239+
> ./b [8] ./index.js 2:0-47
240+
[0] ./d.js 20 bytes {3} {7} {10} {11} {12} [built]
241+
[4] ./b.js 72 bytes {7} {11} [built]
242+
chunk {8} async-c.js (async-c) 72 bytes <{9}> ={0}= ={4}= ={1}= ={3}= [rendered]
243+
> ./c [8] ./index.js 3:0-47
244+
[5] ./c.js 72 bytes {8} {12} [built]
245+
chunk {9} main.js (main) 147 bytes >{0}< >{2}< >{1}< >{7}< >{3}< >{6}< >{4}< >{8}< [entry] [rendered]
246+
> ./ main
247+
[8] ./index.js 147 bytes {9} [built]
248+
chunk {10} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.js (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) 176 bytes ={0}= ={2}= >{1}< >{5}< [entry] [rendered]
249+
> ./a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
250+
[0] ./d.js 20 bytes {3} {7} {10} {11} {12} [built]
251+
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
252+
| ./a.js 121 bytes [built]
253+
| ./e.js 20 bytes [built]
254+
chunk {11} bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.js (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) 112 bytes ={0}= ={2}= [entry] [rendered]
255+
> ./b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
256+
[0] ./d.js 20 bytes {3} {7} {10} {11} {12} [built]
257+
[2] ./f.js 20 bytes {1} {11} {12} [built]
258+
[4] ./b.js 72 bytes {7} {11} [built]
259+
chunk {12} cccccccccccccccccccccccccccccc.js (cccccccccccccccccccccccccccccc) 112 bytes ={0}= ={4}= [entry] [rendered]
260+
> ./c cccccccccccccccccccccccccccccc
261+
[0] ./d.js 20 bytes {3} {7} {10} {11} {12} [built]
262+
[2] ./f.js 20 bytes {1} {11} {12} [built]
263+
[5] ./c.js 72 bytes {8} {12} [built]

test/statsCases/split-chunks/webpack.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ module.exports = [
7878
}
7979
},
8080
stats
81+
},
82+
{
83+
name: "name-too-long",
84+
mode: "production",
85+
entry: {
86+
main: "./",
87+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "./a",
88+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: "./b",
89+
cccccccccccccccccccccccccccccc: "./c"
90+
},
91+
output: {
92+
filename: "[name].js"
93+
},
94+
optimization: {
95+
splitChunks: {
96+
minSize: 0,
97+
chunks: "all"
98+
}
99+
},
100+
stats
81101
}
82102

83103
];

0 commit comments

Comments
 (0)