forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebWorkerMainTemplatePlugin.unittest.js
282 lines (249 loc) · 7.58 KB
/
WebWorkerMainTemplatePlugin.unittest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"use strict";
const should = require("should");
const sinon = require("sinon");
const WebWorkerMainTemplatePlugin = require("../lib/webworker/WebWorkerMainTemplatePlugin");
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
describe("WebWorkerMainTemplatePlugin", function() {
let env;
beforeEach(() => {
env = {};
});
it("has apply function", function() {
(new WebWorkerMainTemplatePlugin()).apply.should.be.a.Function();
});
describe("when applied", function() {
beforeEach(function() {
env.eventBindings = applyPluginWithOptions(WebWorkerMainTemplatePlugin);
env.handlerContext = {
requireFn: 'requireFn',
indent: (value) => typeof value === 'string' ? value : value.join("\n"),
asString: (values) => values.join("\n"),
renderCurrentHashCode: (value) => value,
outputOptions: {
chunkFilename: 'chunkFilename'
},
applyPluginsWaterfall: (moduleName, fileName, data) => {
return `"${moduleName}${data.hash}${data.hashWithLength()}${data.chunk && data.chunk.id || ''}"`;
},
renderAddModule: () => 'renderAddModuleSource();',
};
});
it("binds five event handlers", function() {
env.eventBindings.length.should.be.exactly(5);
});
describe("local-vars handler", function() {
beforeEach(() => {
env.eventBinding = env.eventBindings[0];
});
it("binds to local-vars event", () => {
env.eventBinding.name.should.be.exactly("local-vars");
});
describe("when no chunks are provided", () => {
beforeEach(() => {
const chunk = {
ids: [],
chunks: []
};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk);
});
it("returns the original source", () => {
env.source.should.be.exactly("moduleSource()")
});
});
describe("when chunks are provided", () => {
beforeEach(() => {
const chunk = {
ids: [1, 2, 3],
chunks: [
'foo',
'bar',
'baz'
]
};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123');
});
it("returns the original source with installed mapping", () => {
env.source.should.be.exactly(`
moduleSource()
// object to store loaded chunks
// "1" means "already loaded"
var installedChunks = {
1: 1,
2: 1,
3: 1
};
`.trim())
});
});
});
describe("require-ensure handler", () => {
beforeEach(() => {
env.eventBinding = env.eventBindings[1];
});
it("binds to require-ensure event", () => {
env.eventBinding.name.should.be.exactly("require-ensure");
});
describe("when called", () => {
beforeEach(() => {
const chunk = {};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123');
});
it("creates import scripts call and promise resolve", () => {
env.source.should.be.exactly(`
return new Promise(function(resolve) {
// "1" is the signal for "already loaded"
if(!installedChunks[chunkId]) {
importScripts("asset-path" + abc123 + "" + abc123 + "" + chunkId + "");
}
resolve();
});
`.trim())
});
});
});
describe("bootstrap handler", () => {
beforeEach(() => {
env.eventBinding = env.eventBindings[2];
});
it("binds to bootstrap event", () => {
env.eventBinding.name.should.be.exactly("bootstrap");
});
describe("when no chunks are provided", () => {
beforeEach(() => {
const chunk = {
ids: [],
chunks: []
};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk);
});
it("returns the original source", () => {
env.source.should.be.exactly("moduleSource()")
});
});
describe("when chunks are provided", () => {
beforeEach(() => {
const chunk = {
ids: [1, 2, 3],
chunks: [
'foo',
'bar',
'baz'
]
};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk);
});
it("returns the original source with chunk callback", () => {
env.source.should.be.exactly(`
moduleSource()
this["webpackChunk"] = function webpackChunkCallback(chunkIds, moreModules) {
for(var moduleId in moreModules) {
renderAddModuleSource();
}
while(chunkIds.length)
installedChunks[chunkIds.pop()] = 1;
};
`.trim())
});
});
});
describe("hot-bootstrap handler", () => {
beforeEach(() => {
env.eventBinding = env.eventBindings[3];
});
it("binds to hot-bootstrap event", () => {
env.eventBinding.name.should.be.exactly("hot-bootstrap");
});
describe("when called", () => {
beforeEach(() => {
const chunk = {};
env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123');
});
it("returns the original source with hot update callback", () => {
env.source.should.be.exactly(`
moduleSource()
var parentHotUpdateCallback = this["webpackHotUpdate"];
this["webpackHotUpdate"] = function webpackHotUpdateCallback(chunkId, moreModules) { // eslint-disable-line no-unused-vars
hotAddUpdateChunk(chunkId, moreModules);
if(parentHotUpdateCallback) parentHotUpdateCallback(chunkId, moreModules);
} ;
function hotDownloadUpdateChunk(chunkId) { // eslint-disable-line no-unused-vars
importScripts(requireFn.p + "asset-path" + abc123 + "" + abc123 + "" + chunkId + "");
}
function hotDownloadManifest(requestTimeout) { // eslint-disable-line no-unused-vars
requestTimeout = requestTimeout || 10000;
return new Promise(function(resolve, reject) {
if(typeof XMLHttpRequest === "undefined")
return reject(new Error("No browser support"));
try {
var request = new XMLHttpRequest();
var requestPath = requireFn.p + "asset-path" + abc123 + "" + abc123 + "";
request.open("GET", requestPath, true);
request.timeout = requestTimeout;
request.send(null);
} catch(err) {
return reject(err);
}
request.onreadystatechange = function() {
if(request.readyState !== 4) return;
if(request.status === 0) {
// timeout
reject(new Error("Manifest request to " + requestPath + " timed out."));
} else if(request.status === 404) {
// no update available
resolve();
} else if(request.status !== 200 && request.status !== 304) {
// other failure
reject(new Error("Manifest request to " + requestPath + " failed."));
} else {
// success
try {
var update = JSON.parse(request.responseText);
} catch(e) {
reject(e);
return;
}
resolve(update);
}
};
});
}
function hotDisposeChunk(chunkId) { //eslint-disable-line no-unused-vars
delete installedChunks[chunkId];
}
`.trim())
});
});
});
describe("hash handler", () => {
beforeEach(() => {
env.eventBinding = env.eventBindings[4];
env.handlerContext = {
outputOptions: {
publicPath: "Alpha",
filename: "Bravo",
chunkFilename: "Charlie",
chunkCallbackName: "Delta",
library: "Echo"
}
};
env.hashMock = {
update: sinon.spy()
};
env.eventBinding.handler.call(env.handlerContext, env.hashMock);
});
it("binds to hash event", () => {
env.eventBinding.name.should.be.exactly("hash");
});
it("updates hash object", () => {
env.hashMock.update.callCount.should.be.exactly(7);
sinon.assert.calledWith(env.hashMock.update, "webworker");
sinon.assert.calledWith(env.hashMock.update, "3");
sinon.assert.calledWith(env.hashMock.update, "Alpha");
sinon.assert.calledWith(env.hashMock.update, "Bravo");
sinon.assert.calledWith(env.hashMock.update, "Charlie");
sinon.assert.calledWith(env.hashMock.update, "Delta");
sinon.assert.calledWith(env.hashMock.update, "Echo");
});
});
});
});