We ended up changing the approach: instead of telling ESBuild to ignore a file on watch mode, we do not write the file if its contents are the same, like so:
const newContent = (...)
if (fs.existsSync(path)) {
const currentContent = fs.readFileSync(path, 'utf8');
if (currentContent === newContent) {
process.exit(0);
}
}
fs.writeFileSync(path, newContent);