Skip to content

Commit 2ff2850

Browse files
author
Kanchalai Tanglertsampan
committed
Emit __esModule when emitting ES6 Module
1 parent 8372a1d commit 2ff2850

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace ts {
4545
let currentSourceFile: SourceFile; // The current file.
4646
let currentModuleInfo: ExternalModuleInfo; // The ExternalModuleInfo for the current file.
4747
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
4849

4950
return transformSourceFile;
5051

@@ -62,6 +63,7 @@ namespace ts {
6263

6364
currentSourceFile = node;
6465
currentModuleInfo = collectExternalModuleInfo(node, resolver, compilerOptions);
66+
shouldAppendUnderscoreUnderscoreEsModule = false;
6567
moduleInfoMap[getOriginalNodeId(node)] = currentModuleInfo;
6668

6769
// Perform the transformation.
@@ -88,11 +90,14 @@ namespace ts {
8890
addRange(statements, endLexicalEnvironment());
8991
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false);
9092

93+
if (shouldAppendUnderscoreUnderscoreEsModule) {
94+
append(statements, createUnderscoreUnderscoreESModule());
95+
}
96+
9197
const updated = updateSourceFileNode(node, createNodeArray(statements, node.statements));
9298
if (currentModuleInfo.hasExportStarsToExportValues) {
9399
addEmitHelper(updated, exportStarHelper);
94100
}
95-
96101
return updated;
97102
}
98103

@@ -378,6 +383,10 @@ namespace ts {
378383
// Append the 'export =' statement if provided.
379384
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ true);
380385

386+
if (shouldAppendUnderscoreUnderscoreEsModule) {
387+
append(statements, createUnderscoreUnderscoreESModule());
388+
}
389+
381390
const body = createBlock(statements, /*location*/ undefined, /*multiLine*/ true);
382391
if (currentModuleInfo.hasExportStarsToExportValues) {
383392
// If we have any `export * from ...` declarations
@@ -657,6 +666,8 @@ namespace ts {
657666
}
658667

659668
const generatedName = getGeneratedNameForNode(node);
669+
shouldAppendUnderscoreUnderscoreEsModule = true;
670+
660671
if (node.exportClause) {
661672
const statements: Statement[] = [];
662673
// export { x, y } from "mod";
@@ -834,6 +845,7 @@ namespace ts {
834845
variables = append(variables, variable);
835846
}
836847
else if (variable.initializer) {
848+
shouldAppendUnderscoreUnderscoreEsModule = true;
837849
expressions = append(expressions, transformInitializedVariable(variable));
838850
}
839851
}
@@ -1107,43 +1119,40 @@ namespace ts {
11071119
* @param allowComments Whether to allow comments on the export.
11081120
*/
11091121
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;
11431123
statements = append(statements, createExportStatement(exportName, expression, location, allowComments));
11441124
return statements;
11451125
}
11461126

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+
11471156
/**
11481157
* Creates a call to the current file's export function to export a value.
11491158
*

0 commit comments

Comments
 (0)