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
|
|
|
|
*/
|
|
|
|
|
2017-03-13 20:31:03 -04:00
|
|
|
import {ɵAnimationEngine} from '@angular/animations/browser';
|
2019-08-22 22:16:25 -04:00
|
|
|
import {DOCUMENT, PlatformLocation, ViewportScroller, ɵNullViewportScroller as NullViewportScroller, ɵPLATFORM_SERVER_ID as PLATFORM_SERVER_ID, ɵgetDOM as getDOM} from '@angular/common';
|
2017-03-22 20:13:24 -04:00
|
|
|
import {HttpClientModule} from '@angular/common/http';
|
2019-11-09 14:00:53 -05:00
|
|
|
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';
|
2019-08-22 22:16:25 -04:00
|
|
|
import {BrowserModule, EVENT_MANAGER_PLUGINS, ɵSharedStylesHost as SharedStylesHost} from '@angular/platform-browser';
|
2017-08-16 12:00:03 -04:00
|
|
|
import {ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
|
2017-03-13 20:31:03 -04:00
|
|
|
import {NoopAnimationsModule, ɵAnimationRendererFactory} from '@angular/platform-browser/animations';
|
2017-02-22 19:49:46 -05:00
|
|
|
|
2017-08-08 05:17:40 -04:00
|
|
|
import {DominoAdapter, parseDocument} from './domino_adapter';
|
2017-02-10 20:00:27 -05:00
|
|
|
import {SERVER_HTTP_PROVIDERS} from './http';
|
2017-02-09 17:10:00 -05:00
|
|
|
import {ServerPlatformLocation} from './location';
|
2017-02-14 19:14:40 -05:00
|
|
|
import {PlatformState} from './platform_state';
|
2018-05-25 10:22:05 -04:00
|
|
|
import {ServerEventManagerPlugin} from './server_events';
|
2017-03-07 19:36:12 -05:00
|
|
|
import {ServerRendererFactory2} from './server_renderer';
|
2017-02-14 14:34:05 -05:00
|
|
|
import {ServerStylesHost} from './styles_host';
|
2017-02-14 22:48:48 -05:00
|
|
|
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
|
2017-02-09 17:10:00 -05:00
|
|
|
|
2016-06-14 22:49:25 -04:00
|
|
|
function notSupported(feature: string): Error {
|
|
|
|
throw new Error(`platform-server does not support '${feature}'.`);
|
|
|
|
}
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: StaticProvider[] = [
|
2017-02-14 19:14:40 -05:00
|
|
|
{provide: DOCUMENT, useFactory: _document, deps: [Injector]},
|
2017-02-22 19:49:46 -05:00
|
|
|
{provide: PLATFORM_ID, useValue: PLATFORM_SERVER_ID},
|
2017-09-09 18:12:13 -04:00
|
|
|
{provide: PLATFORM_INITIALIZER, useFactory: initDominoAdapter, multi: true, deps: [Injector]}, {
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
provide: PlatformLocation,
|
|
|
|
useClass: ServerPlatformLocation,
|
|
|
|
deps: [DOCUMENT, [Optional, INITIAL_CONFIG]]
|
|
|
|
},
|
|
|
|
{provide: PlatformState, deps: [DOCUMENT]},
|
2017-02-12 12:16:23 -05:00
|
|
|
// 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
|
|
|
];
|
|
|
|
|
2017-09-09 18:12:13 -04:00
|
|
|
function initDominoAdapter(injector: Injector) {
|
2017-08-08 05:17:40 -04:00
|
|
|
return () => { DominoAdapter.makeCurrent(); };
|
2016-06-14 22:49:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 20:31:03 -04:00
|
|
|
export function instantiateServerRendererFactory(
|
|
|
|
renderer: RendererFactory2, engine: ɵAnimationEngine, zone: NgZone) {
|
|
|
|
return new ɵAnimationRendererFactory(renderer, engine, zone);
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export const SERVER_RENDER_PROVIDERS: Provider[] = [
|
2017-03-07 19:36:12 -05:00
|
|
|
ServerRendererFactory2,
|
2017-03-13 20:31:03 -04:00
|
|
|
{
|
|
|
|
provide: RendererFactory2,
|
|
|
|
useFactory: instantiateServerRendererFactory,
|
|
|
|
deps: [ServerRendererFactory2, ɵAnimationEngine, NgZone]
|
|
|
|
},
|
2017-02-14 14:34:05 -05:00
|
|
|
ServerStylesHost,
|
|
|
|
{provide: SharedStylesHost, useExisting: ServerStylesHost},
|
2018-05-25 10:22:05 -04:00
|
|
|
{provide: EVENT_MANAGER_PLUGINS, multi: true, useClass: ServerEventManagerPlugin},
|
2016-11-02 19:59:24 -04:00
|
|
|
];
|
|
|
|
|
2016-06-14 22:49:25 -04:00
|
|
|
/**
|
2016-08-15 16:44:01 -04:00
|
|
|
* The ng module for the server.
|
|
|
|
*
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2016-06-14 22:49:25 -04:00
|
|
|
*/
|
2017-02-14 19:14:40 -05:00
|
|
|
@NgModule({
|
|
|
|
exports: [BrowserModule],
|
2019-03-19 20:41:12 -04:00
|
|
|
imports: [HttpClientModule, NoopAnimationsModule],
|
2017-03-01 14:16:56 -05:00
|
|
|
providers: [
|
|
|
|
SERVER_RENDER_PROVIDERS,
|
|
|
|
SERVER_HTTP_PROVIDERS,
|
|
|
|
{provide: Testability, useValue: null},
|
2018-05-17 07:33:50 -04:00
|
|
|
{provide: ViewportScroller, useClass: NullViewportScroller},
|
2017-03-01 14:16:56 -05:00
|
|
|
],
|
2017-02-14 19:14:40 -05:00
|
|
|
})
|
2016-08-15 16:44:01 -04:00
|
|
|
export class ServerModule {
|
|
|
|
}
|
2016-07-26 08:21:19 -04:00
|
|
|
|
2017-02-14 19:14:40 -05:00
|
|
|
function _document(injector: Injector) {
|
|
|
|
let config: PlatformConfig|null = injector.get(INITIAL_CONFIG, null);
|
2019-11-09 14:00:53 -05:00
|
|
|
const document = config && config.document ? parseDocument(config.document, config.url) :
|
|
|
|
getDOM().createHtmlDocument();
|
|
|
|
// Tell ivy about the global document
|
|
|
|
ɵsetDocument(document);
|
|
|
|
return document;
|
2017-02-14 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
2016-07-26 08:21:19 -04:00
|
|
|
/**
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2016-07-26 08:21:19 -04:00
|
|
|
*/
|
2019-08-22 22:24:00 -04:00
|
|
|
export const platformServer: (extraProviders?: StaticProvider[] | undefined) => PlatformRef =
|
2016-08-15 16:44:01 -04:00
|
|
|
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.
|
|
|
|
*
|
2018-10-19 07:12:20 -04:00
|
|
|
* @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
|
|
|
*/
|
2016-07-26 08:21:19 -04:00
|
|
|
export const platformDynamicServer =
|
2016-08-15 22:37:42 -04:00
|
|
|
createPlatformFactory(platformCoreDynamic, 'serverDynamic', INTERNAL_SERVER_PLATFORM_PROVIDERS);
|