forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.js
281 lines (244 loc) · 6.84 KB
/
watcher.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
const assert = require('assert');
const fs = require('../src/utils/fs');
const nodeFS = require('fs');
const path = require('path');
const {
bundler,
run,
assertBundleTree,
sleep,
nextBundle,
rimraf,
ncp
} = require('./utils');
describe('watcher', function() {
let b;
beforeEach(async function() {
await rimraf(__dirname + '/input');
});
afterEach(function() {
if (b) {
b.stop();
}
});
it('should rebuild on file change', async function() {
await ncp(__dirname + '/integration/commonjs', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true});
let bundle = await b.bundle();
let output = await run(bundle);
assert.equal(output(), 3);
await fs.writeFile(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
bundle = await nextBundle(b);
output = await run(bundle);
assert.equal(output(), 10);
});
it('should re-generate bundle tree when files change', async function() {
await ncp(__dirname + '/integration/dynamic-hoist', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true});
let bundle = await b.bundle();
await assertBundleTree(bundle, {
name: 'index.js',
assets: [
'index.js',
'common.js',
'common-dep.js',
'bundle-loader.js',
'bundle-url.js',
'js-loader.js'
],
childBundles: [
{
assets: ['a.js'],
childBundles: [
{
type: 'map'
}
]
},
{
assets: ['b.js'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(bundle);
assert.equal(await output(), 7);
// change b.js so that it no longer depends on common.js.
// This should cause common.js and dependencies to no longer be hoisted to the root bundle.
await fs.writeFile(__dirname + '/input/b.js', 'module.exports = 5;');
bundle = await nextBundle(b);
await assertBundleTree(bundle, {
name: 'index.js',
assets: ['index.js', 'bundle-loader.js', 'bundle-url.js', 'js-loader.js'],
childBundles: [
{
assets: ['a.js', 'common.js', 'common-dep.js'],
childBundles: [
{
type: 'map'
}
]
},
{
assets: ['b.js'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
output = await run(bundle);
assert.equal(await output(), 8);
});
it('should only re-package bundles that changed', async function() {
await ncp(__dirname + '/integration/dynamic-hoist', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true});
await b.bundle();
let mtimes = (await fs.readdir(__dirname + '/dist')).map(
f =>
(nodeFS.statSync(__dirname + '/dist/' + f).mtime.getTime() / 1000) | 0
);
await sleep(1100); // mtime only has second level precision
await fs.writeFile(
__dirname + '/input/b.js',
'module.exports = require("./common")'
);
await nextBundle(b);
let newMtimes = (await fs.readdir(__dirname + '/dist')).map(
f =>
(nodeFS.statSync(__dirname + '/dist/' + f).mtime.getTime() / 1000) | 0
);
assert.deepEqual(mtimes.sort().slice(0, 2), newMtimes.sort().slice(0, 2));
assert.notEqual(mtimes[mtimes.length - 1], newMtimes[newMtimes.length - 1]);
});
it('should unload assets that are orphaned', async function() {
await ncp(__dirname + '/integration/dynamic-hoist', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true});
let bundle = await b.bundle();
await assertBundleTree(bundle, {
name: 'index.js',
assets: [
'index.js',
'common.js',
'common-dep.js',
'bundle-loader.js',
'bundle-url.js',
'js-loader.js'
],
childBundles: [
{
assets: ['a.js'],
childBundles: [
{
type: 'map'
}
]
},
{
assets: ['b.js'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(bundle);
assert.equal(await output(), 7);
assert(b.loadedAssets.has(path.join(__dirname, '/input/common-dep.js')));
// Get rid of common-dep.js
await fs.writeFile(__dirname + '/input/common.js', 'module.exports = 5;');
bundle = await nextBundle(b);
await assertBundleTree(bundle, {
name: 'index.js',
assets: [
'index.js',
'common.js',
'bundle-loader.js',
'bundle-url.js',
'js-loader.js'
],
childBundles: [
{
assets: ['a.js'],
childBundles: [
{
type: 'map'
}
]
},
{
assets: ['b.js'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
output = await run(bundle);
assert.equal(await output(), 13);
assert(!b.loadedAssets.has(path.join(__dirname, '/input/common-dep.js')));
});
it('should recompile all assets when a config file changes', async function() {
await ncp(__dirname + '/integration/babel', __dirname + '/input');
b = bundler(__dirname + '/input/index.js', {watch: true});
await b.bundle();
let file = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(!file.includes('function Foo'));
assert(!file.includes('function Bar'));
// Change babelrc, should recompile both files
let babelrc = JSON.parse(
await fs.readFile(__dirname + '/input/.babelrc', 'utf8')
);
babelrc.presets[0][1].targets.browsers.push('IE >= 11');
await sleep(100);
await fs.writeFile(__dirname + '/input/.babelrc', JSON.stringify(babelrc));
await nextBundle(b);
file = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
assert(file.includes('function Foo'));
assert(file.includes('function Bar'));
});
it('should rebuild if the file behind a symlink changes', async function() {
await ncp(
__dirname + '/integration/commonjs-with-symlinks/',
__dirname + '/input'
);
b = bundler(__dirname + '/input/src/index.js', {
watch: true
});
let bundle = await b.bundle();
let output = await run(bundle);
assert.equal(output(), 3);
await fs.writeFile(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
bundle = await nextBundle(b);
output = await run(bundle);
assert.equal(output(), 10);
});
});