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

Add flag to preserve dynamic import #727

Merged
merged 1 commit into from Jul 25, 2022

Conversation

alangpierce
Copy link
Owner

Progress toward #726

This PR adds a new preserveDynamicImport flag that, when enabled, changes the
imports transform so that only static imports are transformed, while dynamic
import() expressions are passed through as-is.

This behavior matches what TypeScript does when handling a .cjs file when
module: nodenext is specified, and ts-node expects transpilers to handle this
mode, so this PR is a necessary step toward Sucrase as a ts-node plugin.

This mode also seems like the best interpretation of import() in new code
these days; if import() is transpiled, then it's impossible to access ESM
code from CJS in Node, so leaving it alone allows a mechanism to import
ESM-only libraries. My tentative plan is to change this mode to be the default
behavior in an upcoming semver-major release.

Progress toward #726

This PR adds a new `preserveDynamicImport` flag that, when enabled, changes the
`imports` transform so that only static `import`s are transformed, while dynamic
`import()` expressions are passed through as-is.

This behavior matches what TypeScript does when handling a `.cjs` file when
`module: nodenext` is specified, and ts-node expects transpilers to handle this
mode, so this PR is a necessary step toward Sucrase as a ts-node plugin.

This mode also seems like the best interpretation of `import()` in new code
these days; if `import()` is transpiled, then it's impossible to access ESM
code from CJS. My tentative plan is to change this mode to be the default
behavior in an upcoming semver-major release.
@codecov
Copy link

codecov bot commented Jul 25, 2022

Codecov Report

Merging #727 (c9b3fba) into main (5268e6c) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #727   +/-   ##
=======================================
  Coverage   87.15%   87.16%           
=======================================
  Files          54       54           
  Lines        5708     5711    +3     
  Branches     1345     1346    +1     
=======================================
+ Hits         4975     4978    +3     
  Misses        466      466           
  Partials      267      267           
Impacted Files Coverage Δ
src/Options-gen-types.ts 100.00% <ø> (ø)
src/Options.ts 100.00% <ø> (ø)
src/transformers/RootTransformer.ts 93.65% <ø> (ø)
src/transformers/CJSImportTransformer.ts 88.41% <100.00%> (+0.08%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@github-actions
Copy link

github-actions bot commented Jul 25, 2022

Benchmark results

Before this PR: 313.1 thousand lines per second
After this PR: 315.6 thousand lines per second

Measured change: 0.8% faster (0.18% faster to 1.88% faster)
Summary: Probably faster

@alangpierce alangpierce merged commit 8a5fdf8 into main Jul 25, 2022
@alangpierce alangpierce deleted the add-preserve-dynamic-import-flag branch July 25, 2022 07:36
1Lighty pushed a commit to Astra-mod/sucrase that referenced this pull request Aug 14, 2022
Progress toward alangpierce#726

This PR adds a new `preserveDynamicImport` flag that, when enabled, changes the
`imports` transform so that only static `import`s are transformed, while dynamic
`import()` expressions are passed through as-is.

This behavior matches what TypeScript does when handling a `.cjs` file when
`module: nodenext` is specified, and ts-node expects transpilers to handle this
mode, so this PR is a necessary step toward Sucrase as a ts-node plugin.

This mode also seems like the best interpretation of `import()` in new code
these days; if `import()` is transpiled, then it's impossible to access ESM
code from CJS in Node, so leaving it alone allows a mechanism to import
ESM-only libraries. My tentative plan is to change this mode to be the default
behavior in an upcoming semver-major release.
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.

None yet

1 participant