Skip to content

Commit 6ef2923

Browse files
committed
Always wrap classes with decorators or static properties in an IIFE
Currently only script targets less than or equal to ES5 will wrap classes. However, the wrapping is also crucial to file size optimizations for ES2015+ as well. Without the IIFE wrapper, minification tools do not elide the class. This is due to references to the class being present within the downlevelled decorator and static property code. This change represents the full completion of issue #15857
1 parent b8b5948 commit 6ef2923

File tree

1 file changed

+8
-4
lines changed
  • src/compiler/transformers

1 file changed

+8
-4
lines changed

src/compiler/transformers/ts.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ namespace ts {
2323
IsNamedExternalExport = 1 << 4,
2424
IsDefaultExternalExport = 1 << 5,
2525
IsDerivedClass = 1 << 6,
26-
UseImmediatelyInvokedFunctionExpression = 1 << 7,
2726

2827
HasAnyDecorators = HasConstructorDecorators | HasMemberDecorators,
2928
NeedsName = HasStaticInitializedProperties | HasMemberDecorators,
30-
MayNeedImmediatelyInvokedFunctionExpression = HasAnyDecorators | HasStaticInitializedProperties,
29+
UseImmediatelyInvokedFunctionExpression = HasAnyDecorators | HasStaticInitializedProperties,
3130
IsExported = IsExportOfNamespace | IsDefaultExternalExport | IsNamedExternalExport,
3231
}
3332

@@ -590,7 +589,6 @@ namespace ts {
590589
if (isExportOfNamespace(node)) facts |= ClassFacts.IsExportOfNamespace;
591590
else if (isDefaultExternalModuleExport(node)) facts |= ClassFacts.IsDefaultExternalExport;
592591
else if (isNamedExternalModuleExport(node)) facts |= ClassFacts.IsNamedExternalExport;
593-
if (languageVersion <= ScriptTarget.ES5 && (facts & ClassFacts.MayNeedImmediatelyInvokedFunctionExpression)) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
594592
return facts;
595593
}
596594

@@ -661,6 +659,12 @@ namespace ts {
661659
const iife = createImmediatelyInvokedArrowFunction(statements);
662660
setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper);
663661

662+
// Class comment is already added by the ES2015 transform when targeting ES5 or below.
663+
// Only add if targetting ES2015+ to prevent duplicates
664+
if (languageVersion > ScriptTarget.ES5) {
665+
addSyntheticLeadingComment(iife, SyntaxKind.MultiLineCommentTrivia, "* @class ");
666+
}
667+
664668
const varStatement = createVariableStatement(
665669
/*modifiers*/ undefined,
666670
createVariableDeclarationList([
@@ -669,7 +673,7 @@ namespace ts {
669673
/*type*/ undefined,
670674
iife
671675
)
672-
])
676+
], languageVersion > ScriptTarget.ES5 ? NodeFlags.Let : undefined)
673677
);
674678

675679
setOriginalNode(varStatement, node);

0 commit comments

Comments
 (0)