
Bootstrap Angular 2 with Ahead of Time Template Compilation
Tyler Davis
Reading time: about 4 min
Topics:
main.ts
and will look like this:
import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
In looking at this file, we can see a few different things happening. All of the poyfills required to run Angular 2 are imported from the core-js and zone node modules. Then platformBrowserDynamic
is imported from @angular/platform-browser-dynamic
. This is fine for development, but it is much better to compile your templates once and then serve the pre-compiled templates to your users. In order to precompile your templates, you’ll need to update your bootstrap logic. Here is a diff of the changes you'll need to make:
-import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
+import { platformBrowser } from '@angular/platform-browser';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
-import { AppModule } from './app/';
+import { AppModuleNgFactory } from './app/app.module.ngfactory';
if (environment.production) {
enableProdMode();
}
-platformBrowserDynamic().bootstrapModule(AppModule);
+platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
And here is the resulting file:
import './polyfills.ts';
import { platformBrowser } from '@angular/platform-browser';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModuleNgFactory } from './app/app.module.ngfactory';
if (environment.production) {
enableProdMode();
}
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
Two things changed here. We removed dynamic from our platform browser import, and we also imported and bootstrapped AppModuleNgFactory
instead of AppModule
. AppModuleNgFactory
is a TypeScript class that is generated by the compiler to create your application. When the dynamic platform is used, this factory is created each time you load your application; however, now we are creating it before our application even runs. The AppModuleNgFactory
is not going to exist until you build your application with ngc. If you try to build your application with ng build
or tsc
, you’ll get an error like this:
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './app/app.ngfactory' in
@ ./src/main.ts 5:0-57
@ multi main
It is likely that ngc was installed with your angular application, so in order to run ngc, you’ll just have to run ./path/to/node_modules/.bin/ngc -p ./path/to/src in your project directory. If it’s not installed, you can install it with npm install @angular/compiler-cli typescript@next @angular/platform-server @angular/compiler
.
After successfully running ngc, you’ll see that you have a main.js
in your project's output directory. You’ll notice that it outputs es6 and does not package it, so you’ll have to use another tool to package your es6. My colleague set up an example application that uses Google Closure Compiler to package an Angular application with AOT template compilation for production and Just in Time (JIT) template compilation for development. He also wrote a blog post about his endeavors that you can find here.
There you have it — everything you need to successfully bootstrap an Angular 2 application with Ahead of Time template compilation.About Lucid
Lucid Software is the leader in visual collaboration and work acceleration, helping teams see and build the future by turning ideas into reality. Its products include the Lucid Visual Collaboration Suite (Lucidchart and Lucidspark) and airfocus. The Lucid Visual Collaboration Suite, combined with powerful accelerators for business agility, cloud, and process transformation, empowers organizations to streamline work, foster alignment, and drive business transformation at scale. airfocus, an AI-powered product management and roadmapping platform, extends these capabilities by helping teams prioritize work, define product strategy, and align execution with business goals. The most used work acceleration platform by the Fortune 500, Lucid's solutions are trusted by more than 100 million users across enterprises worldwide, including Google, GE, and NBC Universal. Lucid partners with leaders such as Google, Atlassian, and Microsoft, and has received numerous awards for its products, growth, and workplace culture.