From 50d4a4fe5c9047d8eb28d91a339f3e9ee41efe40 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 20 Jun 2018 16:00:48 -0700 Subject: [PATCH] fix(compiler): fix a few non-tree-shakeable code patterns (#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 #24677 --- .../compiler/src/render3/view/template.ts | 22 +++++++++++++------ .../src/template_parser/template_ast.ts | 2 +- .../hello_world_r2/bundle.golden_symbols.json | 9 -------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/compiler/src/render3/view/template.ts b/packages/compiler/src/render3/view/template.ts index decfe1ea94..2a2c92253b 100644 --- a/packages/compiler/src/render3/view/template.ts +++ b/packages/compiler/src/render3/view/template.ts @@ -32,12 +32,20 @@ import {htmlAstToRender3Ast} from '../r3_template_transform'; import {R3QueryMetadata} from './api'; 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'; -const BINDING_INSTRUCTION_MAP: {[type: number]: o.ExternalReference} = { - [BindingType.Property]: R3.elementProperty, - [BindingType.Attribute]: R3.elementAttribute, - [BindingType.Class]: R3.elementClassNamed, - [BindingType.Style]: R3.elementStyleNamed, -}; +function mapBindingToInstruction(type: BindingType): o.ExternalReference|undefined { + switch (type) { + case BindingType.Property: + return R3.elementProperty; + case BindingType.Attribute: + return R3.elementAttribute; + case BindingType.Class: + return R3.elementClassNamed; + case BindingType.Style: + return R3.elementStyleNamed; + default: + return undefined; + } +} // `className` is used below instead of `class` because the interception // code (where this map is used) deals with DOM element property values @@ -414,7 +422,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver return; } - const instruction = BINDING_INSTRUCTION_MAP[input.type]; + const instruction = mapBindingToInstruction(input.type); if (instruction) { // TODO(chuckj): runtime: security context? this.instruction( diff --git a/packages/compiler/src/template_parser/template_ast.ts b/packages/compiler/src/template_parser/template_ast.ts index 4f69a54608..df201b6766 100644 --- a/packages/compiler/src/template_parser/template_ast.ts +++ b/packages/compiler/src/template_parser/template_ast.ts @@ -58,7 +58,7 @@ export class AttrAst implements TemplateAst { visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitAttr(this, context); } } -export enum PropertyBindingType { +export const enum PropertyBindingType { // A normal binding to a property (e.g. `[property]="expression"`). Property, // A binding to an element attribute (e.g. `[attr.name]="expression"`). diff --git a/packages/core/test/bundling/hello_world_r2/bundle.golden_symbols.json b/packages/core/test/bundling/hello_world_r2/bundle.golden_symbols.json index ab45b5d0a1..84573d89a0 100644 --- a/packages/core/test/bundling/hello_world_r2/bundle.golden_symbols.json +++ b/packages/core/test/bundling/hello_world_r2/bundle.golden_symbols.json @@ -356,9 +356,6 @@ { "name": "CORE" }, - { - "name": "CORE$1" - }, { "name": "CR" }, @@ -803,9 +800,6 @@ { "name": "Identifiers" }, - { - "name": "Identifiers$1" - }, { "name": "IfStmt" }, @@ -1208,9 +1202,6 @@ { "name": "PreparsedElementType" }, - { - "name": "PropertyBindingType" - }, { "name": "PropertyRead" },