@@ -850,32 +850,57 @@ export function transformModule(context: TransformationContext): (x: SourceFile
850850 function createImportCallExpressionCommonJS ( arg : Expression | undefined , isInlineable ?: boolean ) : Expression {
851851 // import(x)
852852 // emit as
853- // var _a;
854- // (_a = x, Promise.resolve().then(() => require(_a)) /*CommonJs Require*/
853+ // Promise.resolve(`${x}`).then((s) => require(s)) /*CommonJs Require*/
855854 // We have to wrap require in then callback so that require is done in asynchronously
856855 // if we simply do require in resolve callback in Promise constructor. We will execute the loading immediately
857- // If the arg is not inlineable, we have to evaluate it in the current scope with a temp var
858- const temp = arg && ! isSimpleInlineableExpression ( arg ) && ! isInlineable ? factory . createTempVariable ( hoistVariableDeclaration ) : undefined ;
856+ // If the arg is not inlineable, we have to evaluate and ToString() it in the current scope
857+ // Otherwise, we inline it in require() so that it's statically analyzable
858+ const needSyncEval = arg && ! isSimpleInlineableExpression ( arg ) && ! isInlineable ;
859+
859860 const promiseResolveCall = factory . createCallExpression (
860861 factory . createPropertyAccessExpression ( factory . createIdentifier ( "Promise" ) , "resolve" ) ,
861862 /*typeArguments*/ undefined ,
862- /*argumentsArray*/ [ ] ,
863+ /*argumentsArray*/ needSyncEval
864+ ? languageVersion >= ScriptTarget . ES2015
865+ ? [
866+ factory . createTemplateExpression ( factory . createTemplateHead ( "" ) , [
867+ factory . createTemplateSpan ( arg , factory . createTemplateTail ( "" ) ) ,
868+ ] ) ,
869+ ]
870+ : [
871+ factory . createCallExpression (
872+ factory . createPropertyAccessExpression ( factory . createStringLiteral ( "" ) , "concat" ) ,
873+ /*typeArguments*/ undefined ,
874+ [ arg ]
875+ ) ,
876+ ]
877+ : [ ]
863878 ) ;
879+
864880 let requireCall : Expression = factory . createCallExpression (
865881 factory . createIdentifier ( "require" ) ,
866882 /*typeArguments*/ undefined ,
867- temp ? [ temp ] : arg ? [ arg ] : [ ] ,
883+ needSyncEval ? [ factory . createIdentifier ( "s" ) ] : arg ? [ arg ] : [ ] ,
868884 ) ;
869885 if ( getESModuleInterop ( compilerOptions ) ) {
870886 requireCall = emitHelpers ( ) . createImportStarHelper ( requireCall ) ;
871887 }
872888
889+ const parameters = needSyncEval
890+ ? [
891+ factory . createParameterDeclaration (
892+ /*modifiers*/ undefined ,
893+ /*dotDotDotToken*/ undefined ,
894+ /*name*/ "s" ) ,
895+ ]
896+ : [ ] ;
897+
873898 let func : FunctionExpression | ArrowFunction ;
874899 if ( languageVersion >= ScriptTarget . ES2015 ) {
875900 func = factory . createArrowFunction (
876901 /*modifiers*/ undefined ,
877902 /*typeParameters*/ undefined ,
878- /*parameters*/ [ ] ,
903+ /*parameters*/ parameters ,
879904 /*type*/ undefined ,
880905 /*equalsGreaterThanToken*/ undefined ,
881906 requireCall ) ;
@@ -886,14 +911,14 @@ export function transformModule(context: TransformationContext): (x: SourceFile
886911 /*asteriskToken*/ undefined ,
887912 /*name*/ undefined ,
888913 /*typeParameters*/ undefined ,
889- /*parameters*/ [ ] ,
914+ /*parameters*/ parameters ,
890915 /*type*/ undefined ,
891916 factory . createBlock ( [ factory . createReturnStatement ( requireCall ) ] ) ) ;
892917 }
893918
894919 const downleveledImport = factory . createCallExpression ( factory . createPropertyAccessExpression ( promiseResolveCall , "then" ) , /*typeArguments*/ undefined , [ func ] ) ;
895920
896- return temp === undefined ? downleveledImport : factory . createCommaListExpression ( [ factory . createAssignment ( temp , arg ! ) , downleveledImport ] ) ;
921+ return downleveledImport ;
897922 }
898923
899924 function getHelperExpressionForExport ( node : ExportDeclaration , innerExpr : Expression ) {
0 commit comments