feat(48702): Emit for dynamic import (import()) when target >= ES2020 and module == None (#50942)

This commit is contained in:
Oleksandr T 2023-02-07 02:18:06 +02:00 committed by GitHub
parent 76c23e43da
commit cdeb1e3660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 0 deletions

View File

@ -850,6 +850,9 @@ export function transformModule(context: TransformationContext): (x: SourceFile
}
function visitImportCallExpression(node: ImportCall): Expression {
if (moduleKind === ModuleKind.None && languageVersion >= ScriptTarget.ES2020) {
return visitEachChild(node, visitor, context);
}
const externalModuleName = getExternalModuleNameLiteral(factory, node, currentSourceFile, host, resolver, compilerOptions);
const firstArgument = visitNode(firstOrUndefined(node.arguments), visitor, isExpression);
// 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.

View File

@ -0,0 +1,11 @@
//// [tests/cases/compiler/moduleNoneDynamicImport.ts] ////
//// [a.ts]
const foo = import("./b");
//// [b.js]
export default 1;
//// [a.js]
const foo = Promise.resolve().then(() => require("b"));

View File

@ -0,0 +1,9 @@
=== /a.ts ===
const foo = import("./b");
>foo : Symbol(foo, Decl(a.ts, 0, 5))
>"./b" : Symbol("/b", Decl(b.js, 0, 0))
=== /b.js ===
export default 1;

View File

@ -0,0 +1,10 @@
=== /a.ts ===
const foo = import("./b");
>foo : Promise<typeof import("/b")>
>import("./b") : Promise<typeof import("/b")>
>"./b" : "./b"
=== /b.js ===
export default 1;

View File

@ -0,0 +1,11 @@
//// [tests/cases/compiler/moduleNoneDynamicImport.ts] ////
//// [a.ts]
const foo = import("./b");
//// [b.js]
export default 1;
//// [a.js]
const foo = import("./b");

View File

@ -0,0 +1,9 @@
=== /a.ts ===
const foo = import("./b");
>foo : Symbol(foo, Decl(a.ts, 0, 5))
>"./b" : Symbol("/b", Decl(b.js, 0, 0))
=== /b.js ===
export default 1;

View File

@ -0,0 +1,10 @@
=== /a.ts ===
const foo = import("./b");
>foo : Promise<typeof import("/b")>
>import("./b") : Promise<typeof import("/b")>
>"./b" : "./b"
=== /b.js ===
export default 1;

View File

@ -0,0 +1,9 @@
// @allowJs: true
// @target: es2015,es2020
// @module: none
// @outFile: /a.js
// @filename: /a.ts
const foo = import("./b");
// @filename: /b.js
export default 1;