2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-14 22:49:25 -04:00
|
|
|
import {PlatformLocation} from '@angular/common';
|
2016-07-18 06:50:31 -04:00
|
|
|
import {analyzeAppProvidersForDeprecatedConfiguration, coreDynamicPlatform} from '@angular/compiler';
|
|
|
|
import {ApplicationRef, CompilerFactory, ComponentRef, NgModule, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, Type, assertPlatform, bootstrapModule, corePlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
|
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
2016-06-14 22:49:25 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
import {Console, ReflectionCapabilities, reflector, wtfInit} from '../core_private';
|
2016-06-14 22:49:25 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
import {ConcreteType} from './facade/lang';
|
2016-06-14 22:49:25 -04:00
|
|
|
import {Parse5DomAdapter} from './parse5_adapter';
|
|
|
|
|
|
|
|
function notSupported(feature: string): Error {
|
|
|
|
throw new Error(`platform-server does not support '${feature}'.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ServerPlatformLocation extends PlatformLocation {
|
|
|
|
getBaseHrefFromDOM(): string { throw notSupported('getBaseHrefFromDOM'); };
|
|
|
|
onPopState(fn: any): void { notSupported('onPopState'); };
|
|
|
|
onHashChange(fn: any): void { notSupported('onHashChange'); };
|
|
|
|
get pathname(): string { throw notSupported('pathname'); }
|
|
|
|
get search(): string { throw notSupported('search'); }
|
|
|
|
get hash(): string { throw notSupported('hash'); }
|
|
|
|
replaceState(state: any, title: string, url: string): void { notSupported('replaceState'); };
|
|
|
|
pushState(state: any, title: string, url: string): void { notSupported('pushState'); };
|
|
|
|
forward(): void { notSupported('forward'); };
|
|
|
|
back(): void { notSupported('back'); };
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
2016-06-14 22:49:25 -04:00
|
|
|
{provide: PLATFORM_INITIALIZER, useValue: initParse5Adapter, multi: true},
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@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 {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
{provide: PlatformLocation, useClass: ServerPlatformLocation},
|
|
|
|
];
|
|
|
|
|
2016-06-14 22:49:25 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
/**
|
|
|
|
* A set of providers to initialize the Angular platform in a server.
|
|
|
|
*
|
|
|
|
* Used automatically by `serverBootstrap`, or can be passed to `platform`.
|
|
|
|
* @deprecated Use `serverPlatform()` or create a custom platform factory via
|
|
|
|
* `createPlatformFactory(serverPlatform, ...)`
|
|
|
|
*/
|
|
|
|
export const SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
|
|
|
|
[PLATFORM_COMMON_PROVIDERS, INTERNAL_SERVER_PLATFORM_PROVIDERS];
|
2016-06-14 22:49:25 -04:00
|
|
|
|
|
|
|
function initParse5Adapter() {
|
|
|
|
Parse5DomAdapter.makeCurrent();
|
|
|
|
wtfInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-07-18 06:50:31 -04:00
|
|
|
export const serverPlatform =
|
|
|
|
createPlatformFactory(corePlatform, 'server', INTERNAL_SERVER_PLATFORM_PROVIDERS);
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@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 {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The server platform that supports the runtime compiler.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export const serverDynamicPlatform =
|
2016-07-18 06:50:31 -04:00
|
|
|
createPlatformFactory(coreDynamicPlatform, 'serverDynamic', INTERNAL_SERVER_PLATFORM_PROVIDERS);
|
2016-06-14 22:49:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to bootstrap Angular in server environment (such as node).
|
|
|
|
*
|
|
|
|
* This version of bootstrap only creates platform injector and does not define anything for
|
|
|
|
* application injector. It is expected that application providers are imported from other
|
|
|
|
* packages such as `@angular/platform-browser` or `@angular/platform-browser-dynamic`.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* import {BROWSER_APP_PROVIDERS} from '@angular/platform-browser';
|
|
|
|
* import {BROWSER_APP_COMPILER_PROVIDERS} from '@angular/platform-browser-dynamic';
|
|
|
|
*
|
|
|
|
* serverBootstrap(..., [BROWSER_APP_PROVIDERS, BROWSER_APP_COMPILER_PROVIDERS])
|
|
|
|
* ```
|
|
|
|
*
|
2016-07-18 06:50:31 -04:00
|
|
|
* @deprecated create an {@link NgModule} and use {@link bootstrapModule} with the {@link
|
2016-07-08 16:40:54 -04:00
|
|
|
* serverDynamicPlatform}()
|
|
|
|
* instead.
|
2016-06-14 22:49:25 -04:00
|
|
|
*/
|
2016-07-18 06:50:31 -04:00
|
|
|
export function serverBootstrap<T>(
|
|
|
|
appComponentType: ConcreteType<T>,
|
|
|
|
customProviders: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<T>> {
|
2016-07-08 16:40:54 -04:00
|
|
|
console.warn(
|
2016-07-18 06:50:31 -04:00
|
|
|
'serverBootstrap is deprecated. Create an @NgModule and use `bootstrapModule` with the `serverDynamicPlatform()` instead.');
|
2016-06-14 22:49:25 -04:00
|
|
|
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
2016-07-18 06:50:31 -04:00
|
|
|
|
|
|
|
const deprecatedConfiguration = analyzeAppProvidersForDeprecatedConfiguration(customProviders);
|
|
|
|
const declarations = [deprecatedConfiguration.moduleDeclarations.concat([appComponentType])];
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
providers: customProviders,
|
|
|
|
declarations: declarations,
|
|
|
|
imports: [BrowserModule],
|
2016-07-25 03:36:30 -04:00
|
|
|
entryComponents: [appComponentType]
|
2016-07-18 06:50:31 -04:00
|
|
|
})
|
|
|
|
class DynamicModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootstrapModule(
|
|
|
|
DynamicModule, serverDynamicPlatform(), deprecatedConfiguration.compilerOptions)
|
|
|
|
.then((moduleRef) => {
|
|
|
|
const console = moduleRef.injector.get(Console);
|
|
|
|
deprecatedConfiguration.deprecationMessages.forEach((msg) => console.warn(msg));
|
|
|
|
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
|
|
|
return appRef.bootstrap(appComponentType);
|
|
|
|
});
|
2016-06-14 22:49:25 -04:00
|
|
|
}
|