diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b3554876c14..5f7771d02ea 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2163,6 +2163,8 @@ namespace ts { return bindJsxAttribute(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); // Imports and exports + case SyntaxKind.ImportCallExpression: + return bindImportCallExpression(node); case SyntaxKind.ImportEqualsDeclaration: case SyntaxKind.NamespaceImport: case SyntaxKind.ImportSpecifier: @@ -2267,6 +2269,15 @@ namespace ts { } } + function bindImportCallExpression(node: ImportCallExpression) { + if (!file.dynamicImportIndicator) { + file.dynamicImportIndicator = node; + if (!file.externalModuleIndicator) { + bindSourceFileAsExternalModule(); + } + } + } + function bindImportClause(node: ImportClause) { if (node.name) { declareSymbolAndAddToSymbolTable(node, SymbolFlags.Alias, SymbolFlags.AliasExcludes); diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 0bd7989327b..962bbc7e234 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -1644,7 +1644,7 @@ namespace ts { if (node.resolvedTypeReferenceDirectiveNames !== undefined) updated.resolvedTypeReferenceDirectiveNames = node.resolvedTypeReferenceDirectiveNames; if (node.imports !== undefined) updated.imports = node.imports; if (node.moduleAugmentations !== undefined) updated.moduleAugmentations = node.moduleAugmentations; - if (node.containsDynamicImport !== undefined) updated.containsDynamicImport = node.containsDynamicImport; + if (node.dynamicImportIndicator !== undefined) updated.dynamicImportIndicator = node.dynamicImportIndicator; return updateNode(updated, node); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a7427e58134..e4931b09e3c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -461,7 +461,11 @@ namespace ts { } export function isExternalModule(file: SourceFile): boolean { - return file.externalModuleIndicator !== undefined || file.containsDynamicImport; + return file.externalModuleIndicator !== undefined; + } + + export function containedDynamicImport(file: SourceFile): boolean { + return file.dynamicImportIndicator !== undefined; } // Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 9d811243718..6614f1a063e 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -55,7 +55,7 @@ namespace ts { * @param node The SourceFile node. */ function transformSourceFile(node: SourceFile) { - if (isDeclarationFile(node) || !(isExternalModule(node) || compilerOptions.isolatedModules)) { + if (isDeclarationFile(node) || !(isExternalModule(node) || compilerOptions.isolatedModules || containedDynamicImport(node))) { return node; } diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 2798c7ca2cc..32005722e63 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -50,9 +50,7 @@ namespace ts { * @param node The SourceFile node. */ function transformSourceFile(node: SourceFile) { - if (isDeclarationFile(node) - || !(isExternalModule(node) - || compilerOptions.isolatedModules)) { + if (isDeclarationFile(node) || !(isExternalModule(node) || compilerOptions.isolatedModules || containedDynamicImport(node))) { return node; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 07a6610f8fd..21da99c62ef 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2253,6 +2253,7 @@ namespace ts { /* @internal */ externalModuleIndicator: Node; // The first node that causes this file to be a CommonJS module /* @internal */ commonJsModuleIndicator: Node; + /* @internal */ dynamicImportIndicator: Node; /* @internal */ identifiers: Map; /* @internal */ nodeCount: number; @@ -2282,7 +2283,6 @@ namespace ts { /* @internal */ moduleAugmentations: LiteralExpression[]; /* @internal */ patternAmbientModules?: PatternAmbientModule[]; /* @internal */ ambientModuleNames: string[]; - /* @internal */ containsDynamicImport?: boolean; } export interface Bundle extends Node { diff --git a/src/services/services.ts b/src/services/services.ts index df5330ebaed..b0dd1f2eb1e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -458,6 +458,7 @@ namespace ts { public hasNoDefaultLib: boolean; public externalModuleIndicator: Node; // The first node that causes this file to be an external module public commonJsModuleIndicator: Node; // The first node that causes this file to be a CommonJS module + public dynamicImportIndicator: Node; // The first dynamic import in this file public nodeCount: number; public identifierCount: number; public symbolCount: number;