Skip to content

Commit c4c5986

Browse files
authored
fix: Handle watch events on macOS caused by git (#1696)
1 parent 9e73cbe commit c4c5986

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/next-intl/src/plugin/createMessagesDeclaration.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import path from 'path';
33
import {throwError} from './utils.js';
4+
import watchFile from './watchFile.js';
45

56
function runOnce(fn: () => void) {
67
if (process.env._NEXT_INTL_COMPILE_MESSAGES === '1') {
@@ -40,10 +41,8 @@ export default function createMessagesDeclaration(messagesPath: string) {
4041
}
4142

4243
function startWatching(messagesPath: string) {
43-
const watcher = fs.watch(messagesPath, (eventType) => {
44-
if (eventType === 'change') {
45-
compileDeclaration(messagesPath, true);
46-
}
44+
const watcher = watchFile(messagesPath, () => {
45+
compileDeclaration(messagesPath, true);
4746
});
4847

4948
process.on('exit', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* Wrapper around `fs.watch` that provides a workaround
6+
* for https://github.com/nodejs/node/issues/5039.
7+
*/
8+
export default function watchFile(filepath: string, callback: () => void) {
9+
const directory = path.dirname(filepath);
10+
const filename = path.basename(filepath);
11+
12+
return fs.watch(
13+
directory,
14+
{persistent: false, recursive: false},
15+
(event, changedFilename) => {
16+
if (changedFilename === filename) {
17+
callback();
18+
}
19+
}
20+
);
21+
}

0 commit comments

Comments
 (0)