WebWorkerMainTemplatePlugin.js
5.83 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const Template = require("../Template");
class WebWorkerMainTemplatePlugin {
apply(mainTemplate) {
const needChunkOnDemandLoadingCode = chunk => {
for (const chunkGroup of chunk.groupsIterable) {
if (chunkGroup.getNumberOfChildren() > 0) return true;
}
return false;
};
mainTemplate.hooks.localVars.tap(
"WebWorkerMainTemplatePlugin",
(source, chunk) => {
if (needChunkOnDemandLoadingCode(chunk)) {
return Template.asString([
source,
"",
"// object to store loaded chunks",
'// "1" means "already loaded"',
"var installedChunks = {",
Template.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 1`).join(",\n")
),
"};"
]);
}
return source;
}
);
mainTemplate.hooks.requireEnsure.tap(
"WebWorkerMainTemplatePlugin",
(_, chunk, hash) => {
const chunkFilename = mainTemplate.outputOptions.chunkFilename;
const chunkMaps = chunk.getChunkMaps();
return Template.asString([
"promises.push(Promise.resolve().then(function() {",
Template.indent([
'// "1" is the signal for "already loaded"',
"if(!installedChunks[chunkId]) {",
Template.indent([
"importScripts(" +
"__webpack_require__.p + " +
mainTemplate.getAssetPath(JSON.stringify(chunkFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length =>
`" + ${mainTemplate.renderCurrentHashCode(
hash,
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] + "`;
},
contentHash: {
javascript: `" + ${JSON.stringify(
chunkMaps.contentHash.javascript
)}[chunkId] + "`
},
contentHashWithLength: {
javascript: length => {
const shortContentHashMap = {};
const contentHash = chunkMaps.contentHash.javascript;
for (const chunkId of Object.keys(contentHash)) {
if (typeof contentHash[chunkId] === "string") {
shortContentHashMap[chunkId] = contentHash[
chunkId
].substr(0, length);
}
}
return `" + ${JSON.stringify(
shortContentHashMap
)}[chunkId] + "`;
}
},
name: `" + (${JSON.stringify(
chunkMaps.name
)}[chunkId]||chunkId) + "`
},
contentHashType: "javascript"
}) +
");"
]),
"}"
]),
"}));"
]);
}
);
mainTemplate.hooks.bootstrap.tap(
"WebWorkerMainTemplatePlugin",
(source, chunk, hash) => {
if (needChunkOnDemandLoadingCode(chunk)) {
const chunkCallbackName =
mainTemplate.outputOptions.chunkCallbackName;
const globalObject = mainTemplate.outputOptions.globalObject;
return Template.asString([
source,
`${globalObject}[${JSON.stringify(
chunkCallbackName
)}] = function webpackChunkCallback(chunkIds, moreModules) {`,
Template.indent([
"for(var moduleId in moreModules) {",
Template.indent(
mainTemplate.renderAddModule(
hash,
chunk,
"moduleId",
"moreModules[moduleId]"
)
),
"}",
"while(chunkIds.length)",
Template.indent("installedChunks[chunkIds.pop()] = 1;")
]),
"};"
]);
}
return source;
}
);
mainTemplate.hooks.hotBootstrap.tap(
"WebWorkerMainTemplatePlugin",
(source, chunk, hash) => {
const hotUpdateChunkFilename =
mainTemplate.outputOptions.hotUpdateChunkFilename;
const hotUpdateMainFilename =
mainTemplate.outputOptions.hotUpdateMainFilename;
const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
const globalObject = mainTemplate.outputOptions.globalObject;
const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
JSON.stringify(hotUpdateChunkFilename),
{
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length =>
`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
chunk: {
id: '" + chunkId + "'
}
}
);
const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
JSON.stringify(hotUpdateMainFilename),
{
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length =>
`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
}
);
return (
source +
"\n" +
`var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
hotUpdateFunction
)}];\n` +
`${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ` +
Template.getFunctionContent(
require("./WebWorkerMainTemplate.runtime")
)
.replace(/\/\/\$semicolon/g, ";")
.replace(/\$require\$/g, mainTemplate.requireFn)
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
.replace(/\$hash\$/g, JSON.stringify(hash))
);
}
);
mainTemplate.hooks.hash.tap("WebWorkerMainTemplatePlugin", hash => {
hash.update("webworker");
hash.update("4");
});
}
}
module.exports = WebWorkerMainTemplatePlugin;