forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.js
181 lines (155 loc) · 4.69 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
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'));
describe('watcher', function() {
let b;
beforeEach(function() {
rimraf.sync(__dirname + '/input');
});
afterEach(function() {
if (b) {
b.stop();
}
});
function nextBundle(b) {
return new Promise(resolve => {
b.once('bundled', resolve);
});
}
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 = run(bundle);
assert.equal(output(), 3);
fs.writeFileSync(
__dirname + '/input/local.js',
'exports.a = 5; exports.b = 5;'
);
bundle = await nextBundle(b);
output = 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();
assertBundleTree(bundle, {
name: 'index.js',
assets: [
'index.js',
'common.js',
'common-dep.js',
'bundle-loader.js',
'bundle-url.js'
],
childBundles: [
{
assets: ['a.js'],
childBundles: []
},
{
assets: ['b.js'],
childBundles: []
}
]
});
let output = 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.
fs.writeFileSync(__dirname + '/input/b.js', 'module.exports = 5;');
bundle = await nextBundle(b);
assertBundleTree(bundle, {
name: 'index.js',
assets: ['index.js', 'bundle-loader.js', 'bundle-url.js'],
childBundles: [
{
assets: ['a.js', 'common.js', 'common-dep.js'],
childBundles: []
},
{
assets: ['b.js'],
childBundles: []
}
]
});
output = 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});
let bundle = await b.bundle();
let mtimes = fs
.readdirSync(__dirname + '/dist')
.map(
f => (fs.statSync(__dirname + '/dist/' + f).mtime.getTime() / 1000) | 0
);
await sleep(1000); // mtime only has second level precision
fs.writeFileSync(
__dirname + '/input/b.js',
'module.exports = require("./common")'
);
bundle = await nextBundle(b);
let newMtimes = fs
.readdirSync(__dirname + '/dist')
.map(
f => (fs.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();
assertBundleTree(bundle, {
name: 'index.js',
assets: [
'index.js',
'common.js',
'common-dep.js',
'bundle-loader.js',
'bundle-url.js'
],
childBundles: [
{
assets: ['a.js'],
childBundles: []
},
{
assets: ['b.js'],
childBundles: []
}
]
});
let output = run(bundle);
assert.equal(await output(), 7);
assert(b.loadedAssets.has(path.join(__dirname, '/input/common-dep.js')));
// Get rid of common-dep.js
fs.writeFileSync(__dirname + '/input/common.js', 'module.exports = 5;');
bundle = await nextBundle(b);
assertBundleTree(bundle, {
name: 'index.js',
assets: ['index.js', 'common.js', 'bundle-loader.js', 'bundle-url.js'],
childBundles: [
{
assets: ['a.js'],
childBundles: []
},
{
assets: ['b.js'],
childBundles: []
}
]
});
output = run(bundle);
assert.equal(await output(), 13);
assert(!b.loadedAssets.has(path.join(__dirname, '/input/common-dep.js')));
});
});