forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmr.js
279 lines (220 loc) · 7.44 KB
/
hmr.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
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {bundler, run, assertBundleTree, sleep} = require('./utils');
const rimraf = require('rimraf');
const promisify = require('../src/utils/promisify');
const ncp = promisify(require('ncp'));
const WebSocket = require('ws');
const parseJson = require('parse-json');
describe('hmr', function() {
let b, ws;
beforeEach(function() {
rimraf.sync(__dirname + '/input');
});
afterEach(function() {
if (b) {
b.stop();
b = null;
}
if (ws) {
ws.close();
ws = null;
}
});
function nextEvent(emitter, event) {
return new Promise(resolve => {
emitter.once(event, resolve);
});
}
it('should emit an HMR update for the file that changed', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
ws = new WebSocket('ws://localhost:' + b.options.hmrPort);
fs.writeFileSync(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
let msg = parseJson(await nextEvent(ws, 'message'));
assert.equal(msg.type, 'update');
assert.equal(msg.assets.length, 1);
assert.equal(msg.assets[0].generated.js, 'exports.a = 5; exports.b = 5;');
assert.deepEqual(msg.assets[0].deps, {});
});
it('should emit an HMR update for all new dependencies along with the changed file', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
ws = new WebSocket('ws://localhost:' + b.options.hmrPort);
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"); exports.a = 5; exports.b = 5;'
);
let msg = parseJson(await nextEvent(ws, 'message'));
assert.equal(msg.type, 'update');
assert.equal(msg.assets.length, 2);
});
it('should emit an HMR error on bundle failure', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
ws = new WebSocket('ws://localhost:' + b.options.hmrPort);
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"; exports.a = 5; exports.b = 5;'
);
let msg = JSON.parse(await nextEvent(ws, 'message'));
assert.equal(msg.type, 'error');
assert.equal(
msg.error.message,
`${path.join(
__dirname,
'/input/local.js'
)}:1:12: Unexpected token, expected , (1:12)`
);
assert.equal(
msg.error.stack,
'> 1 | require("fs"; exports.a = 5; exports.b = 5;\n | ^'
);
});
it('should emit an HMR error to new connections after a bundle failure', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"; exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'buildEnd');
await sleep(50);
ws = new WebSocket('ws://localhost:' + b.options.hmrPort);
let msg = JSON.parse(await nextEvent(ws, 'message'));
assert.equal(msg.type, 'error');
});
it('should emit an HMR error-resolved on build after error', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
ws = new WebSocket('ws://localhost:' + b.options.hmrPort);
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"; exports.a = 5; exports.b = 5;'
);
let msg = JSON.parse(await nextEvent(ws, 'message'));
assert.equal(msg.type, 'error');
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"); exports.a = 5; exports.b = 5;'
);
let msg2 = JSON.parse(await nextEvent(ws, 'message'));
assert.equal(msg2.type, 'error-resolved');
});
it('should accept HMR updates in the runtime', async function() {
await ncp(__dirname + '/integration/hmr', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let outputs = [];
run(bundle, {
output(o) {
outputs.push(o);
}
});
assert.deepEqual(outputs, [3]);
fs.writeFileSync(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3, 10]);
});
it('should call dispose and accept callbacks', async function() {
await ncp(__dirname + '/integration/hmr-callbacks', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let outputs = [];
run(bundle, {
output(o) {
outputs.push(o);
}
});
assert.deepEqual(outputs, [3]);
fs.writeFileSync(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3, 'dispose', 10, 'accept']);
});
it('should work across bundles', async function() {
await ncp(__dirname + '/integration/hmr-dynamic', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let outputs = [];
run(bundle, {
output(o) {
outputs.push(o);
}
});
await sleep(50);
assert.deepEqual(outputs, [3]);
fs.writeFileSync(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'bundled');
await sleep(50);
assert.deepEqual(outputs, [3, 10]);
});
it('should log emitted errors', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let logs = [];
run(bundle, {
console: {
error(msg) {
logs.push(msg);
}
}
});
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"; exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'buildEnd');
await sleep(50);
assert.equal(logs.length, 1);
assert(logs[0].trim().startsWith('[parcel] 🚨'));
});
it('should log when errors resolve', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let logs = [];
run(bundle, {
console: {
error(msg) {
logs.push(msg);
},
log(msg) {
logs.push(msg);
}
}
});
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"; exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'buildEnd');
fs.writeFileSync(
__dirname + '/input/local.js',
'require("fs"); exports.a = 5; exports.b = 5;'
);
await nextEvent(b, 'buildEnd');
await sleep(50);
assert.equal(logs.length, 2);
assert(logs[0].trim().startsWith('[parcel] 🚨'));
assert(logs[1].trim().startsWith('[parcel] ✨'));
});
});