The Template Cache Transformer generates the template cache and optionally merges the CSS and HTML of AngularDart components into a hosting Web page. This substantially speeds up the Web page loading by eliminating the need for the browser to make multiple server requests for downloading component dependencies.
Getting Started
-
Add
template_cache_transformer
to thedev_dependencies
of your AngularDart application:pubspec.yamldev_dependencies: template_cache_transformer: any
-
Add
template_cache_transformer
to thetransformers
list and specifyentry_point
setting for the transformer. It should point to yourindex.html
file:pubspec.yamltransformers: - template_cache_transformer: entry_point: my_app|web/index.html (1)
1 Note, entry_point
must be provided in AssetId format, more on this read below. -
Run
pub get
to download just added dependencies:$ pub get Resolving dependencies... + template_cache_transformer 0.1.0 Downloading template_cache_transformer 0.1.0... Changed 1 dependency!
-
Run
pub build
. When the build completes you should be able to find filetemplate_cache.generated.html
insidebuild/web/
directory. The generated cache should be also inserted in the HTML page you specified as the entry point right before closing</head>
tag.Make sure you have at least one AngularDart component in your application which uses
templateUrl
orcssUrl
property to reference a file, otherwisetemplate_cache.generated.html
file will be empty. Example:@Component( selector: 'toggle-button', templateUrl: 'packages/my_app/components/toggle_button.html') class ToggleButton { }
How It Works?
The template cache transformer internally runs in two phases: TemplateCacheGenerator
and TemplateCacheWriter
:
TemplateCacheGenerator
):-
Transformer finds the entry point of Dart application (
main()
function) and traverses the entire AST looking for AngularDart’s@Component
annotations. -
For each
@Component
annotation it collects URIs referenced bytemplateUrl
andcssUrl
properties. -
Then it reads assets referenced by the collected URIs and wraps the content of each asset into a
<template>
tag representing a template cache entry. -
All cache entries created on the previous step are concatenated and written in a newly created asset -
web/template_cache.generated.html
.
TemplateCacheWriter
, runs only if entry_point
is specified in pubspec.yaml
):-
Reads
web/template_cache.generated.html
asset generated in the phase 1 and inserts its content into an HTML page specified byentry_point
setting inpubspec.yaml
right before closing</head>
tag.
Configuration Options
Optional. Transformer uses static code analyzer which requires to know the path to Dart SDK to resolve Dart libraries. In most cases you don’t need to specify this setting, Dart SDK is discovered automatically. But in rare cases transformer is not able to find Dart SDK (please, see discussion here), to workaround this issue you can explicitly specify path using this setting.
Optional. Use to specify an entry point of your application. It must be a string referencing an HTML page. Usually it is index.html
. The value must be provided in an AssetId
format. Example: my_app|web/index.html
, where my_app
is the package name of your application. In future versions you should be able to specify only the path part.
FAQ
Why asset can’t be found?
Sometimes you may see warning messages similar to this:
[Warning from TemplateCacheGenerator]: Can't find asset lib/components/toggle_button.css.
This may happen if the specified asset should be produced by some other transformer. For example if you reference CSS files in your @Component
annotations, but actually uses LESS files for your stylesheets. Then LESS Transformer should be run before the Template Cache Transformer to make it work properly.
Why Dart SDK can’t be found?
Sometimes you may see error messages similar to this:
Error on line 8, column 3 of pubspec.yaml: Error loading transformer: Illegal argument(s): sdkDirectory must be provided. - template_cache_transformer ^^^^^^^^^^^^^^^^^^^^^^^^^^
To workaround the issue explicitly specify dart_sdk
option for the transformer. Please read more about this above.
Why entry point is optional?
If entry point is specified generated template cache will be inserted into the main HTML page. But it’s not necessarily the desired behavior. Sometimes you may want to load generated HTML file manually to control place and moment where and when to insert cached templates.