fix(rivetkit): emit CJS-safe import.meta.url so CommonJS consumers can load rivetkit#5379
Draft
khaled-mansour-zid wants to merge 1 commit into
Draft
Conversation
…n load rivetkit getRequireFn() (src/utils/node.ts) uses createRequire(import.meta.url), but the tsup build sets shims:false (to keep ESM shims out of the browser build), so the CJS output keeps a verbatim import.meta.url — a SyntaxError at load for any CommonJS consumer (e.g. importing rivetkit under ts-node). Define a CJS-safe replacement for the cjs format only; ESM is untouched. Addresses the existing 'TODO: This causes issues in tsup' in getRequireFn.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rivetkit's CJS build ships a verbatimimport.meta.url, which is a load-time SyntaxError in any CommonJS consumer (e.g. importingrivetkitunderts-node/ a CJS host process).Root cause
getRequireFn()inrivetkit-typescript/packages/rivetkit/src/utils/node.tsusescreateRequire(import.meta.url)(and carries a// TODO: This causes issues in tsupwith the CJSrequirebranch commented out). The package'stsup.config.tsdeliberately setsshims: false— correctly, since the ESM shims pull in Node-only modules that break the browser build (rivetkit/client). But with shims off, tsup emitsimport.meta.urlverbatim into the.cjsoutput, so:fails to load in CommonJS.
Fix
Keep
shims: false(don't regress the browser build) and instead define a CJS-safe replacement forimport.meta.urlonly for thecjsformat, via tsup's format-awareesbuildOptions(options, context):ESM output is untouched (keeps native
import.meta.url); only the CJS bundle gets the load-safe form. Single-file change totsup.config.ts.Notes
require('rivetkit')from a CommonJS module against the current published build throws onimport.meta.@rivetkit/*subpackages declare arequireexport condition but don't ship.cjs) — happy to follow up separately; this PR fixes the mainrivetkitentry.