feat(utils): consume __EGG_MODULE_IMPORTER__ in importModule#5992
Conversation
Follow-up to #5989 (which added the `ModuleImporter` type/global and the @eggjs/tegg-loader consumer). Extend the same async module-importer override to egg-core's module/boot-file loading via `importModule`: when a test runner sets `globalThis.__EGG_MODULE_IMPORTER__`, app boot files (app.ts, app/extend/*.ts) and config load through the runner's module graph too, so enums/decorators in boot files transpile and the whole app shares one module realm with the test (matching how the tegg loader already routes tegg modules). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthrough
ChangesGlobal EGG_MODULE_IMPORTER hook in importModule
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 introduces an async module importer override via globalThis.__EGG_MODULE_IMPORTER__ to intercept module loading, which is useful for test runners like Vitest. It also adds comprehensive unit tests to verify this behavior. The review feedback suggests simplifying the module unwrapping logic in import.ts to reduce redundant checks and improve readability.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
Extends @eggjs/utils’s importModule() to optionally delegate module loading to a global async importer hook (globalThis.__EGG_MODULE_IMPORTER__), enabling test runners/bundlers (e.g. Vitest) to load boot/config files through a single module graph/realm. Adds a dedicated utils test to validate the new interception and export-normalization behavior.
Changes:
- Add
__EGG_MODULE_IMPORTER__override path toimportModule()with export normalization (__esModuledouble-default +importDefaultOnly). - Add Vitest coverage validating importer interception, normalization, and precedence over native dynamic import.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/utils/src/import.ts | Adds importer-hook interception to importModule() before native import()/require(). |
| packages/utils/test/module-importer.test.ts | Adds tests covering importer interception, normalization, and precedence. |
Deploying egg with
|
| Latest commit: |
e06638c
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b8999730.egg-cci.pages.dev |
| Branch Preview URL: | https://feat-utils-module-importer.egg-cci.pages.dev |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/utils/src/import.ts`:
- Around line 507-516: Normalize the module path before calling the global
importer hook in importModule so Windows callers don’t pass a different key than
tegg modules. Update the _moduleImporter(moduleFilePath) path in
packages/utils/src/import.ts to convert the path to a POSIX-style module
identifier first, keeping the existing default-unwrapping logic in place. Use
the importModule and _moduleImporter symbols to locate the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1d3323fe-9228-4b09-95ab-382b56c25d31
📒 Files selected for processing (2)
packages/utils/src/import.tspackages/utils/test/module-importer.test.ts
Deploying egg-v3 with
|
| Latest commit: |
e06638c
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://fa864473.egg-v3.pages.dev |
| Branch Preview URL: | https://feat-utils-module-importer.egg-v3.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #5992 +/- ##
=======================================
Coverage 85.94% 85.95%
=======================================
Files 669 669
Lines 19930 19938 +8
Branches 3962 3965 +3
=======================================
+ Hits 17129 17137 +8
Misses 2423 2423
Partials 378 378 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Re-opened from a same-repo branch so CodeQL code scanning runs (the fork-based #5990 was permanently
BLOCKEDwaiting on CodeQL results that never arrive for fork PRs). Supersedes #5990.What
Follow-up to #5989. That PR added the
ModuleImportertype +__EGG_MODULE_IMPORTER__global and made@eggjs/tegg-loaderconsume it. This extends the same hook to egg-core's module/boot-file loading in@eggjs/utilsimportModule():Why
importModule()loads plugin boot files (app.ts,app/extend/*.ts) and config. Under a bundler-based test runner (Vitest) the worker thread loads these via nativeimport(), which can't transpile TSenum/decorators in boot files and puts them in a different module realm than the test. RoutingimportModulethrough the same__EGG_MODULE_IMPORTER__makes boot files, config, and tegg modules resolve through one module graph — which is what lets a downstream tegg distribution (externalized published packages) boot its app fixtures and runctx.getEggObject(ImportedClass)against a single class instance.__EGG_BUNDLE_MODULE_LOADER__/_snapshotModuleLoaderand the@eggjs/tegg-loaderconsumer from feat(tegg-loader): support async module importer override (__EGG_MODULE_IMPORTER__) #5989.ModuleImportertype + global are already onnext.Test
Adds
packages/utils/test/module-importer.test.ts(load through importer / importDefaultOnly / __esModule unwrap / precedence over native import).🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests