@@ -721,7 +721,7 @@ namespace ts {
721
721
return createImportCallExpressionUMD ( argument ?? factory . createVoidZero ( ) , containsLexicalThis ) ;
722
722
case ModuleKind . CommonJS :
723
723
default :
724
- return createImportCallExpressionCommonJS ( argument , containsLexicalThis ) ;
724
+ return createImportCallExpressionCommonJS ( argument ) ;
725
725
}
726
726
}
727
727
@@ -745,7 +745,7 @@ namespace ts {
745
745
return factory . createConditionalExpression (
746
746
/*condition*/ factory . createIdentifier ( "__syncRequire" ) ,
747
747
/*questionToken*/ undefined ,
748
- /*whenTrue*/ createImportCallExpressionCommonJS ( arg , containsLexicalThis ) ,
748
+ /*whenTrue*/ createImportCallExpressionCommonJS ( arg ) ,
749
749
/*colonToken*/ undefined ,
750
750
/*whenFalse*/ createImportCallExpressionAMD ( argClone , containsLexicalThis )
751
751
) ;
@@ -755,7 +755,7 @@ namespace ts {
755
755
return factory . createComma ( factory . createAssignment ( temp , arg ) , factory . createConditionalExpression (
756
756
/*condition*/ factory . createIdentifier ( "__syncRequire" ) ,
757
757
/*questionToken*/ undefined ,
758
- /*whenTrue*/ createImportCallExpressionCommonJS ( temp , containsLexicalThis ) ,
758
+ /*whenTrue*/ createImportCallExpressionCommonJS ( temp , /* isInlineable */ true ) ,
759
759
/*colonToken*/ undefined ,
760
760
/*whenFalse*/ createImportCallExpressionAMD ( temp , containsLexicalThis )
761
761
) ) ;
@@ -820,14 +820,25 @@ namespace ts {
820
820
return promise ;
821
821
}
822
822
823
- function createImportCallExpressionCommonJS ( arg : Expression | undefined , containsLexicalThis : boolean ) : Expression {
824
- // import("./blah" )
823
+ function createImportCallExpressionCommonJS ( arg : Expression | undefined , isInlineable ? : boolean ) : Expression {
824
+ // import(x )
825
825
// 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*/
827
828
// We have to wrap require in then callback so that require is done in asynchronously
828
829
// 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
+ ) ;
831
842
if ( getESModuleInterop ( compilerOptions ) ) {
832
843
requireCall = emitHelpers ( ) . createImportStarHelper ( requireCall ) ;
833
844
}
@@ -851,16 +862,11 @@ namespace ts {
851
862
/*parameters*/ [ ] ,
852
863
/*type*/ undefined ,
853
864
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
- }
861
865
}
862
866
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 ] ) ;
864
870
}
865
871
866
872
function getHelperExpressionForExport ( node : ExportDeclaration , innerExpr : Expression ) {
0 commit comments