feat: add watch flag to cli#111
Open
RalphK66 wants to merge 4 commits into
Open
Conversation
On Windows the ReadDirectoryChangesW call is not async and is queued via APC
Contributor
Author
|
@privatenumber I know it's a reasonably extensive change, but would be cool to see the feature added. Let me know if you want to chat about the implementation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #110
Summary
Adds
--watch/-wflag to the CLI. When enabled,.d.tsfiles are generated/updated/removed in real-time as.module.cssfiles are created, modified, or deleted — no browser or manual re-run needed.npx vite-css-modules --watch npx vite-css-modules -w "src/**/*.module.css"Motivation
In large projects you frequently create or edit
.module.cssfiles and expect TypeScript types immediately. Today you must either load the page in the browser (so the transform hook fires) or re-run the CLI manually. Neither works well during active development.How it works
.d.ts.d.tsand.d.ts.mapDesign decisions
File watcher:
@parcel/watcherEvaluated three options:
@parcel/watcher(chosen) — native C++ bindings using OS-level APIs (FSEvents/inotify/ReadDirectoryChanges). Delivers batched events via a callback, no "ready" event ceremony needed.subscribe()is async — watcher is live once the promise resolves.chokidar— pure JS, most popular. But v5 dropped glob support and theignoredoption no longer accepts globs, requiring manual workarounds. Also hit EMFILE errors when symlinkednode_modulescaused recursive watching.fs.watch— zero deps but unreliable cross-platform (duplicate events, no recursive on Linux < Node 20).Glob matching:
picomatch@parcel/watcherwatches a directory and reports all events. We filter withpicomatchmatchers against the user's glob patterns, usingslash()to normalize paths on Windows.Debounce: 100ms per-file
Editors often emit multiple FS events for a single save (write temp → rename). Per-file debounce at 100ms prevents partial reads and redundant work without delaying unrelated files. Evaluated 30ms (too aggressive for VS Code's write patterns) and chokidar's
awaitWriteFinish(polling-based, adds latency).Error handling
process.exitCode = 1(existing behavior)failOnError: false). Errors always print regardless of--silent.Ignored directories
node_modules,.git,dist,coverage— passed to@parcel/watcher's native ignore (so the OS never reports events from them) and also checked at runtime via picomatch as a safety net.Refactoring
Extracted shared logic so one-shot and watch modes use the same code paths:
generate-declaration.ts— single-file type generation withfailOnErrorandallowArbitraryNamedExportsparams (previouslyallowArbitraryNamedExportswas hardcoded tofalsein the CLI)glob-scope.ts— resolves user globs + cwd vs config root logicload-css-module.ts— addedinvalidate()method to clear the transform cache for a specific file (required for watch to re-process changed files)New dependencies
@parcel/watcherpicomatch@types/picomatchTests
6 new tests covering:
.d.tswhen source file is deleted--silent--silentAll 44 existing CLI tests continue to pass.
NOTE: I used AI to help me