Skip to content

refactor(watcher): use direct class imports for event sources#5870

Open
killagu wants to merge 1 commit intoeggjs:nextfrom
killagu:split/04-watcher-class-imports
Open

refactor(watcher): use direct class imports for event sources#5870
killagu wants to merge 1 commit intoeggjs:nextfrom
killagu:split/04-watcher-class-imports

Conversation

@killagu
Copy link
Copy Markdown
Contributor

@killagu killagu commented Apr 14, 2026

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

  • `pnpm --filter=@eggjs/watcher test` — 4/4 passed (+ 4 expected skips)
  • typecheck clean

Stack context

Other batch-1 PRs (independent, can land in any order):

  • `feat(utils): add setBundleModuleLoader runtime hook`
  • `refactor(onerror): inline error page template as string constant`
  • `refactor(development): inline loader trace template as string constant`

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Enhanced the watcher plugin's event source configuration system to support greater flexibility in how event sources are specified, enabling both direct class references and module path specifications.

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>
Copilot AI review requested due to automatic review settings April 14, 2026 03:59
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 338965e0-a5fa-4aff-b57c-82670fa6edab

📥 Commits

Reviewing files that changed from the base of the PR and between 490f849 and 2c971d4.

📒 Files selected for processing (1)
  • plugins/watcher/src/config/config.default.ts

📝 Walkthrough

Walkthrough

The WatcherConfig's eventSources type definition was expanded to accept both module path strings and direct event source class references. Default configuration values for default and development environments were refactored to use direct class imports instead of computed filesystem paths.

Changes

Cohort / File(s) Summary
Configuration Type Enhancement
plugins/watcher/src/config/config.default.ts
Updated eventSources type signature to Record<string, string | typeof BaseEventSource>, allowing both module paths and class references. Removed node:path usage and replaced filesystem-based default values with direct event source class imports (DefaultEventSource, DevelopmentEventSource).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 A config so neat, with classes so bright,
No paths need computing in the pale moonlight!
EventSources dance with flexibility's grace,
Strings and classes sharing the same cozy place!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing string-based module paths with direct ES class imports for event sources in the watcher plugin.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
import type { BaseEventSource } from '../lib/event-sources/base.ts';
import { BaseEventSource } from '../lib/event-sources/base.ts';

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 eventSources entries with imported DefaultEventSource / DevelopmentEventSource classes
  • Update WatcherConfig.eventSources typing 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';
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

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)."

Copilot uses AI. Check for mistakes.
* key is event source type, value is string (module path) or event source class
*/
eventSources: Record<string, string>;
eventSources: Record<string, string | typeof BaseEventSource>;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

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)."

Suggested change
eventSources: Record<string, string | typeof BaseEventSource>;
eventSources: Record<string, string | (new (...args: any[]) => BaseEventSource)>;

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +3
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';
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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';

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.00%. Comparing base (490f849) to head (2c971d4).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants