Skip to content

Commit cdeb1e3

Browse files
authored
feat(48702): Emit for dynamic import (import()) when target >= ES2020 and module == None (#50942)
1 parent 76c23e4 commit cdeb1e3

8 files changed

+72
-0
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ export function transformModule(context: TransformationContext): (x: SourceFile
850850
}
851851

852852
function visitImportCallExpression(node: ImportCall): Expression {
853+
if (moduleKind === ModuleKind.None && languageVersion >= ScriptTarget.ES2020) {
854+
return visitEachChild(node, visitor, context);
855+
}
853856
const externalModuleName = getExternalModuleNameLiteral(factory, node, currentSourceFile, host, resolver, compilerOptions);
854857
const firstArgument = visitNode(firstOrUndefined(node.arguments), visitor, isExpression);
855858
// Only use the external module name if it differs from the first argument. This allows us to preserve the quote style of the argument on output.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/moduleNoneDynamicImport.ts] ////
2+
3+
//// [a.ts]
4+
const foo = import("./b");
5+
6+
//// [b.js]
7+
export default 1;
8+
9+
10+
//// [a.js]
11+
const foo = Promise.resolve().then(() => require("b"));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== /a.ts ===
2+
const foo = import("./b");
3+
>foo : Symbol(foo, Decl(a.ts, 0, 5))
4+
>"./b" : Symbol("/b", Decl(b.js, 0, 0))
5+
6+
=== /b.js ===
7+
8+
export default 1;
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== /a.ts ===
2+
const foo = import("./b");
3+
>foo : Promise<typeof import("/b")>
4+
>import("./b") : Promise<typeof import("/b")>
5+
>"./b" : "./b"
6+
7+
=== /b.js ===
8+
9+
export default 1;
10+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/moduleNoneDynamicImport.ts] ////
2+
3+
//// [a.ts]
4+
const foo = import("./b");
5+
6+
//// [b.js]
7+
export default 1;
8+
9+
10+
//// [a.js]
11+
const foo = import("./b");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== /a.ts ===
2+
const foo = import("./b");
3+
>foo : Symbol(foo, Decl(a.ts, 0, 5))
4+
>"./b" : Symbol("/b", Decl(b.js, 0, 0))
5+
6+
=== /b.js ===
7+
8+
export default 1;
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== /a.ts ===
2+
const foo = import("./b");
3+
>foo : Promise<typeof import("/b")>
4+
>import("./b") : Promise<typeof import("/b")>
5+
>"./b" : "./b"
6+
7+
=== /b.js ===
8+
9+
export default 1;
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @allowJs: true
2+
// @target: es2015,es2020
3+
// @module: none
4+
// @outFile: /a.js
5+
// @filename: /a.ts
6+
const foo = import("./b");
7+
8+
// @filename: /b.js
9+
export default 1;

0 commit comments

Comments
 (0)