Skip to content
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

Support for transpiling imports with assert {type: "json"} to use the old native way of node:fs ? #744

Closed
renhiyama opened this issue Sep 11, 2022 · 5 comments · Fixed by #746

Comments

@renhiyama
Copy link

Consider looking at this code: https://deno.land/x/emoji@0.2.1/emoji.ts?source
It has assert, which crashes sucrase:
image

The 4th line has assert.

@alangpierce
Copy link
Owner

@renhiyama thanks for the report! I filed a tracking issue a while back as #566 , but I know this syntax is becoming more common, so definitely seems time to add better support. Could you clarify more specifically what you mean by "the old native way of node:fs"? Is there another transpiler that has the behavior you want that I could model the implementation off of?

I think the behavior I'd be most comfortable with is this:

  • When targeting CJS (when imports is specified as a transform), the import becomes a require as usual and the assert {type: "json"} is ignored/removed. This should work with node because require handles JSON files automatically.
  • When targeting ESM (when imports is not specified as a transform), the import assertion is passed through as-is, so the output code has the assert { type: "json" };. I just confirmed that this works on node 16+ when using node's native ESM support.

Would that work for your use case?

@renhiyama
Copy link
Author

Oh i didn't knew it was supported in nodejs v16! Yea then we can continue with what you said above! (I didn't test a json import but I remember doing it last year) looking forward for the fix since a lot of deno modules use that and I need a way to get them too!

alangpierce added a commit that referenced this issue Sep 14, 2022
Fixes #744
Fixes #566

In ESM mode, we parse import assertions and usually just pass them through to
the output code, though TS import elision needs to know how to remove them as
well.

In CJS mode, we always remove import assertions since `require` can handle JSON
files.
alangpierce added a commit that referenced this issue Sep 15, 2022
Fixes #744
Fixes #566

In ESM mode, we parse import assertions and usually just pass them through to
the output code, though TS import elision needs to know how to remove them as
well.

In CJS mode, we always remove import assertions since `require` can handle JSON
files.
@renhiyama
Copy link
Author

hey @alangpierce , when are you publishing a new version of sucrase so I can start using it in my projects?

@alangpierce
Copy link
Owner

Just released as 3.27.0, let me know if you see issues!

@renhiyama
Copy link
Author

Worked!
image
Cheers 🎉

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 a pull request may close this issue.

2 participants