forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetChunkFilenameRuntimeModule.js
80 lines (75 loc) · 2.37 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");
class GetChunkFilenameRuntimeModule extends RuntimeModule {
constructor(compilation, chunk, contentType, global, filename) {
super(`get chunk ${contentType} filename`);
this.compilation = compilation;
this.chunk = chunk;
this.contentType = contentType;
this.global = global;
this.filename = filename;
}
/**
* @returns {string} runtime code
*/
generate() {
const { global, chunk, contentType, filename, compilation } = this;
const mainTemplate = compilation.mainTemplate;
const chunkMaps = chunk.getChunkMaps();
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: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`,
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([
`${global} = function(chunkId) {`,
Template.indent([`return ${url};`]),
"};"
]);
}
}
module.exports = GetChunkFilenameRuntimeModule;