Skip to content

feat: add shared egg typings package#5895

Closed
killagu wants to merge 1 commit intonextfrom
egg-15-typings-bundle-loader
Closed

feat: add shared egg typings package#5895
killagu wants to merge 1 commit intonextfrom
egg-15-typings-bundle-loader

Conversation

@killagu
Copy link
Copy Markdown
Contributor

@killagu killagu commented Apr 26, 2026

Summary

  • add new workspace package @eggjs/typings for shared BundleModuleLoader/global typing
  • make @eggjs/utils consume BundleModuleLoader from @eggjs/typings and keep the bundle loader registry on globalThis
  • wire @eggjs/core to the shared typings package and add bundle import coverage

Closes EGG-15.

Local validation

  • pnpm -r --if-present run typecheck passed
  • pnpm exec oxlint --type-aware --type-check --quiet passed (274 existing warnings, 0 errors)
  • pnpm exec oxfmt --check . passed
  • pnpm exec vitest run packages/utils/test/bundle-import.test.ts --bail 1 --retry 2 --testTimeout 20000 --hookTimeout 20000 passed
  • pnpm exec vitest run packages/utils/test/import.test.ts packages/utils/test/index.test.ts --bail 1 --retry 2 --testTimeout 20000 --hookTimeout 20000 passed
  • pnpm --filter @eggjs/typings exec tsdown passed
  • pnpm --filter @eggjs/utils exec tsdown passed
  • pnpm --filter @eggjs/core exec tsdown passed

Notes

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 26, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 92d8989f-77c2-4c7a-8b6a-cab4d67e5dbe

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch egg-15-typings-bundle-loader

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.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0d1f1eb
Status: ✅  Deploy successful!
Preview URL: https://3fe164e0.egg-cci.pages.dev
Branch Preview URL: https://egg-15-typings-bundle-loader.egg-cci.pages.dev

View logs

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.50%. Comparing base (0b6c9fb) to head (0d1f1eb).
⚠️ Report is 44 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #5895      +/-   ##
==========================================
- Coverage   85.52%   85.50%   -0.02%     
==========================================
  Files         662      662              
  Lines       18863    18875      +12     
  Branches     3658     3662       +4     
==========================================
+ Hits        16132    16140       +8     
- Misses       2360     2362       +2     
- Partials      371      373       +2     

☔ 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.

@socket-security
Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedtypescript@​5.9.3100100909990
Addedoxlint@​1.61.0991009196100
Addedoxlint-tsgolint@​0.11.51001009796100

View full report

@socket-security
Copy link
Copy Markdown

Warning

Review the following alerts detected in dependencies.

According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.

Action Severity Alert  (click "▶" to expand/collapse)
Warn High
Obfuscated code: npm js-beautify is 100.0% likely obfuscated

Confidence: 1.00

Location: Package overview

From: ?npm/@eggjs/tegg@3.78.15npm/js-beautify@1.15.4

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support@socket.dev.

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/js-beautify@1.15.4. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0d1f1eb
Status: ✅  Deploy successful!
Preview URL: https://dac41da1.egg-v3.pages.dev
Branch Preview URL: https://egg-15-typings-bundle-loader.egg-v3.pages.dev

View logs

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 introduces the @eggjs/typings package and implements a global bundle module loader within @eggjs/utils to support bundled applications. The importModule function is updated to check for a registered loader before proceeding with standard imports, including logic for path normalization and module unwrapping. Review feedback suggests extending the BundleModuleLoader type to accept options for better manifest verification in multi-application environments and refactoring the module unwrapping logic to reduce duplication and improve maintainability.

* serve modules that no longer exist on disk. Return `undefined` to fall
* through to the standard import path.
*/
export type BundleModuleLoader = (filepath: string) => unknown;
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.

medium

The BundleModuleLoader currently only receives the raw filepath. In multi-application environments, it is important to verify that the global bundle store's baseDir matches the requested baseDir to prevent using an incorrect manifest. Consider passing options (including baseDir) to the loader to facilitate this check.

Suggested change
export type BundleModuleLoader = (filepath: string) => unknown;
export type BundleModuleLoader = (filepath: string, options?: { baseDir?: string; [key: string]: any }) => unknown;
References
  1. When using a global bundle store, verify that its baseDir matches the requested baseDir to prevent using an incorrect manifest in multi-application environments.

Comment on lines +412 to +423

export async function importModule(filepath: string, options?: ImportModuleOptions): Promise<any> {
const _bundleModuleLoader = globalThis.__EGG_BUNDLE_MODULE_LOADER__;
if (_bundleModuleLoader) {
const hit = _bundleModuleLoader(normalizeBundleModulePath(filepath));
if (hit !== undefined) {
let obj = hit as any;
if (obj?.default?.__esModule === true && 'default' in obj.default) obj = obj.default;
if (options?.importDefaultOnly && obj && typeof obj === 'object' && 'default' in obj) obj = obj.default;
return obj;
}
}
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.

medium

The logic for unwrapping __esModule "double default" and handling importDefaultOnly is duplicated multiple times. Introducing a private helper function improves maintainability and ensures consistency. Additionally, passing options to the bundle loader allows it to verify the baseDir against the global bundle store, ensuring the correct manifest is used in multi-application environments.

function unwrapModule(obj: any, options?: ImportModuleOptions): any {
  if (obj?.default?.__esModule === true && 'default' in obj.default) {
    obj = obj.default;
  }
  if (options?.importDefaultOnly && obj && typeof obj === 'object' && 'default' in obj) {
    obj = obj.default;
  }
  return obj;
}

export async function importModule(filepath: string, options?: ImportModuleOptions): Promise<any> {
  const _bundleModuleLoader = globalThis.__EGG_BUNDLE_MODULE_LOADER__;
  if (_bundleModuleLoader) {
    const hit = _bundleModuleLoader(normalizeBundleModulePath(filepath), options);
    if (hit !== undefined) {
      return unwrapModule(hit, options);
    }
  }
References
  1. When using a global bundle store, verify that its baseDir matches the requested baseDir to prevent using an incorrect manifest in multi-application environments.

@killagu killagu closed this May 3, 2026
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.

1 participant