@@ -45,6 +45,7 @@ namespace ts {
45
45
let currentSourceFile : SourceFile ; // The current file.
46
46
let currentModuleInfo : ExternalModuleInfo ; // The ExternalModuleInfo for the current file.
47
47
let noSubstitution : boolean [ ] ; // Set of nodes for which substitution rules should be ignored.
48
+ let shouldAppendUnderscoreUnderscoreEsModule : boolean ; // A boolean indicating whether "__esModule" should be emitted
48
49
49
50
return transformSourceFile ;
50
51
@@ -62,6 +63,7 @@ namespace ts {
62
63
63
64
currentSourceFile = node ;
64
65
currentModuleInfo = collectExternalModuleInfo ( node , resolver , compilerOptions ) ;
66
+ shouldAppendUnderscoreUnderscoreEsModule = false ;
65
67
moduleInfoMap [ getOriginalNodeId ( node ) ] = currentModuleInfo ;
66
68
67
69
// Perform the transformation.
@@ -88,11 +90,14 @@ namespace ts {
88
90
addRange ( statements , endLexicalEnvironment ( ) ) ;
89
91
addExportEqualsIfNeeded ( statements , /*emitAsReturn*/ false ) ;
90
92
93
+ if ( shouldAppendUnderscoreUnderscoreEsModule ) {
94
+ append ( statements , createUnderscoreUnderscoreESModule ( ) ) ;
95
+ }
96
+
91
97
const updated = updateSourceFileNode ( node , createNodeArray ( statements , node . statements ) ) ;
92
98
if ( currentModuleInfo . hasExportStarsToExportValues ) {
93
99
addEmitHelper ( updated , exportStarHelper ) ;
94
100
}
95
-
96
101
return updated ;
97
102
}
98
103
@@ -378,6 +383,10 @@ namespace ts {
378
383
// Append the 'export =' statement if provided.
379
384
addExportEqualsIfNeeded ( statements , /*emitAsReturn*/ true ) ;
380
385
386
+ if ( shouldAppendUnderscoreUnderscoreEsModule ) {
387
+ append ( statements , createUnderscoreUnderscoreESModule ( ) ) ;
388
+ }
389
+
381
390
const body = createBlock ( statements , /*location*/ undefined , /*multiLine*/ true ) ;
382
391
if ( currentModuleInfo . hasExportStarsToExportValues ) {
383
392
// If we have any `export * from ...` declarations
@@ -657,6 +666,8 @@ namespace ts {
657
666
}
658
667
659
668
const generatedName = getGeneratedNameForNode ( node ) ;
669
+ shouldAppendUnderscoreUnderscoreEsModule = true ;
670
+
660
671
if ( node . exportClause ) {
661
672
const statements : Statement [ ] = [ ] ;
662
673
// export { x, y } from "mod";
@@ -834,6 +845,7 @@ namespace ts {
834
845
variables = append ( variables , variable ) ;
835
846
}
836
847
else if ( variable . initializer ) {
848
+ shouldAppendUnderscoreUnderscoreEsModule = true ;
837
849
expressions = append ( expressions , transformInitializedVariable ( variable ) ) ;
838
850
}
839
851
}
@@ -1107,43 +1119,40 @@ namespace ts {
1107
1119
* @param allowComments Whether to allow comments on the export.
1108
1120
*/
1109
1121
function appendExportStatement ( statements : Statement [ ] | undefined , exportName : Identifier , expression : Expression , location ?: TextRange , allowComments ?: boolean ) : Statement [ ] | undefined {
1110
- if ( exportName . text === "default" ) {
1111
- const sourceFile = getOriginalNode ( currentSourceFile , isSourceFile ) ;
1112
- if ( sourceFile && ! sourceFile . symbol . exports . get ( "___esModule" ) ) {
1113
- if ( languageVersion === ScriptTarget . ES3 ) {
1114
- statements = append ( statements ,
1115
- createStatement (
1116
- createExportExpression (
1117
- createIdentifier ( "__esModule" ) ,
1118
- createLiteral ( true )
1119
- )
1120
- )
1121
- ) ;
1122
- }
1123
- else {
1124
- statements = append ( statements ,
1125
- createStatement (
1126
- createCall (
1127
- createPropertyAccess ( createIdentifier ( "Object" ) , "defineProperty" ) ,
1128
- /*typeArguments*/ undefined ,
1129
- [
1130
- createIdentifier ( "exports" ) ,
1131
- createLiteral ( "__esModule" ) ,
1132
- createObjectLiteral ( [
1133
- createPropertyAssignment ( "value" , createLiteral ( true ) )
1134
- ] )
1135
- ]
1136
- )
1137
- )
1138
- ) ;
1139
- }
1140
- }
1141
- }
1142
-
1122
+ shouldAppendUnderscoreUnderscoreEsModule = true ;
1143
1123
statements = append ( statements , createExportStatement ( exportName , expression , location , allowComments ) ) ;
1144
1124
return statements ;
1145
1125
}
1146
1126
1127
+ function createUnderscoreUnderscoreESModule ( ) {
1128
+ let statement : Statement ;
1129
+ if ( languageVersion === ScriptTarget . ES3 ) {
1130
+ statement = createStatement (
1131
+ createExportExpression (
1132
+ createIdentifier ( "__esModule" ) ,
1133
+ createLiteral ( true )
1134
+ )
1135
+ )
1136
+ }
1137
+ else {
1138
+ statement = createStatement (
1139
+ createCall (
1140
+ createPropertyAccess ( createIdentifier ( "Object" ) , "defineProperty" ) ,
1141
+ /*typeArguments*/ undefined ,
1142
+ [
1143
+ createIdentifier ( "exports" ) ,
1144
+ createLiteral ( "__esModule" ) ,
1145
+ createObjectLiteral ( [
1146
+ createPropertyAssignment ( "value" , createLiteral ( true ) )
1147
+ ] )
1148
+ ]
1149
+ )
1150
+ ) ;
1151
+ }
1152
+ setEmitFlags ( statement , EmitFlags . CustomPrologue ) ;
1153
+ return statement ;
1154
+ }
1155
+
1147
1156
/**
1148
1157
* Creates a call to the current file's export function to export a value.
1149
1158
*
0 commit comments