Extending the template compiler

The Aurelia template compiler is highly extensible, providing multiple hooks and extension points for advanced customization. This guide covers the advanced features and extension mechanisms available for developers who need to extend template compilation behavior.

Template Compiler Hooks

Registering Compilation Hooks

Template compiler hooks allow you to modify templates during the compilation process:

import { templateCompilerHooks, ITemplateCompilerHooks } from 'aurelia';

@templateCompilerHooks
class MyCompilerHook implements ITemplateCompilerHooks {
  compiling(template: HTMLElement): void {
    // Modify template before compilation
    this.addDefaultAttributes(template);
    this.injectDevelopmentHelpers(template);
  }

  private addDefaultAttributes(template: HTMLElement): void {
    // Add default attributes to form elements
    template.querySelectorAll('input[type="text"]').forEach(input => {
      if (!input.hasAttribute('autocomplete')) {
        input.setAttribute('autocomplete', 'off');
      }
    });
  }

  private injectDevelopmentHelpers(template: HTMLElement): void {
    if (__DEV__) {
      // Add development-only attributes
      template.querySelectorAll('[data-dev-hint]').forEach(el => {
        el.setAttribute('title', el.getAttribute('data-dev-hint')!);
      });
    }
  }
}

Global vs Component-Level Hooks

Hooks can be registered globally or at the component level:

Advanced Attribute Pattern System

Creating Custom Attribute Syntax

The attribute pattern system allows you to create custom binding syntax:

Complex Pattern Matching

Support for multi-part patterns with custom symbols:

Custom Binding Commands

Advanced Binding Command Features

Binding commands can take full control of attribute processing:

Multi-Attribute Processing

Commands can process multiple attributes for complex scenarios:

Template Element Factory Customization

Custom Template Caching

The template element factory supports custom caching strategies:

Template Wrapping Detection

Customize how templates are wrapped for proper compilation:

Advanced Resource Resolution

Custom Resource Discovery

Implement custom resource resolution for dynamic components:

Bindables Information Caching

Optimize bindable resolution with custom caching:

Local Template System

Advanced Local Element Definitions

Create complex local element hierarchies:

Dynamic Local Template Creation

Create local templates programmatically:

Compilation Context System

Hierarchical Resource Resolution

Work with compilation contexts for advanced scenarios:

Custom Dependency Injection

Customize DI container behavior during compilation:

Performance Optimization

Template Compilation Caching

Implement aggressive template caching for performance:

Compilation Mode Optimization

Configure compilation for different environments:

Best Practices

1. Hook Registration

  • Register global hooks early in application bootstrap

  • Use component-level hooks for specific customizations

  • Keep hooks lightweight to avoid compilation performance impact

2. Pattern and Command Design

  • Design patterns to be intuitive and consistent with Aurelia conventions

  • Use descriptive names and clear syntax

  • Provide good error messages for invalid usage

3. Resource Resolution

  • Cache expensive resource lookups

  • Implement fallback mechanisms for missing resources

  • Use lazy loading for dynamic components

4. Performance Considerations

  • Profile template compilation in development

  • Use AOT compilation for production builds

  • Implement smart caching strategies

  • Monitor memory usage with large template caches

5. Testing Extensions

  • Create unit tests for custom hooks and commands

  • Test compilation output for correctness

  • Verify performance impact of extensions

  • Test edge cases and error handling

The template compiler's extensibility allows for powerful customizations while maintaining framework performance and consistency. Use these extension points judiciously to enhance your application's template processing capabilities.

Last updated

Was this helpful?