angular-cn/packages/compiler-cli
Alex Rickabaugh d4cee514f6 refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.

It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.

This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.

As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.

Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.

PR Close #26550
2018-10-19 09:23:05 -07:00
..
integrationtest build(bazel): update to rules_typescript 0.17.0 & rules_nodejs 0.13.4 (#25920) 2018-09-18 13:05:38 -07:00
src refactor(ivy): obviate the Bazel component of the ivy_switch (#26550) 2018-10-19 09:23:05 -07:00
test feat(ivy): i18n compiler support for i18nStart and i18nEnd instructions (#26442) 2018-10-17 11:03:52 -07:00
BUILD.bazel refactor(ivy): obviate the Bazel component of the ivy_switch (#26550) 2018-10-19 09:23:05 -07:00
README.md build: remove references to `tsc-wrapped` (#19298) 2017-09-21 13:55:52 -07:00
browser-rollup.config.js refactor: make all rollup config ES5 compatible (#20028) 2017-10-30 23:09:17 -04:00
index.ts fix(compiler-cli): don't report emit diagnostics when --noEmitOnError is off (#20063) 2017-11-02 14:49:38 -07:00
ngtools2.ts refactor(compiler-cli): expose ngtools api separately (#18978) 2017-08-31 14:37:13 -07:00
package.json feat: add support for TypeScript 3.1 (#26151) 2018-09-28 09:34:51 -07:00
tsconfig-build.json fix(bazel): ng_package should include private exports in fesms (#23054) 2018-03-29 14:11:12 -07:00
tsconfig-tools.json build: remove references to `tsc-wrapped` (#19298) 2017-09-21 13:55:52 -07:00
tsconfig.json build(compiler-cli): fix tsconfig.json circularity issue (#22722) 2018-03-15 21:18:07 -07:00

README.md

Angular Template Compiler

Angular applications are built with templates, which may be .html or .css files, or may be inline template attributes on Decorators like @Component.

These templates are compiled into executable JS at application runtime (except in interpretation mode). This compilation can occur on the client, but it results in slower bootstrap time, and also requires that the compiler be included in the code downloaded to the client.

You can produce smaller, faster applications by running Angular's compiler as a build step, and then downloading only the executable JS to the client.

Install and use

# First install angular, see https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02
$ npm install @angular/compiler-cli typescript@next @angular/platform-server @angular/compiler
# Optional sanity check, make sure TypeScript can compile.
$ ./node_modules/.bin/tsc -p path/to/project
# ngc is a drop-in replacement for tsc.
$ ./node_modules/.bin/ngc -p path/to/project

In order to write a bootstrap that imports the generated code, you should first write your top-level component, and run ngc once to produce a generated .ngfactory.ts file. Then you can add an import statement in the bootstrap allowing you to bootstrap off the generated code:

main.module.ts
-------------
import {BrowserModule} from '@angular/platform-browser';
import {Component, NgModule, ApplicationRef} from '@angular/core';

@Component(...)
export class MyComponent {}

@NgModule({
  imports: [BrowserModule],
  declarations: [MyComponent],
  entryComponents: [MyComponent]
})
export class MainModule {
  constructor(appRef: ApplicationRef) {
    appRef.bootstrap(MyComponent);
  }
}

bootstrap.ts
-------------

import {MainModuleNgFactory} from './main.module.ngfactory';
import {platformBrowser} from '@angular/platform-browser';

platformBrowser().bootstrapModuleFactory(MainModuleNgFactory);

Configuration

The tsconfig.json file may contain an additional configuration block:

 "angularCompilerOptions": {
   "genDir": ".",
   "debug": true
 }

genDir

the genDir option controls the path (relative to tsconfig.json) where the generated file tree will be written. If genDir is not set, then the code will be generated in the source tree, next to your original sources. More options may be added as we implement more features.

We recommend you avoid checking generated files into version control. This permits a state where the generated files in the repository were created from sources that were never checked in, making it impossible to reproduce the current state. Also, your changes will effectively appear twice in code reviews, with the generated version inscrutible by the reviewer.

In TypeScript 1.8, the generated sources will have to be written alongside your originals, so set genDir to the same location as your files (typicially the same as rootDir). Add **/*.ngfactory.ts and **/*.ngsummary.json to your .gitignore or other mechanism for your version control system.

In TypeScript 1.9 and above, you can add a generated folder into your application, such as codegen. Using the rootDirs option, you can allow relative imports like import {} from './foo.ngfactory' even though the src and codegen trees are distinct. Add **/codegen to your .gitignore or similar.

Note that in the second option, TypeScript will emit the code into two parallel directories as well. This is by design, see https://github.com/Microsoft/TypeScript/issues/8245. This makes the configuration of your runtime module loader more complex, so we don't recommend this option yet.

debug

Set the debug option to true to generate debug information in the generate files. Default to false.

See the example in the test/ directory for a working example.

Compiler CLI

This program mimics the TypeScript tsc command line. It accepts a -p flag which points to a tsconfig.json file, or a directory containing one.

This CLI is intended for demos, prototyping, or for users with simple build systems that run bare tsc.

Users with a build system should expect an Angular template plugin. Such a plugin would be based on the public_api.ts in this directory, but should share the TypeScript compiler instance with the one already used in the plugin for TypeScript typechecking and emit.

Design

At a high level, this program

  • collects static metadata about the sources
  • uses the OfflineCompiler from @angular/compiler to codegen additional .ts files
  • these .ts files are written to the genDir path, then compiled together with the application.

For developers

# Build Angular and the compiler
./build.sh

# Run the test once
# (First edit the LINKABLE_PKGS to use npm link instead of npm install)
$ ./scripts/ci/offline_compiler_test.sh

# Keep a package fresh in watch mode
./node_modules/.bin/tsc -p packages/compiler/tsconfig-build.json -w

# Recompile @angular/core module (needs to use tsc-ext to keep the metadata)
$ export NODE_PATH=${NODE_PATH}:$(pwd)/dist/all:$(pwd)/dist/tools
$ node dist/tools/@angular/compiler-cli/src/main -p packages/core/tsconfig-build.json

# Iterate on the test
$ cd /tmp/wherever/e2e_test.1464388257/
$ ./node_modules/.bin/ngc
$ ./node_modules/.bin/jasmine test/*_spec.js