refactor(watcher): use direct class imports for event sources#5870
refactor(watcher): use direct class imports for event sources#5870killagu wants to merge 1 commit intoeggjs:nextfrom
Conversation
Replace `path.join(import.meta.dirname, ...)` string paths with direct ES imports of the DefaultEventSource and DevelopmentEventSource classes so the watcher plugin can be statically bundled by turbopack. The `eventSources` config type widens to `Record<string, string | typeof BaseEventSource>` so existing consumers passing module paths continue to work. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe WatcherConfig's eventSources type definition was expanded to accept both module path strings and direct event source class references. Default configuration values for Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request updates the watcher configuration to use direct class imports for event sources instead of file paths. A TypeScript error was identified where BaseEventSource is imported as a type but subsequently used with the typeof operator, which requires it to be imported as a value.
| @@ -1,4 +1,6 @@ | |||
| import path from 'node:path'; | |||
| import type { BaseEventSource } from '../lib/event-sources/base.ts'; | |||
There was a problem hiding this comment.
Using import type for BaseEventSource will cause a TypeScript error (TS2693) when used with the typeof operator on line 15. The typeof operator in a type position requires the identifier to be available in the value-space, but import type only brings it into the type-space. Since BaseEventSource is a class, you should import it as a value. TypeScript will still optimize the import away if it is only used in type positions.
| import type { BaseEventSource } from '../lib/event-sources/base.ts'; | |
| import { BaseEventSource } from '../lib/event-sources/base.ts'; |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Refactors watcher eventSources configuration to use direct ES imports of event source classes instead of runtime-resolved module-path strings, improving static discoverability for bundlers.
Changes:
- Replace path-based
eventSourcesentries with importedDefaultEventSource/DevelopmentEventSourceclasses - Update
WatcherConfig.eventSourcestyping and docstring to allow either module-path strings or event source classes
| @@ -1,4 +1,6 @@ | |||
| import path from 'node:path'; | |||
| import type { BaseEventSource } from '../lib/event-sources/base.ts'; | |||
There was a problem hiding this comment.
typeof BaseEventSource is a value query, but BaseEventSource is imported with import type, so it won’t exist as a value. Fix by importing BaseEventSource as a normal import (if it’s a runtime-exported class), or by changing the union to a constructor-type that only depends on the type (e.g., new (...args: any[]) => BaseEventSource)."
| * key is event source type, value is string (module path) or event source class | ||
| */ | ||
| eventSources: Record<string, string>; | ||
| eventSources: Record<string, string | typeof BaseEventSource>; |
There was a problem hiding this comment.
typeof BaseEventSource is a value query, but BaseEventSource is imported with import type, so it won’t exist as a value. Fix by importing BaseEventSource as a normal import (if it’s a runtime-exported class), or by changing the union to a constructor-type that only depends on the type (e.g., new (...args: any[]) => BaseEventSource)."
| eventSources: Record<string, string | typeof BaseEventSource>; | |
| eventSources: Record<string, string | (new (...args: any[]) => BaseEventSource)>; |
| import type { BaseEventSource } from '../lib/event-sources/base.ts'; | ||
| import DefaultEventSource from '../lib/event-sources/default.ts'; | ||
| import DevelopmentEventSource from '../lib/event-sources/development.ts'; |
There was a problem hiding this comment.
These relative imports include .ts extensions. If this module is ever transpiled to .js and executed by Node without a TS-aware loader, Node will try to resolve .ts files at runtime and fail. Prefer the project’s runtime-safe convention (commonly .js specifiers under NodeNext, or extensionless specifiers if your build rewrites them) to avoid runtime/bundle resolution issues.
| import type { BaseEventSource } from '../lib/event-sources/base.ts'; | |
| import DefaultEventSource from '../lib/event-sources/default.ts'; | |
| import DevelopmentEventSource from '../lib/event-sources/development.ts'; | |
| import type { BaseEventSource } from '../lib/event-sources/base.js'; | |
| import DefaultEventSource from '../lib/event-sources/default.js'; | |
| import DevelopmentEventSource from '../lib/event-sources/development.js'; |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #5870 +/- ##
==========================================
- Coverage 86.00% 86.00% -0.01%
==========================================
Files 667 667
Lines 18945 18945
Branches 3652 3652
==========================================
- Hits 16294 16293 -1
- Misses 2297 2298 +1
Partials 354 354 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Changes the default `eventSources` config in `plugins/watcher/src/config/config.default.ts` from `path.join(import.meta.dirname, ...)` strings to direct ES `import` of the event source classes.
Why
This is batch 1, part of a 19-PR split of #5863 (the egg-bundler PR). #5863 is kept open as a tracking reference. This PR is independent of the other batch-1 PRs.
The current path-string form requires the watcher plugin to dynamically resolve and `require()` event source files at runtime, which a static bundler cannot follow. Switching to direct class imports makes the references statically discoverable, so the plugin can be bundled by turbopack.
Pure refactor — runtime behavior is identical (the watcher already resolved these paths into class instances at boot).
Test plan
Stack context
Other batch-1 PRs (independent, can land in any order):
🤖 Generated with Claude Code
Summary by CodeRabbit