feat(browser): use AppModules for bootstrap in the browser

This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:

```
@AppModule({
  modules: [BrowserModule],
  precompile: [MainComponent],
  providers: […], // additional providers
  directives: […], // additional platform directives
  pipes: […] // additional platform pipes
})
class MyModule {
  constructor(appRef: ApplicationRef) {
    appRef.bootstrap(MainComponent);
  }
}

// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);

// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```

The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.

Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
  public so that the offline compiler can resolve the token

BREAKING CHANGES:
- short form bootstrap does no longer allow
  to inject compiler internals (i.e. everything 
  from `@angular/compiler). Inject `Compiler` instead.
  To provide custom providers for the compiler,
  create a custom compiler via `browserCompiler({providers: [...]})`
  and pass that into the `bootstrap` method.
This commit is contained in:
Tobias Bosch
2016-06-30 13:07:17 -07:00
parent 74b45dfbf8
commit 3f55aa609f
71 changed files with 793 additions and 406 deletions

View File

@ -129,7 +129,7 @@ export declare abstract class ApplicationRef {
componentTypes: Type[];
injector: Injector;
zone: NgZone;
abstract bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C>;
abstract bootstrap<C>(componentFactory: ComponentFactory<C> | ConcreteType<C>): ComponentRef<C>;
abstract dispose(): void;
abstract registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void;
abstract registerDisposeListener(dispose: () => void): void;
@ -289,14 +289,8 @@ export declare class Compiler {
clearCacheFor(type: Type): void;
compileAppModuleAsync<T>(moduleType: ConcreteType<T>, metadata?: AppModuleMetadata): Promise<AppModuleFactory<T>>;
compileAppModuleSync<T>(moduleType: ConcreteType<T>, metadata?: AppModuleMetadata): AppModuleFactory<T>;
compileComponentAsync<T>(component: ConcreteType<T>, {moduleDirectives, modulePipes}?: {
moduleDirectives?: ConcreteType<any>[];
modulePipes?: ConcreteType<any>[];
}): Promise<ComponentFactory<T>>;
compileComponentSync<T>(component: ConcreteType<T>, {moduleDirectives, modulePipes}?: {
moduleDirectives?: ConcreteType<any>[];
modulePipes?: ConcreteType<any>[];
}): ComponentFactory<T>;
compileComponentAsync<T>(component: ConcreteType<T>): Promise<ComponentFactory<T>>;
compileComponentSync<T>(component: ConcreteType<T>): ComponentFactory<T>;
}
/** @stable */
@ -501,9 +495,6 @@ export declare function coreBootstrap<C>(componentFactory: ComponentFactory<C>,
/** @experimental */
export declare function coreLoadAndBootstrap(componentType: Type, injector: Injector): Promise<ComponentRef<any>>;
/** @experimental */
export declare function createNgZone(): NgZone;
/** @experimental */
export declare function createPlatform(injector: Injector): PlatformRef;
@ -1224,6 +1215,21 @@ export declare abstract class RootRenderer {
abstract renderComponent(componentType: RenderComponentType): Renderer;
}
/** @stable */
export declare abstract class SanitizationService {
abstract sanitize(context: SecurityContext, value: string): string;
}
/** @stable */
export declare enum SecurityContext {
NONE = 0,
HTML = 1,
STYLE = 2,
SCRIPT = 3,
URL = 4,
RESOURCE_URL = 5,
}
/** @stable */
export declare var Self: SelfMetadataFactory;

View File

@ -16,7 +16,7 @@ export declare function beforeEachProviders(fn: () => Array<any>): void;
/** @stable */
export declare class ComponentFixture<T> {
changeDetectorRef: ChangeDetectorRef;
componentInstance: any;
componentInstance: T;
componentRef: ComponentRef<T>;
debugElement: DebugElement;
elementRef: ElementRef;

View File

@ -1,5 +1,8 @@
/** @experimental */
export declare function bootstrap(appComponentType: Type, customProviders?: Array<any>): Promise<ComponentRef<any>>;
export declare function bootstrap<C>(appComponentType: ConcreteType<C>, customProviders?: Array<any>): Promise<ComponentRef<C>>;
/** @stable */
export declare function bootstrapModule<M>(moduleType: ConcreteType<M>, compiler?: Compiler): Promise<AppModuleRef<M>>;
/** @experimental */
export declare function bootstrapWorkerApp(appComponentType: Type, customProviders?: Array<any>): Promise<ComponentRef<any>>;
@ -10,5 +13,12 @@ export declare function bootstrapWorkerUi(workerScriptUri: string, customProvide
/** @experimental */
export declare const BROWSER_APP_COMPILER_PROVIDERS: Array<any>;
/** @stable */
export declare function browserCompiler({useDebug, useJit, providers}?: {
useDebug?: boolean;
useJit?: boolean;
providers?: Array<any>;
}): Compiler;
/** @experimental */
export declare const CACHED_TEMPLATE_PROVIDER: Array<any>;

View File

@ -1,3 +1,12 @@
/** @experimental */
export declare abstract class AnimationDriver {
abstract animate(element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[], duration: number, delay: number, easing: string): AnimationPlayer;
static NOOP: AnimationDriver;
}
/** @stable */
export declare function bootstrapModuleFactory<M>(moduleFactory: AppModuleFactory<M>): AppModuleRef<M>;
/** @experimental */
export declare const BROWSER_APP_PROVIDERS: Array<any>;
@ -7,6 +16,10 @@ export declare const BROWSER_PLATFORM_PROVIDERS: Array<any>;
/** @experimental */
export declare const BROWSER_SANITIZATION_PROVIDERS: Array<any>;
/** @stable */
export declare class BrowserModule {
}
/** @experimental */
export declare function browserPlatform(): PlatformRef;
@ -147,9 +160,6 @@ export interface SafeStyle extends SafeValue {
export interface SafeUrl extends SafeValue {
}
/** @experimental */
export declare var SecurityContext: typeof t.SecurityContext;
/** @experimental */
export declare abstract class ServiceMessageBroker {
abstract registerMethod(methodName: string, signature: Type[], method: Function, returnType?: Type): void;