Repository navigation
fix: use static require calls in PlatformTools.load for bundler compatibility - #12729
Conversation
Code Review by Qodo
1. Single-driver applications cannot bundle
|
|
This repository was recently restructured into a pnpm workspace, and the If your branch has diverged enough that a full rebase isn't worth it, Sorry for the disruption, and thanks for your patience. |
…ompatibility PR typeorm#12647 replaced explicit require() calls with dynamic require(name), which is invisible to static analyzers used by bundlers (Webpack, Vite, esbuild). This caused "pg package not found" errors because the bundler did not know to include the database driver dependency. Replaced dynamic require(name) with an explicit switch/case containing static require() calls for each known module (pg, mysql2, mongodb, etc.). This allows bundlers to statically analyze and include the required dependencies while preserving the global-install fallback resolution. Closes typeorm#12721
Verify that unknown modules are rejected with a TypeError and that all supported driver modules are covered by the static switch/case statement. This ensures bundlers can statically analyze the require() calls. Closes typeorm#12721
2eb23ad to
aa63810
Compare
commit: |
|
/agentic_review |
Problem
PR #12647 replaced explicit
require()calls inPlatformTools.load()with a dynamicrequire(name), which is invisible to static analyzers used by bundlers (Webpack, Vite, esbuild, etc.).This causes errors like:
...because the bundler does not know to include the database driver dependency in the bundle.
Fixes #12721
Root Cause
Before PR #12647,
PlatformTools.load()used explicitrequire("pg"),require("mysql2")calls that bundlers could statically analyze. After the PR, it was changed torequire(name)wherenameis a runtime variable — invisible to static analysis.When the bundler can't see which modules are needed, it excludes them from the bundle. The runtime check
if (!KNOWN_MODULES.includes(name))validates the name but doesn't help the bundler.Solution
Replaced the dynamic
require(name)with an explicitswitch/casecontaining staticrequire()calls for each known module. Each case has a literal string argument torequire(), which bundlers can analyze.The catch-all fallback preserves the global-install resolution path (when typeorm is installed globally but drivers are in the project).