Search code examples
parceljs

parcel watch only detects first file change


I have the following in ./js/parcel/build-js.js (it is more or less a simplification of exactly what the API docs example does, except that it takes an optional --watch argument):

#!/usr/bin/env node

const Bundler = require('parcel-bundler');
const path = require('path');

const watch = process.argv.indexOf('--watch') > 0;

if (watch) console.log('Watching files...');

(async function bundleJs() {
  const jsBundler = new Bundler(path.join(__dirname, '../src/common.js'), {
    watch,
    hmr: false,
  });

  jsBundler.on('bundled', () => {
    console.log('bundled!');
  });

  const bundle = await jsBundler.bundle();

  console.log('done');
})();

When I run node js/parcel/build-js.js --watch, it detects the first change to src/common.js and prints:

Watching files...
✨  Built in 585ms.
bundled!
done

This is as I'd expect. When I edit and save src/common.js, it sees that and then the total output becomes (done gets deleted):

Watching files...
✨  Built in 585ms.
bundled!
✨  Built in 86ms.
bundled!

But after that, no file changes are detected. I make changes and save but it just sits there, producing no more output or updating the build. Why only once?

Note: If I do strace node js/parcel/build-js.js --watch, it seems to just sit on an unfinished epoll_wait(3,, which I guess means it's waiting for something, but maybe watching the wrong file...

Edit: Versions!

  • parcel-bundler: 1.12.3
  • node: 10.15.1
  • Ubuntu 18.04

Edit: using parcel watch

This appears to be a system-wide thing for me. I did yarn globals add parcel (which also installed 1.12.3), and now watching any JS file with parcel watch path/to/file.js does the same thing.


Solution

  • It turned out to be a conflict between Parcel's change detection and the default Vim setup. From the Hot Module Replacement docs:

    Some text editors and IDE's have a feature called safe write that basically prevents data loss, by taking a copy of the file and renaming it when saved.

    When using Hot Module Reload (HMR) this feature blocks the automatic detection of file updates, to disable safe write use the options provided below:

    I added set backupcopy=yes to my .vimrc and it started working.

    The solution for other editors is documented there as well.