angular-cn/packages/platform-server/src/server.ts

104 lines
4.0 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
import {ɵAnimationEngine} from '@angular/animations/browser';
import {DOCUMENT, PlatformLocation, ViewportScroller, ɵNullViewportScroller as NullViewportScroller, ɵPLATFORM_SERVER_ID as PLATFORM_SERVER_ID, ɵgetDOM as getDOM} from '@angular/common';
import {HttpClientModule} from '@angular/common/http';
import {Injector, NgModule, NgZone, Optional, PLATFORM_ID, PLATFORM_INITIALIZER, PlatformRef, Provider, RendererFactory2, StaticProvider, Testability, createPlatformFactory, platformCore, ɵALLOW_MULTIPLE_PLATFORMS as ALLOW_MULTIPLE_PLATFORMS, ɵsetDocument} from '@angular/core';
import {BrowserModule, EVENT_MANAGER_PLUGINS, ɵSharedStylesHost as SharedStylesHost} from '@angular/platform-browser';
import {ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
import {NoopAnimationsModule, ɵAnimationRendererFactory} from '@angular/platform-browser/animations';
import {DominoAdapter, parseDocument} from './domino_adapter';
import {SERVER_HTTP_PROVIDERS} from './http';
import {ServerPlatformLocation} from './location';
import {PlatformState} from './platform_state';
import {ServerEventManagerPlugin} from './server_events';
import {ServerRendererFactory2} from './server_renderer';
import {ServerStylesHost} from './styles_host';
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
function notSupported(feature: string): Error {
throw new Error(`platform-server does not support '${feature}'.`);
}
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: StaticProvider[] = [
{provide: DOCUMENT, useFactory: _document, deps: [Injector]},
{provide: PLATFORM_ID, useValue: PLATFORM_SERVER_ID},
{provide: PLATFORM_INITIALIZER, useFactory: initDominoAdapter, multi: true, deps: [Injector]}, {
provide: PlatformLocation,
useClass: ServerPlatformLocation,
deps: [DOCUMENT, [Optional, INITIAL_CONFIG]]
},
{provide: PlatformState, deps: [DOCUMENT]},
// Add special provider that allows multiple instances of platformServer* to be created.
{provide: ALLOW_MULTIPLE_PLATFORMS, useValue: 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
];
function initDominoAdapter(injector: Injector) {
return () => { DominoAdapter.makeCurrent(); };
}
export function instantiateServerRendererFactory(
renderer: RendererFactory2, engine: ɵAnimationEngine, zone: NgZone) {
return new ɵAnimationRendererFactory(renderer, engine, zone);
}
export const SERVER_RENDER_PROVIDERS: Provider[] = [
ServerRendererFactory2,
{
provide: RendererFactory2,
useFactory: instantiateServerRendererFactory,
deps: [ServerRendererFactory2, ɵAnimationEngine, NgZone]
},
ServerStylesHost,
{provide: SharedStylesHost, useExisting: ServerStylesHost},
{provide: EVENT_MANAGER_PLUGINS, multi: true, useClass: ServerEventManagerPlugin},
];
/**
* The ng module for the server.
*
* @publicApi
*/
@NgModule({
exports: [BrowserModule],
imports: [HttpClientModule, NoopAnimationsModule],
providers: [
SERVER_RENDER_PROVIDERS,
SERVER_HTTP_PROVIDERS,
{provide: Testability, useValue: null},
{provide: ViewportScroller, useClass: NullViewportScroller},
],
})
export class ServerModule {
}
function _document(injector: Injector) {
let config: PlatformConfig|null = injector.get(INITIAL_CONFIG, null);
const document = config && config.document ? parseDocument(config.document, config.url) :
getDOM().createHtmlDocument();
// Tell ivy about the global document
ɵsetDocument(document);
return document;
}
/**
* @publicApi
*/
export const platformServer: (extraProviders?: StaticProvider[] | undefined) => PlatformRef =
createPlatformFactory(platformCore, '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.
*
* @publicApi
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
*/
export const platformDynamicServer =
createPlatformFactory(platformCoreDynamic, 'serverDynamic', INTERNAL_SERVER_PLATFORM_PROVIDERS);