Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle watch events on macOS caused by git #1696

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/next-intl/src/plugin/createMessagesDeclaration.tsx
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down Expand Up @@ -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', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/next-intl/src/plugin/watchFile.tsx
Original file line number Diff line number Diff line change
@@ -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();
}
}
);
}