refactor(core): change bootstrap of modules and names of platforms

BREAKING CHANGES:
- `browserPlatform`/`browserDynamicPlatform`/... have been deprecated and renamed into `platformBrowser`/`platformBrowserDynamic`/....
- `bootstrapModule` and `bootstrapModuleFactory` have been moved to be members of `PlaformRef`.
  E.g. `platformBrowserDynamic().bootstrapModule(MyModule)`.
This commit is contained in:
Tobias Bosch
2016-07-26 05:21:19 -07:00
parent 00b726f695
commit 5a21f168d6
27 changed files with 219 additions and 163 deletions

View File

@ -161,71 +161,6 @@ export function getPlatform(): PlatformRef {
return isPresent(_platform) && !_platform.disposed ? _platform : null;
}
/**
* Creates an instance of an `@NgModule` for the given platform
* for offline compilation.
*
* ## Simple Example
*
* ```typescript
* my_module.ts:
*
* @NgModule({
* imports: [BrowserModule]
* })
* class MyModule {}
*
* main.ts:
* import {MyModuleNgFactory} from './my_module.ngfactory';
* import {bootstrapModuleFactory} from '@angular/core';
* import {browserPlatform} from '@angular/platform-browser';
*
* let moduleRef = bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
* ```
*
* @experimental APIs related to application bootstrap are currently under review.
*/
export function bootstrapModuleFactory<M>(
moduleFactory: NgModuleFactory<M>, platform: PlatformRef): NgModuleRef<M> {
// Note: We need to create the NgZone _before_ we instantiate the module,
// as instantiating the module creates some providers eagerly.
// So we create a mini parent injector that just contains the new NgZone and
// pass that as parent to the NgModuleFactory.
const ngZone = new NgZone({enableLongStackTrace: isDevMode()});
const ngZoneInjector =
ReflectiveInjector.resolveAndCreate([{provide: NgZone, useValue: ngZone}], platform.injector);
return ngZone.run(() => moduleFactory.create(ngZoneInjector));
}
/**
* Creates an instance of an `@NgModule` for a given platform using the given runtime compiler.
*
* ## Simple Example
*
* ```typescript
* @NgModule({
* imports: [BrowserModule]
* })
* class MyModule {}
*
* let moduleRef = bootstrapModule(MyModule, browserPlatform());
* ```
* @stable
*/
export function bootstrapModule<M>(
moduleType: ConcreteType<M>, platform: PlatformRef,
compilerOptions: CompilerOptions | CompilerOptions[] = []): Promise<NgModuleRef<M>> {
const compilerFactory: CompilerFactory = platform.injector.get(CompilerFactory);
const compiler = compilerFactory.createCompiler(
compilerOptions instanceof Array ? compilerOptions : [compilerOptions]);
return compiler.compileModuleAsync(moduleType)
.then((moduleFactory) => bootstrapModuleFactory(moduleFactory, platform))
.then((moduleRef) => {
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
return appRef.waitForAsyncInitializers().then(() => moduleRef);
});
}
/**
* Shortcut for ApplicationRef.bootstrap.
* Requires a platform to be created first.
@ -261,8 +196,56 @@ export function coreLoadAndBootstrap(
*/
export abstract class PlatformRef {
/**
* Register a listener to be called when the platform is disposed.
* Creates an instance of an `@NgModule` for the given platform
* for offline compilation.
*
* ## Simple Example
*
* ```typescript
* my_module.ts:
*
* @NgModule({
* imports: [BrowserModule]
* })
* class MyModule {}
*
* main.ts:
* import {MyModuleNgFactory} from './my_module.ngfactory';
* import {browserPlatform} from '@angular/platform-browser';
*
* let moduleRef = browserPlatform().bootstrapModuleFactory(MyModuleNgFactory);
* ```
*
* @experimental APIs related to application bootstrap are currently under review.
*/
bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): NgModuleRef<M> {
throw unimplemented();
}
/**
* Creates an instance of an `@NgModule` for a given platform using the given runtime compiler.
*
* ## Simple Example
*
* ```typescript
* @NgModule({
* imports: [BrowserModule]
* })
* class MyModule {}
*
* let moduleRef = browserPlatform().bootstrapModule(MyModule);
* ```
* @stable
*/
bootstrapModule<M>(
moduleType: ConcreteType<M>,
compilerOptions: CompilerOptions|CompilerOptions[] = []): Promise<NgModuleRef<M>> {
throw unimplemented();
}
/**
*Register a listener to be called when the platform is disposed.
*/
abstract registerDisposeListener(dispose: () => void): void;
/**
@ -313,6 +296,31 @@ export class PlatformRef_ extends PlatformRef {
/** @internal */
_applicationDisposed(app: ApplicationRef): void { ListWrapper.remove(this._applications, app); }
bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): NgModuleRef<M> {
// Note: We need to create the NgZone _before_ we instantiate the module,
// as instantiating the module creates some providers eagerly.
// So we create a mini parent injector that just contains the new NgZone and
// pass that as parent to the NgModuleFactory.
const ngZone = new NgZone({enableLongStackTrace: isDevMode()});
const ngZoneInjector =
ReflectiveInjector.resolveAndCreate([{provide: NgZone, useValue: ngZone}], this.injector);
return ngZone.run(() => moduleFactory.create(ngZoneInjector));
}
bootstrapModule<M>(
moduleType: ConcreteType<M>,
compilerOptions: CompilerOptions|CompilerOptions[] = []): Promise<NgModuleRef<M>> {
const compilerFactory: CompilerFactory = this.injector.get(CompilerFactory);
const compiler = compilerFactory.createCompiler(
compilerOptions instanceof Array ? compilerOptions : [compilerOptions]);
return compiler.compileModuleAsync(moduleType)
.then((moduleFactory) => this.bootstrapModuleFactory(moduleFactory))
.then((moduleRef) => {
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
return appRef.waitForAsyncInitializers().then(() => moduleRef);
});
}
}
/**