Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mad Science with the Angular Compiler

Mad Science with the Angular Compiler

One of the most unique features of Angular is the way it performs change detection. The template of each individual component in the app gets transpiled to efficient JavaScript which makes the dirty checking algorithm “obviously fast”.

The fast change detection is not the only benefit we get from the template compiler! In this talk we’ll mix some mad science ideas with the Angular compiler and see where we can go!

Minko Gechev

April 05, 2017
Tweet

More Decks by Minko Gechev

Other Decks in Programming

Transcript

  1. twitter.com/mgechev twitter.com/mgechev @Component({ selector: 'header-cmp', template: ` <header> {{ title

    }} </header> ` }) class HeaderComponent { ... } class _View_HeaderComponent extends import1.AppView<import0.HeaderComponent> { constructor(...) { ... } createInternal(...):import3.AppElement { ... } injectorGetInternal(...):any { ... } }
  2. twitter.com/mgechev Lexical Analysis tokenize('const product = a * b;') [

    { lexeme: 'const', type: 'keyword' }, { lexeme: 'product', type: 'identifier' }, { lexeme: '=', type: 'operator' }, ... ]
  3. twitter.com/mgechev twitter.com/mgechev Static program analysis is the analysis of computer

    software that is performed without actually executing programs. Wikipedia
  4. twitter.com/mgechev tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent

    { ... } `) ); CLASS DECORATOR OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]
  5. twitter.com/mgechev Validate Selector visitClass(node) { node.decorators.forEach(visitDecorator); } visitDecorator(node) { if

    (node.name === 'Component') { visitComponentMetadata(readMetadata(node)) } } visitComponentMetadata(metadata) { if (!parseSelector(metadata.selector).element) { tslint.report('Components should have element selectors'); } }
  6. twitter.com/mgechev twitter.com/mgechev @Component({ selector: 'header-cmp', template: ` <h1 class="greeting">Hello <span>{{

    name }}</span>!</div> `, styles: [` .greeting spam { color: red; } `] }) class HeaderComponent { ... }
  7. twitter.com/mgechev twitter.com/mgechev @Component({ selector: 'header-cmp', template: ` <h1 class="greeting">Hello <span>{{

    name }}</span>!</div> `, styles: [` .greeting spam { color: red; } `] }) class HeaderComponent { ... }
  8. twitter.com/mgechev twitter.com/mgechev TEMPLATE AST DIV.GREETING SPAN COMPONENT STYLES AST CSS

    RULE SELECTOR .GREETING SPAM … <div class="greeting"> <span>...</span> </div> .greeting spam { color: red; }
  9. twitter.com/mgechev Smooth Upgrade to v4 $ npm install @angular/ {common,compiler,compiler-cli,

    core,forms,http,platform-browser, platform-browser-dynamic, platform-server,router,animations} @^4.0.0 typescript@^2.2.2 --save --save-exact
  10. twitter.com/mgechev Smooth Upgrade to v4 …with a few deprecations $

    npm install @angular/ {common,compiler,compiler-cli, core,forms,http,platform-browser, platform-browser-dynamic, platform-server,router,animations} @^4.0.0 typescript@^2.2.2 --save --save-exact
  11. twitter.com/mgechev Understanding Large System • A lot of code which

    is hard to digest • Mixing different levels of abstractions • Humans are good in “visual thinking”
  12. twitter.com/mgechev ngast provides • Directives / components and their metadata

    • Providers and their relations • etc… A way to get all: