forked from alangpierce/sucrase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flag to emit createRequire matching TS nodenext behavior (alangpi…
…erce#728) Progress toward alangpierce#726 This is currently an opt-in flag handling a nuance in how TS transpiles imports like these: ```ts import foo = require('foo'); ``` In the new nodenext mode when targeting ESM, TS now transforms this code to use `createRequire`. The change is described here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#commonjs-interoperability This PR adds a flag `injectCreateRequireForImportRequire` to enable this different behavior. I'm gating this behind a flag out of caution, though it's worth noting that TS gives an error when using this syntax and targeting module esnext, so it will likely be safe to switch to this new emit strategy as the default behavior in the future. As I understand it, the main benefit of this change over explicit `createRequire` is that it allows a single codebase to be transpiled to Node ESM and Node CJS while using this syntax. A downside is that `createRequire` is Node-specific and needs special support from bundlers, but it looks like webpack can recognize the pattern. In most situations, real ESM `import` syntax is probably preferable, but this syntax makes it possible to force the use of CJS. The ts-node transpiler plugin system expects transpilers to have this mode as an option, so this is a step closer to having a compliant ts-node plugin.
- Loading branch information
1 parent
38db7dc
commit 218b152
Showing
8 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters