-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit createRequire
for TypeScript import =
to match module: nodenext
#4101
Comments
Related to #4679 I may have some time for this soon; can anyone point me in the right direction to implement this fix? In ts-node I'm hoping to ship support for TS's new nodenext/node16 modes -- mts, mjs, cts, cjs. But this is a blocker since it means the swc backend can't emit correct mts. |
First, you will have to add a module like For the latter, you would need to wrap swc/crates/swc_ecma_transforms_module/src/amd.rs Lines 64 to 72 in 9a027ba
After then, you can add an import with a specifier named
Once you have it, you can add imports to the module, and replace/wrap require calls with createRequire
|
I've hit a snag and need some guidance. This The problem is, typescript stripping runs before the es6 transformation. This means TsImportEqualsDecl does not exist by the time es6 runs. How should the typescript stripping pass tell the es6 pass that a TsImportEqualsDecl was stripped? I thought about the typescript stripping doing the createRequire, but the problem is that for module=Commonjs, we should not do that. |
I believe this feature is already available in swc. |
Thank you! |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the feature
TypeScript's new
module: nodenext
will emitconst require = createRequire()
as necessary in ESM modules that haveimport =
syntax. Without callingcreateRequire()
, the code fails, becauserequire
is not defined in ES modules on nodejs.When compiled by SWC using target=es2022 and module=es modules, I get: (playground link)
However, TypeScript will include a call to
createRequire
: (playground link)Is there any way to tell SWC to emit this
createRequire
call?Babel plugin or link to the feature description
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: