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

Dynamic import not act as expected as typescript transpiled #778

Closed
miserylee opened this issue Feb 9, 2023 · 1 comment · Fixed by #789
Closed

Dynamic import not act as expected as typescript transpiled #778

miserylee opened this issue Feb 9, 2023 · 1 comment · Fixed by #789

Comments

@miserylee
Copy link

Here is the example:

https://sucrase.io/#compareWithBabel=false&compareWithTypeScript=true&code=import%20b%20from%20'a'%3B%0A%0Aasync%20function%20a%20()%20%7B%0A%20%20console.log(b)%3B%0A%20%20const%20c%20%3D%20await%20import('a')%3B%0A%20%20console.log(c)%3B%0A%20%20console.log(c.default)%3B%0A%7D

While static import is transpiled to use _interopRequireDefault, but dynamic import is not transpiled to use __importStar, so in runtime c.default is not defined.

alangpierce added a commit that referenced this issue Mar 26, 2023
Fixes #778

Previously, `import()` transpiled to a promise-wrapped `require`, which worked
fine for ESM-to-CJS-transpiled modules, but when importing plain CJS modules,
the behavior was inconsistent with other implementations. In that case, we
needed to call `_interopRequireWildcard` to nest the module under a `default`
key.

I tested to confirm that this updated behavior is consistent with Node
ESM-to-CJS dynamic import, as well as Babel, TypeScript, and swc, so this change
will be considered a bug fix rather than a breaking change, even though it is
possible that existing use cases may have been relying on the old behavior.
alangpierce added a commit that referenced this issue Mar 26, 2023
Fixes #778

Previously, `import()` transpiled to a promise-wrapped `require`, which worked
fine for ESM-to-CJS-transpiled modules, but when importing plain CJS modules,
the behavior was inconsistent with other implementations. In that case, we
needed to call `_interopRequireWildcard` to nest the module under a `default`
key.

I tested to confirm that this updated behavior is consistent with Node
ESM-to-CJS dynamic import, as well as Babel, TypeScript, and swc, so this change
will be considered a bug fix rather than a breaking change, even though it is
possible that existing use cases may have been relying on the old behavior.
@alangpierce
Copy link
Owner

Thanks for reporting! I just released a fix in 3.31.0.

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