Skip to content

Support a way to use dynamic import expressions within module type commonjs #45125

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

Closed
5 tasks done
spence-s opened this issue Jul 20, 2021 · 3 comments
Closed
5 tasks done
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@spence-s
Copy link

spence-s commented Jul 20, 2021

Suggestion

These days - nodejs supports esm/commonjs interop by allowing dynamic import expressions inside of cjs modules. Currently, this is the only way to import/require pure ESM packages inside of commonjs based projects. Refactoring a project to pure ESM is not always possible. Some popular open source maintainer are forcing use of pure ESM (ie.. @sindrsorhus)

🔍 Search Terms

module modules import require commonjs esm esm interop export default exports await import import( import() dynamic imports

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Add an escape hatch - perhaps by a comment flag or configuration option to allow dynamic imports in project compiling to commonjs.

📃 Motivating Example

Set compilerOptions.module to "commonjs"

case:1
// error
import meow from 'meow';

case:2
// currently an error
(async () => { const meow = await import('meow') });
compiles to something like const meow = await Promise.resolve(require('meow'))

// after adding a new option to allow dynamic imports case2 would not transform dynamic import expressions while import statements would compile to require.

(async () => { const meow = await import('meow') });

💻 Use Cases

This would allow to use TS using current Node.JS capabilities and native modules more naturally as well as support more libraries for NodeJs without having to use something like webpack to achieve.

@andrewbranch
Copy link
Member

@weswigham I assume this is already handled in one of your recently merged or soon-to-be-merged --module=node12 PRs?

@andrewbranch andrewbranch added the Needs Investigation This issue needs a team member to investigate its status. label Jul 21, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.0 milestone Jul 21, 2021
@andrewbranch
Copy link
Member

Assigning and milestoning for now, but I’m pretty sure this is already done or in the works

@weswigham
Copy link
Member

Aye - the node12 module mode as is currently in PR supports this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants