-
Notifications
You must be signed in to change notification settings - Fork 114
Fix Chokidar watch so Story add or remove is picked up #625
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
Open
daaain
wants to merge
4
commits into
tajo:main
Choose a base branch
from
daaain:fix/chokidar-watch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d87c9cc
Fix Chokidar watch so Story add or remove is picked up
daaain ea93b23
Normalise path separators to fix Windows fails
daaain 35f74b8
Remove duplicated STORY_FILE_REGEX + use picomatch.scan().base for ba…
daaain 457c39a
Update changelog to reflect changes
daaain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@ladle/react": patch | ||
| --- | ||
|
|
||
| Fix story file watching for add/remove detection with chokidar v4 | ||
|
|
||
| Chokidar v4 no longer supports glob patterns directly, so the watcher now: | ||
|
|
||
| - Extracts base directories from glob patterns using `getGlobBasePath` | ||
| - Filters story files using `STORY_FILE_REGEX` in the `ignored` callback | ||
| - Properly handles the case where `stats` is undefined during initial directory checks | ||
|
|
||
| This fix ensures that adding or removing story files triggers a full reload as expected. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import chokidar from "chokidar"; | ||
|
|
||
| /** | ||
| * Extract the base directory from a glob pattern. | ||
| * e.g., "src/**\/*.stories.tsx" -> "src" | ||
| * @param {string} pattern | ||
| * @returns {string} | ||
| */ | ||
| export const getGlobBasePath = (pattern) => { | ||
| // Normalise path separators to forward slashes for cross-platform compatibility | ||
| const normalised = pattern.replace(/\\/g, "/"); | ||
| const parts = normalised.split("/"); | ||
| const baseParts = []; | ||
| for (const part of parts) { | ||
| if (part.includes("*") || part.includes("{") || part.includes("[")) break; | ||
| baseParts.push(part); | ||
| } | ||
| return baseParts.length > 0 ? baseParts.join("/") : "."; | ||
| }; | ||
|
|
||
| // Story file pattern - matches .stories.{js,jsx,ts,tsx,mdx} | ||
| export const STORY_FILE_REGEX = /\.stories\.(js|jsx|ts|tsx|mdx)$/; | ||
|
|
||
| /** | ||
| * Creates a chokidar watcher configured to watch story files. | ||
| * @param {string | string[]} storyPatterns - Glob pattern(s) for stories | ||
| * @param {object} options - Optional chokidar options override | ||
| * @returns {import("chokidar").FSWatcher} | ||
| */ | ||
| export const createStoryWatcher = (storyPatterns, options = {}) => { | ||
| const patterns = Array.isArray(storyPatterns) | ||
| ? storyPatterns | ||
| : [storyPatterns]; | ||
| const baseDirs = [...new Set(patterns.map(getGlobBasePath))]; | ||
|
|
||
| return chokidar.watch(baseDirs, { | ||
| persistent: true, | ||
| ignoreInitial: true, | ||
| ignored: (filePath, stats) => { | ||
| // Don't ignore directories - we need to traverse into them | ||
| // In chokidar v4, stats can be undefined for initial directory checks | ||
| if (stats === undefined || stats?.isDirectory()) return false; | ||
| // Only watch story files | ||
| return !STORY_FILE_REGEX.test(filePath); | ||
| }, | ||
| ...options, | ||
| }); | ||
| }; | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not redefine this here, there is already a default and users can customize it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, apologies, missed that 😅 just pushed an update to use the given pattern and also realised picomatch can find the base path so removed the custom script to do it