Skip to content

Commit

Permalink
Fix bugs related to watching symlinks (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper De Moor authored and devongovett committed Jun 24, 2018
1 parent f7b9fdb commit 7f4049d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Bundler.js
Expand Up @@ -56,6 +56,7 @@ class Bundler extends EventEmitter {
this.pending = false;
this.loadedAssets = new Map();
this.watchedAssets = new Map();

this.farm = null;
this.watcher = null;
this.hmr = null;
Expand Down Expand Up @@ -391,24 +392,27 @@ class Bundler extends EventEmitter {
return asset;
}

watch(path, asset) {
async watch(path, asset) {
if (!this.watcher) {
return;
}

path = await fs.realpath(path);

if (!this.watchedAssets.has(path)) {
this.watcher.watch(path);
this.watchedAssets.set(path, new Set());
}

this.watchedAssets.get(path).add(asset);
}

unwatch(path, asset) {
async unwatch(path, asset) {
if (!this.watchedAssets.has(path)) {
return;
}

path = await fs.realpath(path);

let watched = this.watchedAssets.get(path);
watched.delete(asset);

Expand Down
11 changes: 10 additions & 1 deletion src/utils/fs.js
Expand Up @@ -7,7 +7,16 @@ exports.writeFile = promisify(fs.writeFile);
exports.stat = promisify(fs.stat);
exports.readdir = promisify(fs.readdir);
exports.unlink = promisify(fs.unlink);
exports.realpath = promisify(fs.realpath);
exports.realpath = async function(path) {
const realpath = promisify(fs.realpath);
try {
path = await realpath(path);
} catch (e) {
// do nothing
}
return path;
};
exports.lstat = promisify(fs.lstat);

exports.exists = function(filename) {
return new Promise(resolve => {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/commonjs-with-symlinks/local.js
@@ -0,0 +1,2 @@
exports.a = 1;
exports.b = 2;
5 changes: 5 additions & 0 deletions test/integration/commonjs-with-symlinks/src/index.js
@@ -0,0 +1,5 @@
var local = require('./symlinked_local');

module.exports = function () {
return local.a + local.b;
};
25 changes: 25 additions & 0 deletions test/watcher.js
Expand Up @@ -253,4 +253,29 @@ describe('watcher', function() {
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);
});
});

0 comments on commit 7f4049d

Please sign in to comment.