Skip to content

Commit

Permalink
Watch: listen for new files added to a directory (#3812)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrage committed Oct 8, 2020
1 parent 929f0ac commit 74e5081
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/watch/fileWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class FileWatcher {
};
const watcher = chokidar
.watch([], this.chokidarOptions)
.on('add', handleChange)
.on('change', handleChange)
.on('unlink', handleChange);
return watcher;
Expand Down
89 changes: 89 additions & 0 deletions test/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,95 @@ describe('rollup.watch', () => {
});
});

it('respects initially missing added watched files', () => {
return sander
.copydir('test/watch/samples/basic')
.to('test/_tmp/input')
.then(() => {
watcher = rollup.watch({
input: 'test/_tmp/input/main.js',
output: {
file: 'test/_tmp/output/bundle.js',
format: 'cjs',
exports: 'auto'
},
plugins: {
transform() {
this.addWatchFile('test/_tmp/input/dep');
return `export default ${sander.existsSync('test/_tmp/input/dep')}`;
}
}
});

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), false);
sander.writeFileSync('test/_tmp/input/dep', '');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), true);
}
]);
});
});

it('respects unlinked and re-added watched files', () => {
return sander
.copydir('test/watch/samples/basic')
.to('test/_tmp/input')
.then(() => {
sander.writeFileSync('test/_tmp/input/dep', '');
watcher = rollup.watch({
input: 'test/_tmp/input/main.js',
output: {
file: 'test/_tmp/output/bundle.js',
format: 'cjs',
exports: 'auto'
},
plugins: {
transform() {
this.addWatchFile('test/_tmp/input/dep');
return `export default ${sander.existsSync('test/_tmp/input/dep')}`;
}
}
});

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), true);
sander.unlinkSync('test/_tmp/input/dep');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), false);
sander.writeFileSync('test/_tmp/input/dep', '');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), true);
}
]);
});
});

it('does not rerun the transform hook if a non-watched change triggered the re-run', () => {
let transformRuns = 0;
return sander
Expand Down

0 comments on commit 74e5081

Please sign in to comment.