forked from parcel-bundler/parcel
/
detect-dir-change.js
129 lines (101 loc) 路 3.56 KB
/
detect-dir-change.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
const assert = require('assert');
const Path = require('path');
const fs = require('@parcel/fs');
const {sleep, rimraf, ncp, bundler} = require('@parcel/test-utils');
let inputRoot = Path.join(__dirname, 'input', 'detect-dir-change');
describe('detect directory changes', function() {
beforeEach(async function() {
await rimraf(inputRoot);
await fs.mkdirp(inputRoot);
await ncp(__dirname + '/integration/detect-dir-change/', inputRoot);
});
describe('when a file matches a glob', async function() {
it('should rebuild when the file is added', async function() {
// 1. Bundle
let b = bundler('test/input/detect-dir-change/src/*.js', {
watch: true
});
await b.bundle();
assert(await fs.exists(Path.join(__dirname, '/dist/', 'index.js')));
// We'll check if bundle is called with this listener.
let bundled = false;
b.on('bundled', () => {
bundled = true;
});
// 2. Write file and assert.
await fs.writeFile(
Path.join(inputRoot, './src/app.js'),
"module.exports = function () { return 'app' }"
);
await sleep(1000);
assert(bundled);
});
it('should rebuild when the file is removed', async function() {
// 1. Add file and check the result bundle has all files.
let filePath = Path.join(inputRoot, './src/app.js');
await fs.writeFile(
filePath,
"module.exports = function () { return 'app' }"
);
let b = bundler('test/input/detect-dir-change/src/*.js', {
watch: true
});
let bundle = await b.bundle();
let childBundleNames = Array.from(bundle.childBundles.values()).map(
bundle => Path.basename(bundle.name)
);
assert(childBundleNames.includes('index.js'));
assert(childBundleNames.includes('app.js'));
// We'll check if bundle is called with this listener.
let bundled = false;
b.on('bundled', () => {
bundled = true;
});
// 2. Check dist file removed correctly.
await fs.unlink(filePath);
await sleep(1000);
assert(bundled);
});
});
describe('when a file does not match a glob', async function() {
it('should not rebuild when the file is added', async function() {
// 1. Bundle
let b = bundler('test/input/detect-dir-change/src/*.js', {
watch: true
});
await b.bundle();
assert(await fs.exists(Path.join(__dirname, '/dist/', 'index.js')));
// We'll check if bundle is called with this listener.
let bundled = false;
b.on('bundled', () => {
bundled = true;
});
// 2. Create unrelated file and assert
await fs.writeFile(
Path.join(inputRoot, './src/app2.ts'),
"module.exports = function () { return 'app' }"
);
await sleep(1000);
assert(!bundled);
});
it('should not rebuild when the file is removed', async function() {
// 1. Add file and check bundle has all files.
let filePath = Path.join(inputRoot, './src/test.html');
await fs.writeFile(filePath, '<html></html>');
let b = bundler('test/input/detect-dir-change/src/*.js', {
watch: true
});
let bundle = await b.bundle();
assert(Path.basename(bundle.name), 'index.js');
// We'll check if bundle is called with this listener.
let bundled = false;
b.on('bundled', () => {
bundled = true;
});
// 2. Remove file and assert that bundle() isn't called.
await fs.unlink(filePath);
await sleep(1000);
assert(!bundled);
});
});
});