| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | include ../_util-fns | 
					
						
							|  |  |  |  |    | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   This cookbook describes how to radically improve performance by compiling _Ahead of Time_ (AoT)  | 
					
						
							|  |  |  |  |   during a build process. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这个烹饪指南描述如何通过在构建过程中进行预编译(Ahead of Time - AoT)来从根本上提升性能。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#toc | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Table of Contents | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   ## 目录 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Overview](#overview) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [概览](#overview) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [_Ahead-of-Time_ vs _Just-in-Time_](#aot-jit) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [_预编译_ vs _即时编译_](#aot-jit) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Compile with AoT](#compile) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [用AoT进行编译](#compile) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Bootstrap](#bootstrap) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [引导](#bootstrap) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Tree Shaking](#tree-shaking) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [摇树优化(Tree Shaking)](#tree-shaking) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Load the bundle](#load) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [加载捆文件](#load) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Serve the app](#serve) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [启动应用服务器](#serve) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   * [Source Code](#source-code) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   * [源码](#source-code) | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   * [Tour of Heroes](#toh) | 
					
						
							| 
									
										
										
										
											2016-10-17 22:48:18 +01:00
										 |  |  |  |   * [英雄指南](#toh) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#overview | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Overview | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   ## 概览 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   An Angular application consist largely of components and their HTML templates. | 
					
						
							|  |  |  |  |   Before the browser can render the application,  | 
					
						
							|  |  |  |  |   the components and templates must be converted to executable JavaScript by the _Angular compiler_. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Angular应用主要包含组件和它们的HTML模板。 | 
					
						
							|  |  |  |  |   在浏览器可以渲染应用之前,组件和模板必须要被**Angular编译器**转换为可以执行的JavaScript。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     <a href="https://www.youtube.com/watch?v=kW9cJsvcsGo" target="_blank">Watch compiler author Tobias Bosch explain the Angular Compiler</a> at AngularConnect 2016. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     观看编译器作者Tobias Bosch在AngularConnect 2016大会里,对<a href="http://v.youku.com/v_show/id_XMTc1NTE4NTkwOA==.html?from=y1.7-1.4" target="_blank">Angular编译器</a>的演讲。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   You can compile the app in the browser, at runtime, as the application loads, using the **_Just-in-Time_ (JiT) compiler**.  | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   This is the standard development approach shown throughout the documentation. | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   It's great .. but it has shortcomings. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   你可以在浏览器中使用*即时编译器*(Just-in-Time - JiT)在运行期间编译该应用,也就是在应用加载时。 | 
					
						
							|  |  |  |  |   这是本文档中展示过的标准开发方式。 | 
					
						
							| 
									
										
										
										
											2016-10-17 22:48:18 +01:00
										 |  |  |  |   它很不错,但是有自己的缺点。   | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   JiT compilation incurs a runtime performance penalty.  | 
					
						
							|  |  |  |  |   Views take longer to render because of the in-browser compilation step.  | 
					
						
							|  |  |  |  |   The application is bigger because it includes the Angular compiler  | 
					
						
							|  |  |  |  |   and a lot of library code that the application won't actually need. | 
					
						
							|  |  |  |  |   Bigger apps take longer to transmit and are slower to load. | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   JiT编译导致运行期间的性能损耗。 | 
					
						
							|  |  |  |  |   由于需要在浏览器中的这个编译过程,视图需要花更长时间才能渲染出来。 | 
					
						
							|  |  |  |  |   由于应用包含了Angular编译器以及大量实际上并不需要的库代码,所以文件体积也会更大。 | 
					
						
							|  |  |  |  |   更大的应用需要更长的时间进行传输,加载也更慢。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Compilation can uncover many component-template binding errors. | 
					
						
							|  |  |  |  |   JiT compilation discovers them at runtime which is later than we'd like. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   编译可以揭露一些组件模板绑定错误。JiT变异在运行时才揭露它们,有点太晚了。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   The **_Ahead-of-Time_ (AoT) compiler** can catch template errors early and improve performance  | 
					
						
							|  |  |  |  |   by compiling at build time as you'll learn in this chapter. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **预编译**(AoT)通过在构建时编译,在早期截获模板错误,提高应用性能。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#aot-jit | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## _Ahead-of-time_ (AoT) vs _Just-in-time_ (JiT) | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   ## 预编译(AoT) vs 即时编译(JiT) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   There is actually only one Angular compiler. The difference between AoT and JiT is a matter of timing and tooling.  | 
					
						
							|  |  |  |  |   With AoT, the compiler runs once at build time using one set of libraries;  | 
					
						
							|  |  |  |  |   With JiT it runs every time for every user at runtime using a different set of libraries.   | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   事实上只有一个Angular编译器,AoT和JiT之间的差别仅仅在于编译的时机和所用的工具。 | 
					
						
							|  |  |  |  |   使用AoT,编译器仅仅使用一组库在构建期间运行一次;使用JiT,编译器在每个用户的每次运行期间都要用不同的库运行一次。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   ### Why do AoT compilation? | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ### 为什么需要AoT编译? | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *Faster rendering* | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **渲染得更快** | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   With AoT, the browser downloads a pre-compiled version of the application.  | 
					
						
							|  |  |  |  |   The browser loads executable code so it can render the application immediately, without waiting to compile the app first. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   使用AoT,浏览器下载预编译版本的应用程序。 | 
					
						
							|  |  |  |  |   浏览器直接加载运行代码,所以它可以立即渲染该应用,而不用等应用完成首次编译。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *Fewer asynchronous requests* | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **需要的异步请求更少** | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   The compiler _inlines_ external html templates and css style sheets within the application JavaScript, | 
					
						
							|  |  |  |  |   eliminating separate ajax requests for those source files. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   编译器把外部html模板和css样式表内联到了该应用的JavaScript中。 | 
					
						
							|  |  |  |  |   消除了用来下载那些源文件的Ajax请求。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *Smaller Angular framework download size* | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **需要下载的Angular框架体积更小** | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   There's no need to download the Angular compiler if the app is already compiled.  | 
					
						
							|  |  |  |  |   The compiler is roughly half of Angular itself, so omitting it dramatically reduces the application payload. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   如果应用已经编译过了,自然不需要再下载Angular编译器了。 | 
					
						
							|  |  |  |  |   该编译器差不多占了Angular自身体积的一半儿,所以,省略它可以显著减小应用的体积。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *Detect template errors earlier* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **提早检测模板错误** | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   The AoT compiler detects and reports template binding errors during the build step | 
					
						
							|  |  |  |  |   before users can see them. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   AoT编译器在构建过程中检测和报告模板绑定错误,避免用户遇到这些错误。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *Better security* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   **更安全** | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   AoT compiles HTML templates and components into JavaScript files long before they are served to the client. | 
					
						
							|  |  |  |  |   With no templates to read and no risky client-side HTML or JavaScript evaluation, | 
					
						
							|  |  |  |  |   there are fewer opportunities for injection attacks. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   AoT编译远在HTML模版和组件被服务到客户端之前,将它们编译到JavaScript文件。 | 
					
						
							|  |  |  |  |   没有模版可以阅读,没有高风险客户端HTML或JavaScript可利用,所以注入攻击的机会较少。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | a#compile | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Compile with AoT | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ## 用AoT进行编译 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   ### Prepare for offline compilation | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ### 为离线编译做准备 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Take the <a href='/docs/ts/latest/quickstart.html'>QuickStart</a> as a starting point. | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   A few minor changes to the lone `app.component` lead to these two class and html files: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   本烹饪书以<a href='/docs/ts/latest/quickstart.html'>“快速起步”</a>作为起始点。 | 
					
						
							|  |  |  |  |   只要单独对`app.component`文件的类文件和html文件做少量修改就可以了。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `cb-aot-compiler/ts/app/app.component.html, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/app/app.component.ts`, | 
					
						
							|  |  |  |  |   null, | 
					
						
							|  |  |  |  |   `app/app.component.html, | 
					
						
							|  |  |  |  |    app/app.component.ts` | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | )(format='.') | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   Install a few new npm dependencies with the following command:  | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   用下列命令安装少量新的npm依赖: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							| 
									
										
										
										
											2016-09-14 21:39:53 +02:00
										 |  |  |  |   npm install @angular/compiler-cli @angular/platform-server --save | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   You will run the `ngc` compiler provided in the `@angular/compiler-cli` npm package | 
					
						
							|  |  |  |  |   instead of the TypeScript compiler (`tsc`).  | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   你要用`@angular/compiler-cli`包中提供的`ngc`编译器来代替TypeScript编译器(`tsc`)。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   `ngc` is a drop-in replacement for `tsc` and is configured much the same way.   | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `ngc`是一个`tsc`的高仿替代品,它们的配置方式几乎完全一样。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   `ngc` requires its own `tsconfig.json` with AoT-oriented settings. | 
					
						
							|  |  |  |  |   Copy the original `tsconfig.json` to a file called `tsconfig-aot.json`, then modify it to look as follows. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `ngc`需要自己的带有AoT专用设置的`tsconfig.json`。 | 
					
						
							|  |  |  |  |   把原始的`tsconfig.json`拷贝到一个名叫`tsconfig-aot.json`的文件中,然后像这样修改它: | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | +makeExample('cb-aot-compiler/ts/tsconfig-aot.json', null, 'tsconfig-aot.json')(format='.') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   The `compilerOptions` section is unchanged except for one property.  | 
					
						
							|  |  |  |  |   **Set the `module` to `es2015`**.  | 
					
						
							|  |  |  |  |   This is important as explained later in the [Tree Shaking](#tree-shaking) section. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `compilerOptions`部分只修改了一个属性:**把`module`设置为`es2015`。 | 
					
						
							|  |  |  |  |   这一点非常重要,我们会在后面的[摇树优化](#tree-shaking)部分解释为什么。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   What's really new is the `ngc` section at the bottom called `angularCompilerOptions`.  | 
					
						
							|  |  |  |  |   Its `"genDir"` property tells the compiler  | 
					
						
							|  |  |  |  |   to store the compiled output files in a new `aot` folder. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `ngc`区真正新增的内容是底部的`angularCompilerOptions`。 | 
					
						
							|  |  |  |  |   它的`"genDir"`属性告诉编译器把编译结果保存在新的`aot`目录下。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   The `"skipMetadataEmit" : true` property prevents the compiler from generating metadata files with the compiled application.  | 
					
						
							|  |  |  |  |   Metadata files are not necessary when targeting TypeScript files, so there is no reason to include them. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `"skipMetadataEmit" : true`属性阻止编译器为编译后的应用生成元数据文件。 | 
					
						
							|  |  |  |  |   当输出成TypeScript文件时,元数据并不是必须的,因此不需要包含它们。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   ### Compiling the application | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ### 编译该应用 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Initiate AoT compilation from the command line using the previously installed `ngc` compiler by executing: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   在命令行中执行下列命令,借助刚安装好的`ngc`编译器来启动AoT编译: | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   node_modules/.bin/ngc -p tsconfig-aot.json | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     Windows users should surround the `ngc` command in double quotes: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     Windows用户应该双引号`ngc`命令: | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   code-example(format='.'). | 
					
						
							|  |  |  |  |     "node_modules/.bin/ngc" -p tsconfig-aot.json | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   `ngc` expects the `-p` switch to point to a `tsconfig.json` file or a folder containing a `tsconfig.json` file. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   `ngc`希望`-p`选项指向一个`tsconfig.json`文件,或者一个包含`tsconfig.json`文件的目录。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   After `ngc` completes, look for a collection of _NgFactory_ files in the `aot` folder (the folder specified as `genDir` in `tsconfig-aot.json`).  | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   在`ngc`完成时,会在`aot`目录下看到一组*NgFactory*文件(该目录是在`tsconfig-aot.json`的`genDir`属性中指定的)。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   These factory files are essential to the compiled application.  | 
					
						
							|  |  |  |  |   Each component factory creates an instance of the component at runtime by combining the original class file | 
					
						
							|  |  |  |  |   and a JavaScript representation of the component's template. | 
					
						
							|  |  |  |  |   Note that the original component class is still referenced internally by the generated factory. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这些工厂文件对于编译后的应用是必要的。 | 
					
						
							|  |  |  |  |   每个组件工厂都可以在运行时创建一个组件的实例,其中带有一个原始的类文件和一个用JavaScript表示的组件模板。 | 
					
						
							|  |  |  |  |   注意,原始的组件类依然是由所生成的这个工厂进行内部引用的。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     The curious can open the `aot/app.component.ngfactory.ts` to see the original Angular template syntax | 
					
						
							|  |  |  |  |     in its intermediate, compiled-to-TypeScript form. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     如果你好奇,可以打开`aot/app.component.ngfactory.ts`来看看原始Angular模板语法被编译成TypeScript时的中间结果。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     JiT compilation generates these same _NgFactories_ in memory where they are largely invisible. | 
					
						
							|  |  |  |  |     AoT compilation reveals them as separate, physical files. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     JiT编译器在内存中同样会生成这一堆*NgFactory*,但它们大部分是不可见的。 | 
					
						
							|  |  |  |  |     AoT编译器则会生成在单独的物理文件中。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  | .alert.is-important | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     Do not edit the _NgFactories_! Re-compilation replaces these files and all edits will be lost.  | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							| 
									
										
										
										
											2016-09-22 15:27:49 +08:00
										 |  |  |  |     不要编辑这些*NgFactory*!重新编译时会替换这些文件,你做的所有修改都会丢失。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#bootstrap | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Bootstrap | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   ## 引导 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   The AoT path changes application bootstrapping. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   AoT也改变了应用的引导方式。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Instead of bootstrapping `AppModule`, you bootstrap the application with the generated module factory, `AppModuleNgFactory`. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   引导的方式从引导`AppModule`改成了引导生成的模块工厂:`AppModuleNgFactory`。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Switch from the `platformBrowserDynamic.bootstrap` used in JiT compilation to   | 
					
						
							|  |  |  |  |   `platformBrowser().bootstrapModuleFactory` and pass in the `AppModuleNgFactory`.     | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   从使用JiT编译时的`platformBrowserDynamic.bootstrap`换成了`platformBrowser().bootstrapModuleFactory`,并把`AppModuleNgFactory`传进去。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Here is AoT bootstrap in `main.ts` next to the familiar JiT version: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这里是AoT版本`main.ts`中的引导过程,下一个是你所熟悉的JiT版本。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `cb-aot-compiler/ts/app/main.ts, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/app/main-jit.ts`, | 
					
						
							|  |  |  |  |   null, | 
					
						
							|  |  |  |  |   `app/main.ts (AoT), | 
					
						
							|  |  |  |  |    app/main.ts (JiT)` | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   Be sure to recompile with `ngc`! | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   确保用`ngc`进行重新编译! | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#tree-shaking | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Tree Shaking | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   ## 摇树优化(Tree Shaking) | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   AoT compilation sets the stage for further optimization through a process called _Tree Shaking_. | 
					
						
							|  |  |  |  |   A Tree Shaker walks the dependency graph, top to bottom, and _shakes out_ unused code like | 
					
						
							|  |  |  |  |   dead needles in a Christmas tree. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   AoT编译为接下来通过一个叫做*摇树优化*的过程做好了准备。 | 
					
						
							|  |  |  |  |   摇树优化器从上到下遍历依赖图谱,并且*摇掉*用不到的代码,这些代码就像是圣诞树中那些死掉的松针一样。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Tree Shaking can greatly reduce the downloaded size of the application  | 
					
						
							|  |  |  |  |   by removing unused portions of both source and library code.  | 
					
						
							|  |  |  |  |   In fact, most of the reduction in small apps comes from removing unreferenced Angular features.  | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   通过移除源码和库代码中用不到的部分,摇树优化可以大幅缩减应用的下载体积。 | 
					
						
							|  |  |  |  |   事实上,在小型应用中大部分的缩减都是因为筛掉了那些没用到的Angular特性。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   For example, this demo application doesn't use anything from the `@angular/forms` library. | 
					
						
							|  |  |  |  |   There is no reason to download Forms-related Angular code and tree shaking ensures that you don't. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   例如,这个演示程序中没有用到`@angular/forms`库中的任何东西,那么也就没有理由去下载这些与表单有关的Angular代码了。摇树优化可以帮你确保这一点。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Tree Shaking and AoT compilation are separate steps.  | 
					
						
							|  |  |  |  |   Tree Shaking can only target JavaScript code.  | 
					
						
							|  |  |  |  |   AoT compilation converts more of the application to JavaScript,  | 
					
						
							|  |  |  |  |   which in turn makes more of the application "Tree Shakable". | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   摇树优化和AoT编译是单独的步骤。 | 
					
						
							|  |  |  |  |   摇树优化不仅针对JavaScript代码。 | 
					
						
							|  |  |  |  |   AoT编译会把应用中的大部分都转换成JavaScript,这种转换会让应用更容易被“摇树优化”。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   ### Rollup | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   This cookbook illustrates a Tree Shaking utility called _Rollup_. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这个烹饪宝典中用来示范的摇树优化工具是*Rollup*。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Rollup statically analyzes the application by following the trail of `import` and `export` statements. | 
					
						
							|  |  |  |  |   It produces a final code _bundle_ that excludes code that is exported, but never imported. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   Rollup会通过跟踪`import`和`export`语句来对本应用进行静态分析。 | 
					
						
							|  |  |  |  |   它所生成的最终代码*捆*中会排除那些被导出过但又从未被导入的代码。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Rollup can only Tree Shake `ES2015` modules which have `import` and `export` statements. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   Rollup只能对`ES2015`模块摇树,因为那里有`import`和`export`语句。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     Recall that `tsconfig-aot.json` is configured to produce `ES2015` modules.  | 
					
						
							|  |  |  |  |     It's not important that the code itself be written with `ES2015` syntax such as `class` and `const`.  | 
					
						
							|  |  |  |  |     What matters is that the code uses ES `import` and `export` statements rather than `require` statements. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     回忆一下,`tsconfig-aot.json`中曾配置为生成`ES2015`的模块。 | 
					
						
							|  |  |  |  |     代码本身是否用到了`ES2015`语法(例如`class`和`const`)并不重要,重要的是这些代码使用的应该是`import`和`export`语句,而不是`require`语句。 | 
					
						
							|  |  |  |  |      | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   Install the Rollup dependencies with this command: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   通过下列命令安装Rollup依赖: | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   npm install rollup rollup-plugin-node-resolve rollup-plugin-commonjs rollup-plugin-uglify --save-dev | 
					
						
							|  |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Next, create a configuration file (`rollup-config.js`) | 
					
						
							|  |  |  |  |   in the project root directory to tell Rollup how to process the application.  | 
					
						
							|  |  |  |  |   The cookbook configuration file looks like this. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   接下来,在项目根目录新建一个配置文件(`rollup-config.js`),来告诉Rollup如何处理应用。 | 
					
						
							|  |  |  |  |   本烹饪书配置文件是这样的: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeExample('cb-aot-compiler/ts/rollup-config.js', null, 'rollup-config.js')(format='.') | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   It tells Rollup that the app entry point is `app/main.js` . | 
					
						
							|  |  |  |  |   The `dest` attribute tells Rollup to create a bundle called `build.js` in the `dist` folder. | 
					
						
							|  |  |  |  |   Then there are plugins. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   它会告诉Rollup,该应用的入口点是`app/main.js`。 | 
					
						
							|  |  |  |  |   `dest`属性告诉Rollup要在`dist`目录下创建一个名叫`build.js`的捆文件。 | 
					
						
							|  |  |  |  |   然后是插件。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ### Rollup Plugins | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ### Rollup插件 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Optional plugins filter and transform the Rollup inputs and output. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这些可选插件过滤并转换Rollup的输入和输出。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   *RxJS* | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   *RxJS* | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   Rollup expects application source code to use `ES2015` modules.  | 
					
						
							|  |  |  |  |   Not all external dependencies are published as `ES2015` modules. | 
					
						
							|  |  |  |  |   In fact, most are not. Many of them are published as _CommonJS_ modules. | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   Rollup期望应用的源码使用`ES2015`模块。 | 
					
						
							|  |  |  |  |   但并不是所有外部依赖都发布成了`ES2015`模块。 | 
					
						
							|  |  |  |  |   事实上,大多数都不是。它们大多数都发布成了*CommonJS*模块。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   The _RxJs_ observable library is an essential Angular dependency published as an ES5 JavaScript _CommonJS_ module. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-17 15:15:00 +01:00
										 |  |  |  |   可观察对象库*RxJS*是Angular所依赖的基础之一,它就是发布成了ES5 JavaScript的*CommonJS*模块。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Luckily there is a Rollup plugin that modifies _RxJs_  | 
					
						
							|  |  |  |  |   to use the ES `import` and `export` statements that Rollup requires. | 
					
						
							|  |  |  |  |   Rollup then preserves in the final bundle the parts of `RxJS` referenced by the application. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   幸运的是,有一个Rollup插件,它会修改*RxJS*,以使用Rollup所需的ES`import`和`export`语句。 | 
					
						
							|  |  |  |  |   然后Rollup就可以把该应用中用到的那部分`RxJS`代码留在“捆”文件中了。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeExample('cb-aot-compiler/ts/rollup-config.js','commonjs','rollup-config.js (CommonJs to ES2015 Plugin)')(format='.')      | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   *Minification*  | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   *最小化*  | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Rollup Tree Shaking reduces code size considerably.  Minification makes it smaller still. | 
					
						
							|  |  |  |  |   This cookbook relies on the _uglify_ Rollup plugin to minify and mangle the code. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   Rollup做摇树优化时会大幅减小代码体积。最小化过程则会让它更小。 | 
					
						
							|  |  |  |  |   本烹饪宝典依赖于Rollup插件*uglify*来最小化并混淆代码。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeExample('cb-aot-compiler/ts/rollup-config.js','uglify','rollup-config.js (CommonJs to ES2015 Plugin)')(format='.')  | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     In a production setting, you would also enable gzip on the web server to compress | 
					
						
							|  |  |  |  |     the code into an even smaller package going over the wire. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     在生产环境中,我们还应该打开Web服务器的gzip特性来把代码压缩得更小。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ### Run Rollup | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   ### 运行Rollup | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   Execute the Rollup process with this command: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   通过下列命令执行Rollup过程: | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   node_modules/.bin/rollup -c rollup-config.js | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     Rollup may log many lines with the following warning message: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     Rollup可能会记录很多行下面这样的警告信息: | 
					
						
							|  |  |  |  |      | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   code-example(format='.', language='bash'). | 
					
						
							|  |  |  |  |     The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     You can safely ignore these warnings. | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     你可以放心的忽略这些警告。 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#load | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Load the Bundle | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ## 加载捆文件 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Loading the generated application bundle does not require a module loader like SystemJS.  | 
					
						
							|  |  |  |  |   Remove the scripts that concern SystemJS. | 
					
						
							|  |  |  |  |   Instead, load the bundle file using a single `script` tag: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   加载所生成的应用捆文件,并不需要使用像SystemJS这样的模块加载器。 | 
					
						
							|  |  |  |  |   移除与SystemJS有关的那些脚本吧。 | 
					
						
							|  |  |  |  |   改用`script`标签来加载这些捆文件: | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | +makeExample('cb-aot-compiler/ts/index.html','bundle','index.html (load bundle)')(format='.')  | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | a#serve | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Serve the app | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ## 启动应用服务器 | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   You'll need a web server to host the application. | 
					
						
							|  |  |  |  |   Use the same _Lite Server_ employed elsewhere in the documentation: | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   你需要一个Web服务器来作为应用的宿主。 | 
					
						
							|  |  |  |  |   像与文档中其它部分一样,用*Lite Server*吧: | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   npm run lite | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   The server starts, launches a browser, and the app should appear. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 18:13:42 +08:00
										 |  |  |  |   启动了服务器、打开浏览器,应用就出现了。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | a#source-code | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   ## AoT QuickStart Source Code | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   ## AoT快速开始源代码 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Here's the pertinent source code: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   下面是相关源代码: | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `cb-aot-compiler/ts/app/app.component.html, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/app/app.component.ts, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/app/main.ts, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/index.html, | 
					
						
							|  |  |  |  |    cb-aot-compiler/ts/tsconfig-aot.json, | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |    cb-aot-compiler/ts/rollup-config.js`, | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  |   null, | 
					
						
							|  |  |  |  |   `app/app.component.html, | 
					
						
							|  |  |  |  |    app/app.component.ts, | 
					
						
							|  |  |  |  |    app/main.ts, | 
					
						
							|  |  |  |  |    index.html, | 
					
						
							|  |  |  |  |    tsconfig-aot.json, | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |    rollup-config.js` | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | a#toh | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ## Tour of Heroes | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ## 英雄指南 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   The sample above is a trivial variation of the QuickStart app.  | 
					
						
							|  |  |  |  |   In this section you apply what you've learned about AoT compilation and Tree Shaking  | 
					
						
							|  |  |  |  |   to an app with more substance, the tutorial [_Tour of Heroes_](../tutorial/toh-pt6.html). | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   上面的例子是快速开始应用的一个简单的变体。 | 
					
						
							|  |  |  |  |   在本节中,你将在一个更多内容的应用 - [英雄指南](../tutorial/toh-pt6.html)上使用从AoT编译和摇树优化学到的知识。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   ### JiT in development, AoT in production | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   ### 开发器使用JiT, 产品期使用AoT | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Today AoT compilation and Tree Shaking take more time than is practical for development. That will change soon. | 
					
						
							|  |  |  |  |   For now, it's best to JiT compile in development and switch to AoT compilation before deploying to production. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   目前,AoT编译和摇树优化对开发来说,占用的时间太多了。这将在未来得到改变。 | 
					
						
							|  |  |  |  |   当前的最佳实践是在开发器使用JiT编译,然后在发布产品前切换到AoT编译。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Fortunately, the source code can be compiled either way without change _if_ you account for a few key differences. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   幸运的是,**如果**你处理了几个关键不同点,源代码可以在没有任何变化时,采取两种方式的任何一种都能编译。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   ***Index.html*** | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   ***Index.html*** | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   The JiT and AoT apps are setup and launched so differently that they require their own `index.html` files.  | 
					
						
							|  |  |  |  |   Here they are for comparison: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   JiT和AoT应用的设置和加载非常不一样,以至于它们需要自己单独的`index.html`文件。 | 
					
						
							|  |  |  |  |   下面是它们的比较: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `toh-6/ts/aot/index.html, | 
					
						
							|  |  |  |  |    toh-6/ts/index.html`, | 
					
						
							|  |  |  |  |   null, | 
					
						
							|  |  |  |  |   `aot/index.html (AoT), | 
					
						
							|  |  |  |  |    index.html (JiT)` | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   They can't be in the same folder.  | 
					
						
							|  |  |  |  |   ***Put the AoT version in the `/aot` folder***. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   它们不能在同一个目录。 | 
					
						
							|  |  |  |  |   ***将AoT版本放置到`/aot`目录***。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   The JiT version relies on `SystemJS` to load individual modules and requires the `reflect-metadata` shim.  | 
					
						
							|  |  |  |  |   Both scripts appear in its `index.html`. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   JiT版本依靠`SystemJS`来加载单个模块,并需要`reflect-metadata`垫片。 | 
					
						
							|  |  |  |  |   所以它们出现在它的`index.html`中。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   The AoT version loads the entire application in a single script, `aot/dist/build.js`. | 
					
						
							|  |  |  |  |   It does not need `SystemJS` or the `reflect-metadata` shim; those scripts are absent from its `index.html` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   AoT版本用一个单独的脚本来加载整个应用 - `aot/dist/build.js`。它不需要`SystemJS`和`reflect-metadata`垫片,所以它们不会出现在`index.html`中。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   *Component-relative Template URLS* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   *相对组件的模板路径* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   The AoT compiler requires that `@Component` URLS for external templates and css files be _component-relative_. | 
					
						
							|  |  |  |  |   That means that the value of `@Component.templateUrl` is a URL value relative to the component class file: | 
					
						
							|  |  |  |  |   `foo.component.html` no matter where `foo.component.ts` sits in the project folder structure. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   AoT编译器要求`@Component`外部模板和CSS文件的路径是相对组件的。 | 
					
						
							|  |  |  |  |   意思是,`@Component.templateUrl`的值是一个相对组件类文件`foo.component.html`的路径,不管`foo.component.ts`在项目的哪个目录。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   While JiT app URLs are more flexible, stick with _component-relative_ URLs for compatibility with AoT compilation. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   JiT应用的URLs更加灵活,但是为了与AoT编译兼容,坚持使用**相对组件**路径。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   JiT-compiled apps, using the SystemJS loader and _component-relative_ URLs *must set the* `@Component.moduleId` *property to* `module.id`. | 
					
						
							|  |  |  |  |   The `module` object is undefined when an AoT-compiled app runs.  | 
					
						
							|  |  |  |  |   The app fails unless you assign a global `module` value in the `index.html` like this: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   JiT编译的应用,使用SystemJS加载器,**相对组件路径**必须要设置`@Component.moduleId`*属性*为`module.id`。 | 
					
						
							|  |  |  |  |   `module`对象在AoT编译的应用运行时的值为`undefined`。 | 
					
						
							|  |  |  |  |   应用将会失败,除非你像这样,在`index.html`中指定一个全局`module`值: | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeExample('toh-6/ts/aot/index.html','moduleId')(format='.')   | 
					
						
							|  |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     Setting a global `module` is a temporary expedient.  | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     设置一个全局`module`是暂时的权宜之计。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   *TypeScript configuration* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   *TypeScript配置* | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   JiT-compiled apps transpile to `commonjs` modules. | 
					
						
							|  |  |  |  |   AoT-compiled apps transpile to _ES2015_/_ES6_ modules to facilitate Tree Shaking.  | 
					
						
							|  |  |  |  |   AoT requires its own TypeScript configuration settings as well. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   JiT编译的应用编译为`commonjs`模块。 | 
					
						
							|  |  |  |  |   AoT编译的应用编译为**ES2015/ES6**模块,用来支持摇树优化。 | 
					
						
							|  |  |  |  |   而且AoT需要它自己的TypeScript配置设置。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   You'll need separate TypeScript configuration files such as these: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   你将需要单独的TypeScript配置文件,像这些: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `toh-6/ts/tsconfig-aot.json, | 
					
						
							|  |  |  |  |    toh-6/ts/tsconfig.json`, | 
					
						
							|  |  |  |  |   null, | 
					
						
							| 
									
										
										
										
											2016-10-18 16:11:24 +03:00
										 |  |  |  |   `tsconfig-aot.json (AoT), | 
					
						
							|  |  |  |  |    tsconfig.json (JiT)` | 
					
						
							| 
									
										
										
										
											2016-09-14 13:10:04 -04:00
										 |  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ### Tree Shaking | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   ### 摇树优化 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Rollup does the Tree Shaking as before. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Rollup和以前一样,仍然进行摇树优化。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |    | 
					
						
							|  |  |  |  | +makeExample('toh-6/ts/rollup-config.js',null,'rollup-config.js')(format='.')   | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ### Running the application | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   ### 运行应用 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | .alert.is-important | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     The general audience instructions for running the AoT build of the Tour of Heroes app are not ready. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |     面向大众的运行AoT构建的英雄指南应用的说明还没有准备好。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |     The following instructions presuppose that you have cloned the  | 
					
						
							|  |  |  |  |     <a href="https://github.com/angular/angular.io" target="_blank">angular.io</a>  | 
					
						
							|  |  |  |  |     github repository and prepared it for development as explained in the repo's README.md. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |     下面的说明假设你克隆了<a href="https://github.com/angular/angular.io" target="_blank">angular.io</a> Github库,并按照该库的README.md准备了开发环境。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |     The _Tour of Heroes_ source code is in the `public/docs/_examples/toh-6/ts` folder. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     **英雄指南**源代码在`public/docs/_examples/toh-6/ts`目录。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   Run the JiT-compiled app with `npm start` as for all other JiT examples. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   和其他JiT例子一样,使用`npm start`命令,运行JiT编译的应用: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Compiling with AoT presupposes certain supporting files, most of them discussed above. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   AoT编译假设上面介绍的一些支持文件都以准备好。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | +makeTabs( | 
					
						
							|  |  |  |  |   `toh-6/ts/aot/index.html, | 
					
						
							|  |  |  |  |    toh-6/ts/aot/bs-config.json, | 
					
						
							|  |  |  |  |    toh-6/ts/copy-dist-files.js, | 
					
						
							|  |  |  |  |    toh-6/ts/rollup-config.js, | 
					
						
							|  |  |  |  |    toh-6/ts/tsconfig-aot.json`, | 
					
						
							|  |  |  |  |   null, | 
					
						
							|  |  |  |  |   `aot/index.html, | 
					
						
							|  |  |  |  |    aot/bs-config.json, | 
					
						
							|  |  |  |  |    copy-dist-files.js, | 
					
						
							|  |  |  |  |    rollup-config.js, | 
					
						
							|  |  |  |  |    tsconfig-aot.json`) | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   Extend the `scripts` section of the `package.json` with these npm scripts: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   使用下面的npm脚本,扩展`package.json`文件的`scripts`部分: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   "build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup-config.js", | 
					
						
							|  |  |  |  |   "lite:aot": "lite-server -c aot/bs-config.json", | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   Copy the AoT distribution files into the `/aot` folder with the node script: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   使用下面的node脚本,拷贝AoT发布文件到`/aot/`目录: | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   node copy-dist-files | 
					
						
							|  |  |  |  | .l-sub-section | 
					
						
							|  |  |  |  |   :marked | 
					
						
							|  |  |  |  |     You won't do that again until there are updates to `zone.js` or the `core-js` shim for old browsers. | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     直到`zone.js`或者支持老版本浏览器的`core-js`垫片有更新,你不需要再这样做。 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   Now AoT-compile the app and launch it with the `lite` server: | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   现在AoT编译应用,并使用`lite`服务器启动它: | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   npm run build:aot && npm run lite:aot | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   ### Inspect the Bundle | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   ### 检查包裹 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   It's fascinating to see what the generated JavaScript bundle looks like after Rollup.  | 
					
						
							|  |  |  |  |   The code is minified, so you won't learn much from inspecting the bundle directly. | 
					
						
							|  |  |  |  |   But the <a href="https://github.com/danvk/source-map-explorer/blob/master/README.md" target="_blank">source-map-explorer</a>  | 
					
						
							|  |  |  |  |   tool can be quite revealing.  | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   看看Rollup之后生成的JavaScript包,非常神奇。 | 
					
						
							|  |  |  |  |   代码已经被最小化,所以你不会从中直接学到任何知识。 | 
					
						
							|  |  |  |  |   但是<a href="https://github.com/danvk/source-map-explorer/blob/master/README.md" target="_blank">source-map-explorer</a> 工具非常有用。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Install it: | 
					
						
							| 
									
										
										
										
											2016-10-18 19:24:18 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   安装: | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | code-example(format='.').   | 
					
						
							|  |  |  |  |   npm install source-map-explorer --save-dev | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   Run the following command to generate the map. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   运行下面的命令来生成源映射。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | code-example(format='.'). | 
					
						
							|  |  |  |  |   node_modules/.bin/source-map-explorer aot/dist/build.js | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | :marked | 
					
						
							|  |  |  |  |   The `source-map-explorer` analyzes the source map generated with the bundle and draws a map of all dependencies, | 
					
						
							|  |  |  |  |   showing exactly which application and Angular modules and classes are included in the bundle. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   `source-map-explorer`分析从包生成的源映射,并画出一个依赖地图,显示包中包含哪些应用程序和Angular模块和类。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  |   Here's the map for _Tour of Heroes_.  | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 16:54:21 +01:00
										 |  |  |  |   下面是英雄指南的地图: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 04:44:49 -04:00
										 |  |  |  | a(href="/resources/images/cookbooks/aot-compiler/toh6-bundle.png", target="_blank", title="View larger image") | 
					
						
							|  |  |  |  |   figure.image-display | 
					
						
							|  |  |  |  |     img(src="/resources/images/cookbooks/aot-compiler/toh6-bundle.png" alt="TOH-6-bundle")    |