diff --git a/packages/next-intl/src/plugin/createMessagesDeclaration.tsx b/packages/next-intl/src/plugin/createMessagesDeclaration.tsx index c273a2b13..692097da9 100644 --- a/packages/next-intl/src/plugin/createMessagesDeclaration.tsx +++ b/packages/next-intl/src/plugin/createMessagesDeclaration.tsx @@ -1,6 +1,7 @@ import fs from 'fs'; import path from 'path'; import {throwError} from './utils.js'; +import watchFile from './watchFile.js'; function runOnce(fn: () => void) { if (process.env._NEXT_INTL_COMPILE_MESSAGES === '1') { @@ -40,10 +41,8 @@ export default function createMessagesDeclaration(messagesPath: string) { } function startWatching(messagesPath: string) { - const watcher = fs.watch(messagesPath, (eventType) => { - if (eventType === 'change') { - compileDeclaration(messagesPath, true); - } + const watcher = watchFile(messagesPath, () => { + compileDeclaration(messagesPath, true); }); process.on('exit', () => { diff --git a/packages/next-intl/src/plugin/watchFile.tsx b/packages/next-intl/src/plugin/watchFile.tsx new file mode 100644 index 000000000..744430f89 --- /dev/null +++ b/packages/next-intl/src/plugin/watchFile.tsx @@ -0,0 +1,21 @@ +import fs from 'fs'; +import path from 'path'; + +/** + * Wrapper around `fs.watch` that provides a workaround + * for https://github.com/nodejs/node/issues/5039. + */ +export default function watchFile(filepath: string, callback: () => void) { + const directory = path.dirname(filepath); + const filename = path.basename(filepath); + + return fs.watch( + directory, + {persistent: false, recursive: false}, + (event, changedFilename) => { + if (changedFilename === filename) { + callback(); + } + } + ); +}