Skip to content

Commit 50d4a4f

Browse files
alxhubmhevery
authored andcommitted
fix(compiler): fix a few non-tree-shakeable code patterns (angular#24677)
This change makes @angular/compiler more tree-shakeable by changing an enum to a const enum and by getting rid of a top-level map that the tree-shaker was seeing as a reference which caused r3_identifiers to be retained. This drops a few hundred bytes of JS from tree-shaken ngtsc compiled apps. PR Close angular#24677
1 parent 69510ac commit 50d4a4f

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

packages/compiler/src/render3/view/template.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ import {htmlAstToRender3Ast} from '../r3_template_transform';
3232
import {R3QueryMetadata} from './api';
3333
import {CONTEXT_NAME, I18N_ATTR, I18N_ATTR_PREFIX, ID_SEPARATOR, IMPLICIT_REFERENCE, MEANING_SEPARATOR, REFERENCE_PREFIX, RENDER_FLAGS, TEMPORARY_NAME, asLiteral, getQueryPredicate, invalid, mapToExpression, noop, temporaryAllocator, trimTrailingNulls, unsupported} from './util';
3434

35-
const BINDING_INSTRUCTION_MAP: {[type: number]: o.ExternalReference} = {
36-
[BindingType.Property]: R3.elementProperty,
37-
[BindingType.Attribute]: R3.elementAttribute,
38-
[BindingType.Class]: R3.elementClassNamed,
39-
[BindingType.Style]: R3.elementStyleNamed,
40-
};
35+
function mapBindingToInstruction(type: BindingType): o.ExternalReference|undefined {
36+
switch (type) {
37+
case BindingType.Property:
38+
return R3.elementProperty;
39+
case BindingType.Attribute:
40+
return R3.elementAttribute;
41+
case BindingType.Class:
42+
return R3.elementClassNamed;
43+
case BindingType.Style:
44+
return R3.elementStyleNamed;
45+
default:
46+
return undefined;
47+
}
48+
}
4149

4250
// `className` is used below instead of `class` because the interception
4351
// code (where this map is used) deals with DOM element property values
@@ -414,7 +422,7 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
414422
return;
415423
}
416424

417-
const instruction = BINDING_INSTRUCTION_MAP[input.type];
425+
const instruction = mapBindingToInstruction(input.type);
418426
if (instruction) {
419427
// TODO(chuckj): runtime: security context?
420428
this.instruction(

packages/compiler/src/template_parser/template_ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class AttrAst implements TemplateAst {
5858
visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitAttr(this, context); }
5959
}
6060

61-
export enum PropertyBindingType {
61+
export const enum PropertyBindingType {
6262
// A normal binding to a property (e.g. `[property]="expression"`).
6363
Property,
6464
// A binding to an element attribute (e.g. `[attr.name]="expression"`).

packages/core/test/bundling/hello_world_r2/bundle.golden_symbols.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,6 @@
356356
{
357357
"name": "CORE"
358358
},
359-
{
360-
"name": "CORE$1"
361-
},
362359
{
363360
"name": "CR"
364361
},
@@ -803,9 +800,6 @@
803800
{
804801
"name": "Identifiers"
805802
},
806-
{
807-
"name": "Identifiers$1"
808-
},
809803
{
810804
"name": "IfStmt"
811805
},
@@ -1208,9 +1202,6 @@
12081202
{
12091203
"name": "PreparsedElementType"
12101204
},
1211-
{
1212-
"name": "PropertyBindingType"
1213-
},
12141205
{
12151206
"name": "PropertyRead"
12161207
},

0 commit comments

Comments
 (0)