Skip to content

Implement watch mode#278

Merged
mizdra merged 7 commits into
mainfrom
implement-watch-mode
Dec 20, 2025
Merged

Implement watch mode#278
mizdra merged 7 commits into
mainfrom
implement-watch-mode

Conversation

@mizdra
Copy link
Copy Markdown
Owner

@mizdra mizdra commented Nov 3, 2025

close: #178

2025-12-21.19.26.32.mov

Design Notes:

  • Mimics the behavior of tsc --watch
  • Does not watch changes to tsconfig.json
    • This is for implementation simplicity
  • Uses chokidar for file watching
    • Considered using ts.sys.watchDirectory. However, it was unclear whether this API would be provided in ts-go, so it was not adopted.
  • File watching is handled by the runCMKInWatchMode function.
    • runCMKInWatchMode delegates most processing to Project.
    • Project is used by both the runCMK function for normal mode and the runCMKInWatchMode function for watch mode. It implements the common logic for both modes.
    • Through Project, CSS Module files are parsed and inspected, diagnostics are reported, and .d.ts files are emitted.
    • Project maintains internal caches for the parsing, checking, and emitting stages. When notified of file changes by runCMKInWatchMode, it discards the relevant caches.
    • See also Refactor the runner by introducing Project #276
  • Currently, it excessively discards the cache during the check stage when files are updated.
    • Plans to improve this in the future.

@mizdra mizdra added the Type: Feature New Feature label Nov 3, 2025
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Nov 3, 2025

🦋 Changeset detected

Latest commit: 96351ee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@css-modules-kit/codegen Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mizdra mizdra force-pushed the implement-watch-mode branch 4 times, most recently from 56f6254 to 04e1b6e Compare November 3, 2025 15:47
@mizdra
Copy link
Copy Markdown
Owner Author

mizdra commented Nov 3, 2025

TODO: Add tests for runCMKInWatchMode and Project

emitAndReportDiagnosticsTimer = setTimeout(() => {
emitAndReportDiagnosticsTimer = undefined;
emitAndReportDiagnostics().catch(logger.logError.bind(logger));
}, 250);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @throws {WriteDtsFileError}
*/
async function emitAndReportDiagnostics() {
logger.clearScreen();
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some users may not want the console cleared. We may need to add the --preserveWatchOutput option.

logger.logDiagnostics(diagnostics);
}
logger.logMessage(
`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`,
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly the same format as tsc messages. Users may find it difficult to distinguish whether a message is from tsc or css-modules-kit...

@mizdra mizdra force-pushed the implement-watch-mode branch 2 times, most recently from 352dd22 to 615dc2c Compare November 4, 2025 16:11
@mizdra mizdra force-pushed the implement-watch-mode branch from 5a3b590 to 4829df7 Compare November 22, 2025 13:15
@mizdra mizdra force-pushed the implement-watch-mode branch 2 times, most recently from 31633f2 to e68d6d3 Compare December 4, 2025 16:50
@mizdra mizdra force-pushed the implement-watch-mode branch 5 times, most recently from 2fa9c64 to bbdd340 Compare December 19, 2025 14:52
@mizdra mizdra force-pushed the implement-watch-mode branch from 9b62100 to cba7f2e Compare December 20, 2025 13:57
@mizdra mizdra force-pushed the implement-watch-mode branch from cba7f2e to 96351ee Compare December 20, 2025 15:35
Comment on lines +199 to +226
// Workaround for https://github.com/nodejs/node/issues/54450
if (platform === 'darwin') await sleep(100);

const loggerSpy = createLoggerSpy();
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir }), loggerSpy);
// Workaround for https://github.com/nodejs/node/issues/52601
if (platform === 'darwin') await sleep(100);

// Error when adding a file
vi.spyOn(watcher.project, 'addFile').mockImplementationOnce(() => {
throw new Error('test error');
});
await writeFile(iff.join('src/b.module.css'), '.b_1 { color: red; }');
await vi.waitFor(() => {
expect(loggerSpy.logError).toHaveBeenCalledTimes(1);
});

// Error when changing a file
vi.spyOn(watcher.project, 'updateFile').mockImplementationOnce(() => {
throw new Error('test error');
});
await writeFile(iff.join('src/a.module.css'), '.a_1 { color: blue; }');
await vi.waitFor(() => {
expect(loggerSpy.logError).toHaveBeenCalledTimes(2);
});

// Workaround for https://github.com/paulmillr/chokidar/issues/1399
await sleep(100);
Copy link
Copy Markdown
Owner Author

@mizdra mizdra Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never imagined I'd hit three bugs in a single test case. File watching is a abyss :)

ref: nodejs/node#54450, nodejs/node#52601, paulmillr/chokidar#1399

To help others who dare to challenge this abyss, I've left some information behind. Good luck!

@mizdra mizdra marked this pull request as ready for review December 20, 2025 15:44
@mizdra
Copy link
Copy Markdown
Owner Author

mizdra commented Dec 20, 2025

I've been working on developing this feature since late August, and it's finally complete 🎉

@mizdra mizdra merged commit f33b0a6 into main Dec 20, 2025
18 checks passed
@mizdra mizdra deleted the implement-watch-mode branch December 20, 2025 15:51
@github-actions github-actions Bot mentioned this pull request Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Feature New Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support --watch mode

1 participant