@@ -721,7 +721,7 @@ namespace ts {
721721 return createImportCallExpressionUMD ( argument ?? factory . createVoidZero ( ) , containsLexicalThis ) ;
722722 case ModuleKind . CommonJS :
723723 default :
724- return createImportCallExpressionCommonJS ( argument , containsLexicalThis ) ;
724+ return createImportCallExpressionCommonJS ( argument ) ;
725725 }
726726 }
727727
@@ -745,7 +745,7 @@ namespace ts {
745745 return factory . createConditionalExpression (
746746 /*condition*/ factory . createIdentifier ( "__syncRequire" ) ,
747747 /*questionToken*/ undefined ,
748- /*whenTrue*/ createImportCallExpressionCommonJS ( arg , containsLexicalThis ) ,
748+ /*whenTrue*/ createImportCallExpressionCommonJS ( arg ) ,
749749 /*colonToken*/ undefined ,
750750 /*whenFalse*/ createImportCallExpressionAMD ( argClone , containsLexicalThis )
751751 ) ;
@@ -755,7 +755,7 @@ namespace ts {
755755 return factory . createComma ( factory . createAssignment ( temp , arg ) , factory . createConditionalExpression (
756756 /*condition*/ factory . createIdentifier ( "__syncRequire" ) ,
757757 /*questionToken*/ undefined ,
758- /*whenTrue*/ createImportCallExpressionCommonJS ( temp , containsLexicalThis ) ,
758+ /*whenTrue*/ createImportCallExpressionCommonJS ( temp , /* isInlineable */ true ) ,
759759 /*colonToken*/ undefined ,
760760 /*whenFalse*/ createImportCallExpressionAMD ( temp , containsLexicalThis )
761761 ) ) ;
@@ -820,14 +820,25 @@ namespace ts {
820820 return promise ;
821821 }
822822
823- function createImportCallExpressionCommonJS ( arg : Expression | undefined , containsLexicalThis : boolean ) : Expression {
824- // import("./blah" )
823+ function createImportCallExpressionCommonJS ( arg : Expression | undefined , isInlineable ? : boolean ) : Expression {
824+ // import(x )
825825 // emit as
826- // Promise.resolve().then(function () { return require(x); }) /*CommonJs Require*/
826+ // var _a;
827+ // (_a = x, Promise.resolve().then(() => require(_a)) /*CommonJs Require*/
827828 // We have to wrap require in then callback so that require is done in asynchronously
828829 // if we simply do require in resolve callback in Promise constructor. We will execute the loading immediately
829- const promiseResolveCall = factory . createCallExpression ( factory . createPropertyAccessExpression ( factory . createIdentifier ( "Promise" ) , "resolve" ) , /*typeArguments*/ undefined , /*argumentsArray*/ [ ] ) ;
830- let requireCall : Expression = factory . createCallExpression ( factory . createIdentifier ( "require" ) , /*typeArguments*/ undefined , arg ? [ arg ] : [ ] ) ;
830+ // If the arg is not inlineable, we have to evaluate it in the current scope with a temp var
831+ const temp = arg && ! isSimpleInlineableExpression ( arg ) && ! isInlineable ? factory . createTempVariable ( hoistVariableDeclaration ) : undefined ;
832+ const promiseResolveCall = factory . createCallExpression (
833+ factory . createPropertyAccessExpression ( factory . createIdentifier ( "Promise" ) , "resolve" ) ,
834+ /*typeArguments*/ undefined ,
835+ /*argumentsArray*/ [ ] ,
836+ ) ;
837+ let requireCall : Expression = factory . createCallExpression (
838+ factory . createIdentifier ( "require" ) ,
839+ /*typeArguments*/ undefined ,
840+ temp ? [ temp ] : arg ? [ arg ] : [ ] ,
841+ ) ;
831842 if ( getESModuleInterop ( compilerOptions ) ) {
832843 requireCall = emitHelpers ( ) . createImportStarHelper ( requireCall ) ;
833844 }
@@ -851,16 +862,11 @@ namespace ts {
851862 /*parameters*/ [ ] ,
852863 /*type*/ undefined ,
853864 factory . createBlock ( [ factory . createReturnStatement ( requireCall ) ] ) ) ;
854-
855- // if there is a lexical 'this' in the import call arguments, ensure we indicate
856- // that this new function expression indicates it captures 'this' so that the
857- // es2015 transformer will properly substitute 'this' with '_this'.
858- if ( containsLexicalThis ) {
859- setEmitFlags ( func , EmitFlags . CapturesThis ) ;
860- }
861865 }
862866
863- return factory . createCallExpression ( factory . createPropertyAccessExpression ( promiseResolveCall , "then" ) , /*typeArguments*/ undefined , [ func ] ) ;
867+ const downleveledImport = factory . createCallExpression ( factory . createPropertyAccessExpression ( promiseResolveCall , "then" ) , /*typeArguments*/ undefined , [ func ] ) ;
868+
869+ return temp === undefined ? downleveledImport : factory . createCommaListExpression ( [ factory . createAssignment ( temp , arg ! ) , downleveledImport ] ) ;
864870 }
865871
866872 function getHelperExpressionForExport ( node : ExportDeclaration , innerExpr : Expression ) {
0 commit comments