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

Fix dynamic import on plain CJS files #789

Merged
merged 1 commit into from Mar 26, 2023
Merged

Conversation

alangpierce
Copy link
Owner

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.

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.
@codecov
Copy link

codecov bot commented Mar 26, 2023

Codecov Report

Merging #789 (c59729c) into main (5636395) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #789   +/-   ##
=======================================
  Coverage   88.32%   88.32%           
=======================================
  Files          55       55           
  Lines        5954     5955    +1     
  Branches     1413     1413           
=======================================
+ Hits         5259     5260    +1     
  Misses        430      430           
  Partials      265      265           
Impacted Files Coverage Δ
src/transformers/RootTransformer.ts 93.68% <ø> (ø)
src/transformers/CJSImportTransformer.ts 89.59% <100.00%> (+0.02%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link

Benchmark results

Before this PR: 308 thousand lines per second
After this PR: 300.8 thousand lines per second

Measured change: 2.34% slower (3.93% slower to 0.07% faster)
Summary: Likely no significant difference

@alangpierce alangpierce merged commit 9081e43 into main Mar 26, 2023
8 checks passed
@alangpierce alangpierce deleted the fix-dynamic-import-cjs branch March 26, 2023 19:28
alangpierce added a commit that referenced this pull request Mar 27, 2023
In #789, I changed dynamic `import()` to use `_interopRequireWildcard`. However,
after some more testing, I realized that `enableLegacyTypeScriptModuleInterop`
mode should *not* do this, since it's an older interpretation of interop that
generally doesn't use these helpers. This PR adds that as a new special case.
alangpierce added a commit that referenced this pull request Mar 27, 2023
In #789, I changed dynamic `import()` to use `_interopRequireWildcard`. However,
after some more testing, I realized that `enableLegacyTypeScriptModuleInterop`
mode should *not* do this, since it's an older interpretation of interop that
generally doesn't use these helpers. This PR adds that as a new special case.
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 this pull request may close these issues.

Dynamic import not act as expected as typescript transpiled
1 participant