Description
Suggestion
π Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
dynamic import, commonjs, esm, node
β 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 a tsconfig flag to not transpile dynamic imports when module is CommonJS. Something like transpileDynamicImport
which would be true
by default and only take effect when "module": "CommonJS"
.
π Motivating Example
Since Node 12, it is possible to use dynamic imports. Yet I'm not able to tell the TS compiler to not transpile this into Promise.resolve().then(() => __importStar(require('..')));
. This prevents users from importing ES modules into CommonJS, which will become increasingly common now that Node is transitioning to ES modules.
π» Use Cases
The main use case is to be able to import ES modules into CommonJS, which is possible to do with import()
. The workaround today involves an additional build step which does replacements to hide the import()
statement from TS so it does not get transpiled and re-add it later on, which is suboptimal.