2017-02-14 19:14:40 -05: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
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
import {ApplicationRef, NgModuleFactory, NgModuleRef, PlatformRef, StaticProvider, Type} from '@angular/core';
|
2017-02-22 19:06:21 -05:00
|
|
|
import {ɵTRANSITION_ID} from '@angular/platform-browser';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {first} from 'rxjs/operators';
|
2017-02-14 19:14:40 -05:00
|
|
|
|
|
|
|
import {PlatformState} from './platform_state';
|
2017-02-14 22:48:48 -05:00
|
|
|
import {platformDynamicServer, platformServer} from './server';
|
2017-09-04 03:38:42 -04:00
|
|
|
import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG} from './tokens';
|
2017-02-14 19:14:40 -05:00
|
|
|
|
2017-07-20 17:30:35 -04:00
|
|
|
interface PlatformOptions {
|
2017-02-14 19:14:40 -05:00
|
|
|
document?: string;
|
|
|
|
url?: string;
|
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
|
|
|
extraProviders?: StaticProvider[];
|
2017-02-14 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function _getPlatform(
|
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
|
|
|
platformFactory: (extraProviders: StaticProvider[]) => PlatformRef,
|
2017-02-14 19:14:40 -05:00
|
|
|
options: PlatformOptions): PlatformRef {
|
|
|
|
const extraProviders = options.extraProviders ? options.extraProviders : [];
|
|
|
|
return platformFactory([
|
|
|
|
{provide: INITIAL_CONFIG, useValue: {document: options.document, url: options.url}},
|
|
|
|
extraProviders
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _render<T>(
|
|
|
|
platform: PlatformRef, moduleRefPromise: Promise<NgModuleRef<T>>): Promise<string> {
|
|
|
|
return moduleRefPromise.then((moduleRef) => {
|
2017-02-22 19:06:21 -05:00
|
|
|
const transitionId = moduleRef.injector.get(ɵTRANSITION_ID, null);
|
|
|
|
if (!transitionId) {
|
|
|
|
throw new Error(
|
|
|
|
`renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
|
|
|
|
the server-rendered app can be properly bootstrapped into a client app.`);
|
|
|
|
}
|
2017-02-14 19:14:40 -05:00
|
|
|
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
2018-02-27 17:06:06 -05:00
|
|
|
return applicationRef.isStable.pipe((first((isStable: boolean) => isStable)))
|
|
|
|
.toPromise()
|
2017-02-14 19:14:40 -05:00
|
|
|
.then(() => {
|
2017-09-04 03:38:42 -04:00
|
|
|
const platformState = platform.injector.get(PlatformState);
|
|
|
|
|
|
|
|
// Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
|
|
|
|
const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, null);
|
|
|
|
if (callbacks) {
|
|
|
|
for (const callback of callbacks) {
|
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore exceptions.
|
|
|
|
console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = platformState.renderToString();
|
2017-02-12 12:16:23 -05:00
|
|
|
platform.destroy();
|
2017-02-14 19:14:40 -05:00
|
|
|
return output;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a Module to string.
|
|
|
|
*
|
2017-07-20 17:30:35 -04:00
|
|
|
* `document` is the full document HTML of the page to render, as a string.
|
|
|
|
* `url` is the URL for the current render request.
|
|
|
|
* `extraProviders` are the platform level providers for the current render request.
|
|
|
|
*
|
2017-02-14 19:14:40 -05:00
|
|
|
* Do not use this in a production server environment. Use pre-compiled {@link NgModuleFactory} with
|
2017-07-20 17:30:35 -04:00
|
|
|
* {@link renderModuleFactory} instead.
|
2017-02-14 19:14:40 -05:00
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-07-20 17:30:35 -04:00
|
|
|
export function renderModule<T>(
|
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
|
|
|
module: Type<T>, options: {document?: string, url?: string, extraProviders?: StaticProvider[]}):
|
|
|
|
Promise<string> {
|
2017-02-14 19:14:40 -05:00
|
|
|
const platform = _getPlatform(platformDynamicServer, options);
|
|
|
|
return _render(platform, platform.bootstrapModule(module));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a {@link NgModuleFactory} to string.
|
|
|
|
*
|
2017-07-20 17:30:35 -04:00
|
|
|
* `document` is the full document HTML of the page to render, as a string.
|
|
|
|
* `url` is the URL for the current render request.
|
|
|
|
* `extraProviders` are the platform level providers for the current render request.
|
|
|
|
*
|
2017-02-14 19:14:40 -05:00
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export function renderModuleFactory<T>(
|
2017-07-20 17:30:35 -04:00
|
|
|
moduleFactory: NgModuleFactory<T>,
|
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
|
|
|
options: {document?: string, url?: string, extraProviders?: StaticProvider[]}):
|
|
|
|
Promise<string> {
|
2017-02-14 19:14:40 -05:00
|
|
|
const platform = _getPlatform(platformServer, options);
|
|
|
|
return _render(platform, platform.bootstrapModuleFactory(moduleFactory));
|
|
|
|
}
|