feat(react-email): hot reloading around dynamic import#3422
feat(react-email): hot reloading around dynamic import#3422actuallyzefe wants to merge 21 commits into
Conversation
Introduced a new --watch option to the CLI for specifying additional file or directory paths to monitor. Changes in these paths will trigger a reload of all email previews, enhancing the development experience for files loaded at runtime. Updated the setupHotreloading function to handle these paths and added utility functions for path management.
|
@actuallyzefe is attempting to deploy a commit to the resend Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 548a3c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
commit: |
There was a problem hiding this comment.
No issues found across 6 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Shadow auto-approve: would auto-approve. Additive, opt-in CLI feature for the dev server with no impact on production logic or default behavior. Includes tests and documentation.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Shadow auto-approve: would require human review. Introduces a new CLI flag and additive logic for directory traversal and watching in the dev server. This is a feature addition that requires human review for UX and performance impact.
gabrielmfern
left a comment
There was a problem hiding this comment.
I think it's worth it exploring if we can improve file watching to get things to work with dynamic imports too.
on it, thanks |
This commit removes the `--watch` option from the CLI, simplifying the development server setup. It enhances the dynamic import handling by introducing a mechanism to track directories from dynamic imports, ensuring that changes in these directories trigger email previews to reload. Additionally, it updates the dependency graph to accommodate these changes and includes tests for the new functionality.
There was a problem hiding this comment.
3 issues found across 13 files (changes from recent commits).
Shadow auto-approve: would not auto-approve because issues were found.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/react-email/src/cli/utils/preview/hot-reloading/get-imported-modules.ts">
<violation number="1" location="packages/react-email/src/cli/utils/preview/hot-reloading/get-imported-modules.ts:68">
P2: Dynamic `import()` with template literals that have no interpolation are misclassified as glob prefixes instead of static imports.</violation>
</file>
<file name="packages/react-email/src/cli/utils/preview/hot-reloading/create-dependency-graph.ts">
<violation number="1" location="packages/react-email/src/cli/utils/preview/hot-reloading/create-dependency-graph.ts:81">
P2: `isUnderDirectory` fails for root directory paths (`/`), causing incorrect ancestry/containment checks in new dynamic glob dependency handling.</violation>
<violation number="2" location="packages/react-email/src/cli/utils/preview/hot-reloading/create-dependency-graph.ts:96">
P2: Alias-based dynamic import template prefixes are discarded, so hot reloading can miss changes for files imported via tsconfig path aliases.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
…solution This commit introduces a new function to resolve tsconfig path aliases for dynamic imports, allowing for more flexible module loading. It updates the dependency graph to accommodate these changes and adds tests to verify the correct resolution of aliased paths. Additionally, it refactors the `isUnderDirectory` function to handle edge cases more effectively.
There was a problem hiding this comment.
1 issue found across 8 files (changes from recent commits).
Shadow auto-approve: would not auto-approve because issues were found.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/react-email/src/cli/utils/preview/hot-reloading/resolve-path-aliases.ts">
<violation number="1" location="packages/react-email/src/cli/utils/preview/hot-reloading/resolve-path-aliases.ts:56">
P2: Using `() => true` for `fileExists` in `matchPath` can short-circuit tsconfig path fallback and return non-existent first candidates, causing valid later alias mappings to be skipped.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
0 issues found across 5 files (changes from recent commits).
Shadow auto-approve: would require human review. This PR introduces non-trivial logic changes to the dev server's dependency graph and file-watching mechanism, including AST traversal for dynamic imports.
There was a problem hiding this comment.
0 issues found across 5 files (changes from recent commits).
Shadow auto-approve: would require human review. This PR modifies core dependency graph resolution and hot-reloading logic in the CLI, which is a high-impact area that requires human validation of the AST traversal logic.
…not-watching-messages-files' into fix/3412-email-dev-not-watching-messages-files
…yDirectories and update related logic This change improves clarity by renaming `globDependencyPaths` to `dynamicDependencyDirectories` across the codebase. The update includes adjustments in the dependency graph creation and related tests to reflect this new naming convention, enhancing the understanding of dynamic imports in the context of the project.
There was a problem hiding this comment.
0 issues found across 6 files (changes from recent commits).
Requires human review: This PR introduces significant logic changes to the dev server's dependency graph and hot-reloading mechanism, including AST traversal for dynamic imports and manual watcher management.
Hey, thanks for the reviews. I think i resolved the comments. |
Hey, thanks for the reviews. I think i resolved the comments. |
Fixes #3412.
When an email template loads files through a dynamic
import(). for example,react-i18nextregistering translations viaresourcesToBackend((lng, ns) => import(./messages/${lng}/${ns}.json)). those files never entered the static dependency graph thatemail devbuilds. The chokidar watcher therefore never saw them, and editing a translation JSON did not refresh the preview.The user had to edit the template at the same time or restart the dev server.
This PR teaches the dev server's AST traversal to recognize dynamic
import()calls, so the relevant directories are watched automatically, no flag, no configuration.How it works
The dependency graph already parses every module with Babel and collects static
import/requirepaths. Two cases are now alsohandled:
import('./literal-string')treated identically to a static import. The path is added to the graph as a regular filedependency.
import(`./prefix/${expr}/...`)the leading static portion of the template literal (./prefix/) is captured as a globdirectory dependency of the importing module. The directory is added to chokidar's watch set, and any file change inside it
propagates to the importing module's dependents through the existing transitive-resolution path. The set of watched directories is
reconciled on every graph update, so adding or removing a dynamic import while the dev server is running just works.
Guards in
resolveGlobPrefixToDirectorykeep the watch set focused:import(`pkg/${x}`));import(`./${x}`)would otherwise reload everything onevery change);
Behavior
import(`./locales/${lng}.json`)→./locales/is watched automatically.etc.
import 'x.json'continues to work exactly as before.Summary by cubic
Auto-watches directories discovered from dynamic
import()calls inreact-email dev, so edits to runtime-loaded files (e.g., i18n JSON) refresh previews. Resolvestsconfigpath aliases with fallback; no flags or config needed.Bug Fixes
import('./x')andimport(./x)are treated as static;import(./prefix/${...})watches the leading./prefix/; alias formimport(@/prefix/${...})resolvestsconfigpath aliases with fallback to the first existing directory.Refactors
dynamicDependencyDirectoriesfor clarity.Written for commit 548a3c2. Summary will update on new commits.