feat: auto-register ESM loader hooks in TuskDrift.initialize()#146
feat: auto-register ESM loader hooks in TuskDrift.initialize()#146sohankshirsagar merged 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
1 issue found across 17 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/core/esmLoader.ts">
<violation number="1" location="src/core/esmLoader.ts:44">
P1: Set the `__tuskDriftEsmLoaderRegistered` flag only after successful registration (or reset it in `catch`), otherwise one transient failure permanently disables future registration attempts.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Generated 14 tests - 14 passedTip New to Tusk Unit Tests? Learn more here. Test Summary
ResultsTusk's tests all pass cleanly. The test suite validates the core changes: View check history
Was Tusk helpful? Give feedback by reacting with 👍 or 👎 |


tuskDriftInit.tsSummary
ESM users previously had to manually call
register('@use-tusk/drift-node-sdk/hook.mjs', pathToFileURL('./'))in their init file before importing the SDK. This was an extra step that CJS users didn't need, and was easy to forget.This PR auto-registers ESM loader hooks inside
TuskDrift.initialize(), matching. The ESM init file is now identical to CJS. The--importflag is still required for ESM applications. With CJS we can control the import order in the application code, but in ESM imports are evaluated before import module's code runs so callingTuskDrift.initialize()doesn't always guarantee it runs before the rest of the import graphChanges
src/core/esmLoader.ts:initializeEsmLoader()usescreateAddHookMessageChannel()fromimport-in-the-middleandmodule.register()to set up ESM loader hooks automatically. Guarded by CJS detection, Node version check (>= 18.19 / >= 20.6), and a global flag to prevent double-registration.registerEsmLoaderHooksoption inInitParams: Defaults totrue. Users can set tofalseto disable auto-registration ifimport-in-the-middlecauses issues with specific packages.TuskDrift.initialize()callsinitializeEsmLoader()early, beforeregisterDefaultInstrumentations(), gated onregisterEsmLoaderHooks !== false.isCommonJS()tosrc/core/utils/runtimeDetectionUtils.ts: Shared betweenTuskDrift.tsandesmLoader.tsinstead of duplicated. Removed the private method fromTuskDriftCore.tdInit.tsfiles: Removed manualregister()calls — they are now identical to their CJS counterparts.docs/initialization.md: Collapsed ESM/CJS init file examples into one, moved "Determining Your Module System" to the entry point section (still needed for--import), addedregisterEsmLoaderHooksto the params table, added "Troubleshooting ESM" section documenting the opt-out and common failure modes.hook.mjskept for backward compatibility: Existing users with manualregister()calls will continue to work.Notes
isCommonJS()returns early, no ESM hooks are registered.