forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetChunkFilenameRuntimeModule.js
173 lines (167 loc) · 4.79 KB
/
GetChunkFilenameRuntimeModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");
const createChunkNameExpr = obj => {
const optimizedObj = {};
let hasEntries = false;
for (const key of Object.keys(obj)) {
const value = obj[key];
if (key !== value) {
optimizedObj[key] = value;
hasEntries = true;
}
}
return hasEntries
? `(${JSON.stringify(optimizedObj)}[chunkId]||chunkId)`
: "chunkId";
};
class GetChunkFilenameRuntimeModule extends RuntimeModule {
constructor(
compilation,
chunk,
contentType,
name,
global,
filename,
getFilenameForChunk
) {
super(`get ${name} chunk filename`);
this.compilation = compilation;
this.chunk = chunk;
this.contentType = contentType;
this.global = global;
this.filename = filename;
this.getFilenameForChunk = getFilenameForChunk;
}
/**
* @returns {string} runtime code
*/
generate() {
const {
global,
chunk,
contentType,
filename,
getFilenameForChunk,
compilation
} = this;
const mainTemplate = compilation.mainTemplate;
const chunkMaps = chunk.getChunkMaps();
const staticUrls = new Map();
const includeEntries = compilation.chunkGraph
.getTreeRuntimeRequirements(chunk)
.has(RuntimeGlobals.ensureChunkIncludeEntries);
if (includeEntries) {
for (const c of compilation.chunkGraph.getChunkEntryDependentChunksIterable(
chunk
)) {
const chunkFilename = getFilenameForChunk(c) || filename;
const staticChunkFilename = mainTemplate.getAssetPath(
JSON.stringify(chunkFilename),
{
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
chunk: c,
contentHashType: contentType
}
);
// TODO optimize this to not include static filenames when it could be generated by the templated expression
staticUrls.set(c.id, staticChunkFilename);
}
}
const url = mainTemplate.getAssetPath(JSON.stringify(filename), {
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
hashWithLength: length =>
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
chunk: {
id: `" + chunkId + "`,
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
hashWithLength(length) {
const shortChunkHashMap = Object.create(null);
for (const chunkId of Object.keys(chunkMaps.hash)) {
if (typeof chunkMaps.hash[chunkId] === "string") {
shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
0,
length
);
}
}
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
},
name: `" + ${createChunkNameExpr(chunkMaps.name)} + "`,
contentHash: {
[contentType]: `" + ${JSON.stringify(
chunkMaps.contentHash[contentType]
)}[chunkId] + "`
},
contentHashWithLength: {
[contentType]: length => {
const shortContentHashMap = {};
const contentHash = chunkMaps.contentHash[contentType];
if (!contentHash) return "XXXX";
for (const chunkId of Object.keys(contentHash)) {
if (typeof contentHash[chunkId] === "string") {
shortContentHashMap[chunkId] = contentHash[chunkId].substr(
0,
length
);
}
}
return `" + ${JSON.stringify(shortContentHashMap)}[chunkId] + "`;
}
}
},
contentHashType: contentType
});
return Template.asString([
staticUrls.size > 16
? Template.asString([
"// mapping for filenames not based on template",
`var staticMapping = ${JSON.stringify(
Array.from(staticUrls).reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{}
),
null,
"\t"
)};`
])
: "",
includeEntries
? "// This function also allow to reference entries"
: "// This function only allows to reference on-demand chunks",
`${global} = function(chunkId) {`,
Template.indent(
staticUrls.size > 16
? [
// object is shorter for 16 or more cases
"// return url for filenames either based on template or not",
`return Object.prototype.hasOwnProperty.call(staticMapping, chunkId) ? staticMapping[chunkId] : ${url};`
]
: staticUrls.size > 0
? [
// if is shorter for 1 - 15 cases
// it minimizes to `x===1?"...":x===2?"...":"..."`
"// return url for filenames not based on template",
Template.asString(
Array.from(staticUrls).map(
([id, url]) =>
`if(chunkId === ${JSON.stringify(id)}) return ${url};`
)
),
"// return url for filenames based on template",
`return ${url};`
]
: [
"// return url for filenames based on template",
`return ${url};`
]
),
"};"
]);
}
}
module.exports = GetChunkFilenameRuntimeModule;