Skip to content
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

Add support for import defer proposal #60757

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12074,7 +12074,7 @@ export function forEachDynamicImportOrRequireCall<IncludeTypeSpaceImports extend
cb: (node: CallExpression | (IncludeTypeSpaceImports extends false ? never : JSDocImportTag | ImportTypeNode), argument: RequireStringLiteralLikeArgument extends true ? StringLiteralLike : Expression) => void,
): void {
const isJavaScriptFile = isInJSFile(file);
const r = /import|require/g;
const r = /import|defer|require/g;
while (r.exec(file.text) !== null) { // eslint-disable-line no-restricted-syntax
const node = getNodeAtPosition(file, r.lastIndex, /*includeJSDoc*/ includeTypeSpaceImports);
if (isJavaScriptFile && isRequireCall(node, requireStringLiteralLikeArgument)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ b.ts(1,1): error TS18060: Deferred imports are only supported when the '--module
!!! error TS18060: Deferred imports are only supported when the '--module' flag is set to 'esnext' or 'nodenext'.
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a


//// [a.js]
Expand All @@ -24,4 +22,3 @@ function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});
Promise.resolve().then(() => require("./a.js")); // TODO: Without this the import.defer cannot resolve ./a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>"./a.js" : Symbol("a", Decl(a.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>import("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>"./a.js" : "./a.js"
> : ^^^^^^^^

Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
b.ts(1,1): error TS18060: Deferred imports are only supported when the '--module' flag is set to 'esnext' or 'nodenext'.
b.ts(5,1): error TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', 'node18', or 'nodenext'.


==== a.ts (0 errors) ====
export function foo() {
console.log("foo from a");
}

==== b.ts (2 errors) ====
==== b.ts (1 errors) ====
import.defer("./a.js").then(ns => {
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS18060: Deferred imports are only supported when the '--module' flag is set to 'esnext' or 'nodenext'.
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
~~~~~~~~~~~~~~~~
!!! error TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', 'node18', or 'nodenext'.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a


//// [a.js]
Expand All @@ -21,4 +19,3 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});
import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>"./a.js" : Symbol("a", Decl(a.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>import("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>"./a.js" : "./a.js"
> : ^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ b.ts(1,1): error TS18060: Deferred imports are only supported when the '--module
!!! error TS18060: Deferred imports are only supported when the '--module' flag is set to 'esnext' or 'nodenext'.
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a


//// [a.js]
Expand All @@ -21,4 +19,3 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});
import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>"./a.js" : Symbol("a", Decl(a.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>import("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>"./a.js" : "./a.js"
> : ^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a


//// [a.js]
Expand All @@ -21,4 +19,3 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});
import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>"./a.js" : Symbol("a", Decl(a.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>import("./a.js") : Promise<typeof import("a")>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>"./a.js" : "./a.js"
> : ^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a


//// [a.js]
Expand All @@ -26,4 +24,3 @@ Object.defineProperty(exports, "__esModule", { value: true });
import.defer("./a.js").then(ns => {
ns.foo();
});
import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>"./a.js" : Symbol("a", Decl(a.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ import.defer("./a.js").then(ns => {

});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
>import("./a.js") : Promise<{ default: typeof import("a"); foo(): void; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>"./a.js" : "./a.js"
> : ^^^^^^^^

2 changes: 0 additions & 2 deletions tests/cases/conformance/importDefer/dynamicImportDefer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export function foo() {
import.defer("./a.js").then(ns => {
ns.foo();
});

import("./a.js"); // TODO: Without this the import.defer cannot resolve ./a
Loading