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
* /
2018-02-27 17:06:06 -05:00
import { Observable , Observer , Subscription , merge } from 'rxjs' ;
import { share } from 'rxjs/operators' ;
2019-09-17 14:03:44 -04:00
2016-08-02 10:54:14 -04:00
import { ApplicationInitStatus } from './application_init' ;
2016-08-02 10:38:14 -04:00
import { APP_BOOTSTRAP_LISTENER , PLATFORM_INITIALIZER } from './application_tokens' ;
2019-03-03 12:19:27 -05:00
import { getCompilerFacade } from './compiler/compiler_facade' ;
2016-06-08 19:38:52 -04:00
import { Console } from './console' ;
2018-04-26 17:08:13 -04:00
import { Injectable , InjectionToken , Injector , StaticProvider } from './di' ;
2019-08-22 22:24:00 -04:00
import { INJECTOR_SCOPE } from './di/scope' ;
2018-07-31 01:34:15 -04:00
import { ErrorHandler } from './error_handler' ;
2019-07-15 09:28:07 -04:00
import { DEFAULT_LOCALE_ID } from './i18n/localization' ;
2019-05-17 10:13:31 -04:00
import { LOCALE_ID } from './i18n/tokens' ;
2019-01-09 16:49:16 -05:00
import { Type } from './interface/type' ;
2019-07-15 09:28:07 -04:00
import { ivyEnabled } from './ivy_switch' ;
2019-03-03 12:19:27 -05:00
import { COMPILER_OPTIONS , CompilerFactory , CompilerOptions } from './linker/compiler' ;
2016-06-08 19:38:52 -04:00
import { ComponentFactory , ComponentRef } from './linker/component_factory' ;
2017-03-14 19:26:17 -04:00
import { ComponentFactoryBoundToModule , ComponentFactoryResolver } from './linker/component_factory_resolver' ;
2017-05-11 13:26:02 -04:00
import { InternalNgModuleRef , NgModuleFactory , NgModuleRef } from './linker/ng_module_factory' ;
2017-02-17 11:56:49 -05:00
import { InternalViewRef , ViewRef } from './linker/view_ref' ;
2019-03-03 12:19:27 -05:00
import { isComponentResourceResolutionQueueEmpty , resolveComponentResources } from './metadata/resource_loading' ;
2016-06-08 19:38:52 -04:00
import { WtfScopeFn , wtfCreateScope , wtfLeave } from './profile/profile' ;
2018-07-31 01:34:15 -04:00
import { assertNgModuleType } from './render3/assert' ;
2019-01-16 09:42:13 -05:00
import { ComponentFactory as R3ComponentFactory } from './render3/component_ref' ;
2019-07-15 09:28:07 -04:00
import { setLocaleId } from './render3/i18n' ;
2018-07-31 01:34:15 -04:00
import { NgModuleFactory as R3NgModuleFactory } from './render3/ng_module_ref' ;
2019-09-17 14:03:44 -04:00
import { publishDefaultGlobalUtils as _publishDefaultGlobalUtils } from './render3/util/global_utils' ;
2016-06-08 19:38:52 -04:00
import { Testability , TestabilityRegistry } from './testability/testability' ;
2019-01-09 16:49:16 -05:00
import { isDevMode } from './util/is_dev_mode' ;
2018-07-31 01:34:15 -04:00
import { isPromise } from './util/lang' ;
2019-01-09 16:49:16 -05:00
import { scheduleMicroTask } from './util/microtask' ;
import { stringify } from './util/stringify' ;
2017-06-01 17:45:49 -04:00
import { NgZone , NoopNgZone } from './zone/ng_zone' ;
2016-06-08 19:38:52 -04:00
2016-09-20 17:14:57 -04:00
let _platform : PlatformRef ;
2015-09-02 18:19:26 -04:00
2018-07-31 01:34:15 -04:00
let compileNgModuleFactory :
< M > ( injector : Injector , options : CompilerOptions , module Type : Type < M > ) = >
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
Promise < NgModuleFactory < M > > = compileNgModuleFactory__PRE_R3__ ;
2017-02-12 12:16:23 -05:00
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
function compileNgModuleFactory__PRE_R3__ < M > (
2018-07-31 01:34:15 -04:00
injector : Injector , options : CompilerOptions ,
module Type : Type < M > ) : Promise < NgModuleFactory < M > > {
const compilerFactory : CompilerFactory = injector . get ( CompilerFactory ) ;
const compiler = compilerFactory . createCompiler ( [ options ] ) ;
return compiler . compileModuleAsync ( module Type ) ;
2016-06-17 17:09:19 -04:00
}
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
export function compileNgModuleFactory__POST_R3__ < M > (
2018-07-31 01:34:15 -04:00
injector : Injector , options : CompilerOptions ,
module Type : Type < M > ) : Promise < NgModuleFactory < M > > {
ngDevMode && assertNgModuleType ( module Type ) ;
2019-03-03 12:19:27 -05:00
const module Factory = new R3NgModuleFactory ( module Type ) ;
if ( isComponentResourceResolutionQueueEmpty ( ) ) {
return Promise . resolve ( module Factory ) ;
}
const compilerOptions = injector . get ( COMPILER_OPTIONS , [ ] ) . concat ( options ) ;
const compilerProviders = _mergeArrays ( compilerOptions . map ( o = > o . providers ! ) ) ;
// In case there are no compiler providers, we just return the module factory as
// there won't be any resource loader. This can happen with Ivy, because AOT compiled
// modules can be still passed through "bootstrapModule". In that case we shouldn't
// unnecessarily require the JIT compiler.
if ( compilerProviders . length === 0 ) {
return Promise . resolve ( module Factory ) ;
}
const compiler = getCompilerFacade ( ) ;
const compilerInjector = Injector . create ( { providers : compilerProviders } ) ;
const resourceLoader = compilerInjector . get ( compiler . ResourceLoader ) ;
// The resource loader can also return a string while the "resolveComponentResources"
// always expects a promise. Therefore we need to wrap the returned value in a promise.
return resolveComponentResources ( url = > Promise . resolve ( resourceLoader . get ( url ) ) )
. then ( ( ) = > module Factory ) ;
2016-06-17 17:09:19 -04:00
}
2019-09-17 14:03:44 -04:00
// the `window.ng` global utilities are only available in non-VE versions of
// Angular. The function switch below will make sure that the code is not
// included into Angular when PRE mode is active.
export function publishDefaultGlobalUtils__PRE_R3__() { }
export function publishDefaultGlobalUtils__POST_R3__() {
ngDevMode && _publishDefaultGlobalUtils ( ) ;
}
let publishDefaultGlobalUtils : ( ) = > any = publishDefaultGlobalUtils__PRE_R3__ ;
2019-01-16 09:42:13 -05:00
let isBoundToModule : < C > ( cf : ComponentFactory < C > ) = > boolean = isBoundToModule__PRE_R3__ ;
export function isBoundToModule__PRE_R3__ < C > ( cf : ComponentFactory < C > ) : boolean {
return cf instanceof ComponentFactoryBoundToModule ;
}
export function isBoundToModule__POST_R3__ < C > ( cf : ComponentFactory < C > ) : boolean {
return ( cf as R3ComponentFactory < C > ) . isBoundToModule ;
}
2018-07-31 01:34:15 -04:00
export const ALLOW_MULTIPLE_PLATFORMS = new InjectionToken < boolean > ( 'AllowMultipleToken' ) ;
2016-11-09 17:58:40 -05:00
/ * *
* A token for third - party components that can register themselves with NgProbe .
*
2018-10-19 07:12:20 -04:00
* @publicApi
2016-11-09 17:58:40 -05:00
* /
export class NgProbeToken {
constructor ( public name : string , public token : any ) { }
}
2015-11-13 14:21:16 -05:00
/ * *
2016-04-14 17:52:35 -04:00
* Creates a platform .
* Platforms have to be eagerly created via this function .
2016-06-27 15:27:23 -04:00
*
2018-10-19 07:12:20 -04:00
* @publicApi
2015-11-13 14:21:16 -05:00
* /
2016-04-14 17:52:35 -04:00
export function createPlatform ( injector : Injector ) : PlatformRef {
2017-02-12 12:16:23 -05:00
if ( _platform && ! _platform . destroyed &&
! _platform . injector . get ( ALLOW_MULTIPLE_PLATFORMS , false ) ) {
2016-08-25 03:50:16 -04:00
throw new Error (
2016-06-08 19:38:52 -04:00
'There can be only one platform. Destroy the previous one to create a new one.' ) ;
2016-04-14 17:52:35 -04:00
}
2019-09-17 14:03:44 -04:00
publishDefaultGlobalUtils ( ) ;
2016-07-29 09:47:40 -04:00
_platform = injector . get ( PlatformRef ) ;
2017-01-03 19:54:46 -05:00
const inits = injector . get ( PLATFORM_INITIALIZER , null ) ;
2017-04-14 17:40:56 -04:00
if ( inits ) inits . forEach ( ( init : any ) = > init ( ) ) ;
2016-04-14 17:52:35 -04:00
return _platform ;
}
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-08-08 20:18:50 -04:00
* Creates a factory for a platform
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
*
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-18 06:50:31 -04:00
export function createPlatformFactory (
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
parentPlatformFactory : ( ( extraProviders? : StaticProvider [ ] ) = > PlatformRef ) | null ,
name : string , providers : StaticProvider [ ] = [ ] ) : ( extraProviders? : StaticProvider [ ] ) = >
PlatformRef {
2017-12-06 04:13:50 -05:00
const desc = ` Platform: ${ name } ` ;
const marker = new InjectionToken ( desc ) ;
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
return ( extraProviders : StaticProvider [ ] = [ ] ) = > {
2017-02-12 12:16:23 -05:00
let platform = getPlatform ( ) ;
if ( ! platform || platform . injector . get ( ALLOW_MULTIPLE_PLATFORMS , false ) ) {
2016-12-16 18:21:58 -05:00
if ( parentPlatformFactory ) {
parentPlatformFactory (
2016-07-18 06:50:31 -04:00
providers . concat ( extraProviders ) . concat ( { provide : marker , useValue : true } ) ) ;
} else {
2017-12-06 04:13:50 -05:00
const injectedProviders : StaticProvider [ ] =
2019-08-22 22:24:00 -04:00
providers . concat ( extraProviders ) . concat ( { provide : marker , useValue : true } , {
provide : INJECTOR_SCOPE ,
useValue : 'platform'
} ) ;
2017-12-06 04:13:50 -05:00
createPlatform ( Injector . create ( { providers : injectedProviders , name : desc } ) ) ;
2016-07-18 06:50:31 -04:00
}
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
}
return assertPlatform ( marker ) ;
} ;
}
2016-04-14 17:52:35 -04:00
/ * *
2017-02-12 12:16:23 -05:00
* Checks that there currently is a platform which contains the given token as a provider .
2016-06-27 15:27:23 -04:00
*
2018-10-19 07:12:20 -04:00
* @publicApi
2016-04-14 17:52:35 -04:00
* /
export function assertPlatform ( requiredToken : any ) : PlatformRef {
2016-09-20 17:14:57 -04:00
const platform = getPlatform ( ) ;
if ( ! platform ) {
2016-08-25 03:50:16 -04:00
throw new Error ( 'No platform exists!' ) ;
2016-04-14 17:52:35 -04:00
}
2016-09-20 17:14:57 -04:00
if ( ! platform . injector . get ( requiredToken , null ) ) {
2016-08-25 03:50:16 -04:00
throw new Error (
2016-04-14 17:52:35 -04:00
'A platform with a different configuration has been created. Please destroy it first.' ) ;
}
2016-09-20 17:14:57 -04:00
2016-04-14 17:52:35 -04:00
return platform ;
2015-11-13 14:21:16 -05:00
}
2015-11-12 16:40:29 -05:00
2016-08-02 05:32:27 -04:00
/ * *
* Destroy the existing platform .
*
2018-10-19 07:12:20 -04:00
* @publicApi
2016-08-02 05:32:27 -04:00
* /
export function destroyPlatform ( ) : void {
2016-09-20 17:14:57 -04:00
if ( _platform && ! _platform . destroyed ) {
2016-08-02 05:32:27 -04:00
_platform . destroy ( ) ;
2015-11-18 12:18:37 -05:00
}
}
2016-04-14 17:52:35 -04:00
/ * *
* Returns the current platform .
2016-06-27 15:27:23 -04:00
*
2018-10-19 07:12:20 -04:00
* @publicApi
2016-04-14 17:52:35 -04:00
* /
2017-03-29 12:34:45 -04:00
export function getPlatform ( ) : PlatformRef | null {
2016-09-20 17:14:57 -04:00
return _platform && ! _platform . destroyed ? _platform : null ;
2015-09-02 18:19:26 -04:00
}
2017-06-01 17:45:49 -04:00
/ * *
* Provides additional options to the bootstraping process .
*
2018-04-05 17:31:44 -04:00
*
2017-06-01 17:45:49 -04:00
* /
export interface BootstrapOptions {
/ * *
* Optionally specify which ` NgZone ` should be used .
*
* - Provide your own ` NgZone ` instance .
* - ` zone.js ` - Use default ` NgZone ` which requires ` Zone.js ` .
* - ` noop ` - Use ` NoopNgZone ` which does nothing .
* /
ngZone? : NgZone | 'zone.js' | 'noop' ;
}
2015-09-02 18:19:26 -04:00
/ * *
2015-09-15 18:51:15 -04:00
* The Angular platform is the entry point for Angular on a web page . Each page
* has exactly one platform , and services ( such as reflection ) which are common
* to every Angular application running on the page are bound in its scope .
2015-08-20 20:18:27 -04:00
*
2017-04-24 20:15:33 -04:00
* A page ' s platform is initialized implicitly when a platform is created via a platform factory
* ( e . g . { @link platformBrowser } ) , or explicitly by calling the { @link createPlatform } function .
2018-10-19 11:27:04 -04:00
*
* @publicApi
2015-08-20 20:18:27 -04:00
* /
2017-06-01 17:45:49 -04:00
@Injectable ( )
2017-09-12 14:45:02 -04:00
export class PlatformRef {
private _modules : NgModuleRef < any > [ ] = [ ] ;
private _destroyListeners : Function [ ] = [ ] ;
private _destroyed : boolean = false ;
/** @internal */
constructor ( private _injector : Injector ) { }
2015-10-26 13:50:25 -04:00
/ * *
2016-07-26 08:21:19 -04:00
* Creates an instance of an ` @NgModule ` for the given platform
* for offline compilation .
*
2018-05-18 11:13:00 -04:00
* @usageNotes
* # # # Simple Example
2016-07-26 08:21:19 -04:00
*
* ` ` ` typescript
* my_module . ts :
*
* @NgModule ( {
* imports : [ BrowserModule ]
* } )
* class MyModule { }
*
* main . ts :
* import { MyModuleNgFactory } from './my_module.ngfactory' ;
2016-08-15 16:44:01 -04:00
* import { platformBrowser } from '@angular/platform-browser' ;
2016-07-26 08:21:19 -04:00
*
2016-08-15 16:44:01 -04:00
* let module Ref = platformBrowser ( ) . bootstrapModuleFactory ( MyModuleNgFactory ) ;
2016-07-26 08:21:19 -04:00
* ` ` `
* /
2017-06-01 17:45:49 -04:00
bootstrapModuleFactory < M > ( module Factory : NgModuleFactory < M > , options? : BootstrapOptions ) :
2016-08-05 16:32:04 -04:00
Promise < NgModuleRef < M > > {
2016-07-26 08:21:19 -04:00
// 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.
2017-06-01 17:45:49 -04:00
const ngZoneOption = options ? options.ngZone : undefined ;
const ngZone = getNgZone ( ngZoneOption ) ;
2017-12-06 04:13:50 -05:00
const providers : StaticProvider [ ] = [ { provide : NgZone , useValue : ngZone } ] ;
2016-07-29 09:47:40 -04:00
// Attention: Don't use ApplicationRef.run here,
// as we want to be sure that all possible constructor calls are inside `ngZone.run`!
return ngZone . run ( ( ) = > {
2017-12-06 04:13:50 -05:00
const ngZoneInjector = Injector . create (
{ providers : providers , parent : this.injector , name : moduleFactory.moduleType.name } ) ;
2017-05-11 13:26:02 -04:00
const module Ref = < InternalNgModuleRef < M > > module Factory.create ( ngZoneInjector ) ;
2016-08-25 03:50:16 -04:00
const exceptionHandler : ErrorHandler = module Ref.injector.get ( ErrorHandler , null ) ;
2016-07-29 17:45:05 -04:00
if ( ! exceptionHandler ) {
2016-08-30 21:07:40 -04:00
throw new Error ( 'No ErrorHandler. Is platform module (BrowserModule) included?' ) ;
2016-07-29 17:45:05 -04:00
}
2019-05-17 10:13:31 -04:00
// If the `LOCALE_ID` provider is defined at bootstrap we set the value for runtime i18n (ivy)
2019-07-15 09:28:07 -04:00
if ( ivyEnabled ) {
const localeId = module Ref.injector.get ( LOCALE_ID , DEFAULT_LOCALE_ID ) ;
setLocaleId ( localeId || DEFAULT_LOCALE_ID ) ;
}
2017-03-01 17:10:59 -05:00
module Ref.onDestroy ( ( ) = > remove ( this . _modules , module Ref ) ) ;
2017-07-21 01:34:19 -04:00
ngZone ! . runOutsideAngular (
( ) = > ngZone ! . onError . subscribe (
{ next : ( error : any ) = > { exceptionHandler . handleError ( error ) ; } } ) ) ;
return _callAndReportToErrorHandler ( exceptionHandler , ngZone ! , ( ) = > {
2016-08-02 10:54:14 -04:00
const initStatus : ApplicationInitStatus = module Ref.injector.get ( ApplicationInitStatus ) ;
2017-05-16 18:14:55 -04:00
initStatus . runInitializers ( ) ;
2016-08-02 10:38:14 -04:00
return initStatus . donePromise . then ( ( ) = > {
2016-08-02 09:54:08 -04:00
this . _moduleDoBootstrap ( module Ref ) ;
2016-07-29 09:47:40 -04:00
return module Ref ;
} ) ;
} ) ;
} ) ;
2016-07-26 08:21:19 -04:00
}
2017-09-12 14:45:02 -04:00
/ * *
* Creates an instance of an ` @NgModule ` for a given platform using the given runtime compiler .
*
2018-05-18 11:13:00 -04:00
* @usageNotes
* # # # Simple Example
2017-09-12 14:45:02 -04:00
*
* ` ` ` typescript
* @NgModule ( {
* imports : [ BrowserModule ]
* } )
* class MyModule { }
*
* let module Ref = platformBrowser ( ) . bootstrapModule ( MyModule ) ;
* ` ` `
2018-04-05 17:31:44 -04:00
*
2017-09-12 14:45:02 -04:00
* /
2017-06-01 17:45:49 -04:00
bootstrapModule < M > (
module Type : Type < M > , compilerOptions : ( CompilerOptions & BootstrapOptions ) |
Array < CompilerOptions & BootstrapOptions > = [ ] ) : Promise < NgModuleRef < M > > {
const options = optionsReducer ( { } , compilerOptions ) ;
2018-07-31 01:34:15 -04:00
return compileNgModuleFactory ( this . injector , options , module Type )
. then ( module Factory = > this . bootstrapModuleFactory ( module Factory , options ) ) ;
2016-07-26 08:21:19 -04:00
}
2016-08-02 09:54:08 -04:00
2017-05-11 13:26:02 -04:00
private _moduleDoBootstrap ( module Ref : InternalNgModuleRef < any > ) : void {
const appRef = module Ref.injector.get ( ApplicationRef ) as ApplicationRef ;
if ( module Ref._bootstrapComponents.length > 0 ) {
module Ref._bootstrapComponents.forEach ( f = > appRef . bootstrap ( f ) ) ;
2016-08-02 09:54:08 -04:00
} else if ( module Ref.instance.ngDoBootstrap ) {
module Ref.instance.ngDoBootstrap ( appRef ) ;
} else {
2016-08-25 03:50:16 -04:00
throw new Error (
2016-08-02 09:54:08 -04:00
` The module ${ stringify ( module Ref.instance.constructor ) } was bootstrapped, but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. ` +
` Please define one of these. ` ) ;
}
2017-01-01 12:46:53 -05:00
this . _modules . push ( module Ref ) ;
2016-08-02 09:54:08 -04:00
}
2015-09-02 18:19:26 -04:00
2015-08-20 20:18:27 -04:00
/ * *
2017-09-12 14:45:02 -04:00
* Register a listener to be called when the platform is disposed .
2015-10-09 19:22:07 -04:00
* /
2017-09-12 14:45:02 -04:00
onDestroy ( callback : ( ) = > void ) : void { this . _destroyListeners . push ( callback ) ; }
2016-08-02 10:49:33 -04:00
2016-08-02 09:54:08 -04:00
/ * *
2017-09-12 14:45:02 -04:00
* Retrieve the platform { @link Injector } , which is the parent injector for
* every Angular application on the page and provides singleton providers .
2016-08-02 09:54:08 -04:00
* /
2017-09-12 14:45:02 -04:00
get injector ( ) : Injector { return this . _injector ; }
2016-11-04 14:58:06 -04:00
/ * *
2017-09-12 14:45:02 -04:00
* Destroy the Angular platform and all Angular applications on the page .
2016-11-04 14:58:06 -04:00
* /
2017-09-12 14:45:02 -04:00
destroy() {
if ( this . _destroyed ) {
throw new Error ( 'The platform has already been destroyed!' ) ;
}
this . _modules . slice ( ) . forEach ( module = > module .destroy ( ) ) ;
this . _destroyListeners . forEach ( listener = > listener ( ) ) ;
this . _destroyed = true ;
}
2016-11-04 14:58:06 -04:00
2017-09-12 14:45:02 -04:00
get destroyed() { return this . _destroyed ; }
}
2016-11-04 14:58:06 -04:00
2017-06-01 17:45:49 -04:00
function getNgZone ( ngZoneOption? : NgZone | 'zone.js' | 'noop' ) : NgZone {
let ngZone : NgZone ;
if ( ngZoneOption === 'noop' ) {
ngZone = new NoopNgZone ( ) ;
} else {
ngZone = ( ngZoneOption === 'zone.js' ? undefined : ngZoneOption ) ||
new NgZone ( { enableLongStackTrace : isDevMode ( ) } ) ;
}
return ngZone ;
}
2017-09-12 14:45:02 -04:00
function _callAndReportToErrorHandler (
errorHandler : ErrorHandler , ngZone : NgZone , callback : ( ) = > any ) : any {
try {
const result = callback ( ) ;
if ( isPromise ( result ) ) {
return result . catch ( ( e : any ) = > {
ngZone . runOutsideAngular ( ( ) = > errorHandler . handleError ( e ) ) ;
// rethrow as the exception handler might not do it
throw e ;
} ) ;
}
2017-02-03 08:42:22 -05:00
2017-09-12 14:45:02 -04:00
return result ;
} catch ( e ) {
ngZone . runOutsideAngular ( ( ) = > errorHandler . handleError ( e ) ) ;
// rethrow as the exception handler might not do it
throw e ;
}
2015-10-06 09:53:39 -04:00
}
2017-06-01 17:45:49 -04:00
function optionsReducer < T extends Object > ( dst : any , objs : T | T [ ] ) : T {
if ( Array . isArray ( objs ) ) {
dst = objs . reduce ( optionsReducer , dst ) ;
} else {
dst = { . . . dst , . . . ( objs as any ) } ;
}
return dst ;
}
2017-01-30 19:25:15 -05:00
/ * *
2017-09-12 14:45:02 -04:00
* A reference to an Angular application running on a page .
2018-10-19 11:27:04 -04:00
*
2019-01-13 08:38:56 -05:00
* @usageNotes
*
* { @a is - stable - examples }
* # # # isStable examples and caveats
*
* Note two important points about ` isStable ` , demonstrated in the examples below :
* - the application will never be stable if you start any kind
* of recurrent asynchronous task when the application starts
* ( for example for a polling process , started with a ` setInterval ` , a ` setTimeout `
* or using RxJS operators like ` interval ` ) ;
* - the ` isStable ` Observable runs outside of the Angular zone .
*
* Let ' s imagine that you start a recurrent task
* ( here incrementing a counter , using RxJS ` interval ` ) ,
* and at the same time subscribe to ` isStable ` .
*
* ` ` `
* constructor ( appRef : ApplicationRef ) {
* appRef . isStable . pipe (
* filter ( stable = > stable )
* ) . subscribe ( ( ) = > console . log ( 'App is stable now' ) ;
* interval ( 1000 ) . subscribe ( counter = > console . log ( counter ) ) ;
* }
* ` ` `
* In this example , ` isStable ` will never emit ` true ` ,
* and the trace "App is stable now" will never get logged .
*
* If you want to execute something when the app is stable ,
* you have to wait for the application to be stable
* before starting your polling process .
*
* ` ` `
* constructor ( appRef : ApplicationRef ) {
* appRef . isStable . pipe (
* first ( stable = > stable ) ,
* tap ( stable = > console . log ( 'App is stable now' ) ) ,
* switchMap ( ( ) = > interval ( 1000 ) )
* ) . subscribe ( counter = > console . log ( counter ) ) ;
* }
* ` ` `
* In this example , the trace "App is stable now" will be logged
* and then the counter starts incrementing every second .
*
* Note also that this Observable runs outside of the Angular zone ,
* which means that the code in the subscription
* to this Observable will not trigger the change detection .
*
* Let ' s imagine that instead of logging the counter value ,
* you update a field of your component
* and display it in its template .
*
* ` ` `
* constructor ( appRef : ApplicationRef ) {
* appRef . isStable . pipe (
* first ( stable = > stable ) ,
* switchMap ( ( ) = > interval ( 1000 ) )
* ) . subscribe ( counter = > this . value = counter ) ;
* }
* ` ` `
* As the ` isStable ` Observable runs outside the zone ,
* the ` value ` field will be updated properly ,
* but the template will not be refreshed !
*
* You ' ll have to manually trigger the change detection to update the template .
*
* ` ` `
* constructor ( appRef : ApplicationRef , cd : ChangeDetectorRef ) {
* appRef . isStable . pipe (
* first ( stable = > stable ) ,
* switchMap ( ( ) = > interval ( 1000 ) )
* ) . subscribe ( counter = > {
* this . value = counter ;
* cd . detectChanges ( ) ;
* } ) ;
* }
* ` ` `
*
* Or make the subscription callback run inside the zone .
*
* ` ` `
* constructor ( appRef : ApplicationRef , zone : NgZone ) {
* appRef . isStable . pipe (
* first ( stable = > stable ) ,
* switchMap ( ( ) = > interval ( 1000 ) )
* ) . subscribe ( counter = > zone . run ( ( ) = > this . value = counter ) ) ;
* }
* ` ` `
*
2018-10-19 11:27:04 -04:00
* @publicApi
2017-01-30 19:25:15 -05:00
* /
2018-04-26 17:08:13 -04:00
@Injectable ( )
2017-09-12 14:45:02 -04:00
export class ApplicationRef {
2015-10-28 13:34:13 -04:00
/** @internal */
static _tickScope : WtfScopeFn = wtfCreateScope ( 'ApplicationRef#tick()' ) ;
2017-01-03 19:54:46 -05:00
private _bootstrapListeners : ( ( compRef : ComponentRef < any > ) = > void ) [ ] = [ ] ;
2017-02-17 11:56:49 -05:00
private _views : InternalViewRef [ ] = [ ] ;
2015-10-28 13:34:13 -04:00
private _runningTick : boolean = false ;
private _enforceNoNewChanges : boolean = false ;
2017-02-03 08:42:22 -05:00
private _stable = true ;
2015-10-06 09:53:39 -04:00
2017-09-08 21:06:33 -04:00
/ * *
* Get a list of component types registered to this application .
* This list is populated even before the component is created .
* /
public readonly componentTypes : Type < any > [ ] = [ ] ;
/ * *
* Get a list of components registered to this application .
* /
public readonly components : ComponentRef < any > [ ] = [ ] ;
/ * *
* Returns an Observable that indicates when the application is stable or unstable .
2019-01-13 08:38:56 -05:00
*
* @see [ Usage notes ] ( # is - stable - examples ) for examples and caveats when using this API .
2017-09-08 21:06:33 -04:00
* /
2018-06-18 19:38:33 -04:00
// TODO(issue/24571): remove '!'.
public readonly isStable ! : Observable < boolean > ;
2017-09-08 21:06:33 -04:00
2017-09-12 14:45:02 -04:00
/** @internal */
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@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 {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
constructor (
2016-08-02 05:32:27 -04:00
private _zone : NgZone , private _console : Console , private _injector : Injector ,
2016-08-25 03:50:16 -04:00
private _exceptionHandler : ErrorHandler ,
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@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 {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
private _componentFactoryResolver : ComponentFactoryResolver ,
2017-03-01 14:16:56 -05:00
private _initStatus : ApplicationInitStatus ) {
2016-06-17 17:09:19 -04:00
this . _enforceNoNewChanges = isDevMode ( ) ;
2016-08-02 18:53:34 -04:00
this . _zone . onMicrotaskEmpty . subscribe (
{ next : ( ) = > { this . _zone . run ( ( ) = > { this . tick ( ) ; } ) ; } } ) ;
2017-02-03 08:42:22 -05:00
const isCurrentlyStable = new Observable < boolean > ( ( observer : Observer < boolean > ) = > {
this . _stable = this . _zone . isStable && ! this . _zone . hasPendingMacrotasks &&
! this . _zone . hasPendingMicrotasks ;
this . _zone . runOutsideAngular ( ( ) = > {
observer . next ( this . _stable ) ;
observer . complete ( ) ;
} ) ;
} ) ;
const isStable = new Observable < boolean > ( ( observer : Observer < boolean > ) = > {
2017-08-10 20:18:37 -04:00
// Create the subscription to onStable outside the Angular Zone so that
// the callback is run outside the Angular Zone.
let stableSub : Subscription ;
this . _zone . runOutsideAngular ( ( ) = > {
stableSub = this . _zone . onStable . subscribe ( ( ) = > {
NgZone . assertNotInAngularZone ( ) ;
// Check whether there are no pending macro/micro tasks in the next tick
// to allow for NgZone to update the state.
scheduleMicroTask ( ( ) = > {
if ( ! this . _stable && ! this . _zone . hasPendingMacrotasks &&
! this . _zone . hasPendingMicrotasks ) {
this . _stable = true ;
observer . next ( true ) ;
}
} ) ;
2017-02-03 08:42:22 -05:00
} ) ;
} ) ;
const unstableSub : Subscription = this . _zone . onUnstable . subscribe ( ( ) = > {
NgZone . assertInAngularZone ( ) ;
if ( this . _stable ) {
this . _stable = false ;
this . _zone . runOutsideAngular ( ( ) = > { observer . next ( false ) ; } ) ;
}
} ) ;
return ( ) = > {
stableSub . unsubscribe ( ) ;
unstableSub . unsubscribe ( ) ;
} ;
} ) ;
2017-09-08 21:06:33 -04:00
( this as { isStable : Observable < boolean > } ) . isStable =
2018-02-27 17:06:06 -05:00
merge ( isCurrentlyStable , isStable . pipe ( share ( ) ) ) ;
2015-10-06 09:53:39 -04:00
}
2017-09-12 14:45:02 -04:00
/ * *
* Bootstrap a new component at the root level of the application .
*
2018-05-18 11:13:00 -04:00
* @usageNotes
2017-09-12 14:45:02 -04:00
* # # # Bootstrap process
*
* When bootstrapping a new root component into an application , Angular mounts the
2018-05-18 11:13:00 -04:00
* specified application component onto DOM elements identified by the componentType ' s
2017-09-12 14:45:02 -04:00
* selector and kicks off automatic change detection to finish initializing the component .
*
* Optionally , a component can be mounted onto a DOM element that does not match the
2018-05-18 11:13:00 -04:00
* componentType ' s selector .
2017-09-12 14:45:02 -04:00
*
* # # # Example
* { @example core / ts / platform / platform . ts region = 'longform' }
* /
2017-03-31 10:37:20 -04:00
bootstrap < C > ( componentOrFactory : ComponentFactory < C > | Type < C > , rootSelectorOrNode? : string | any ) :
ComponentRef < C > {
2016-08-02 10:38:14 -04:00
if ( ! this . _initStatus . done ) {
2016-08-25 03:50:16 -04:00
throw new Error (
2016-08-02 10:38:14 -04:00
'Cannot bootstrap as there are still asynchronous initializers running. Bootstrap components in the `ngDoBootstrap` method of the root module.' ) ;
}
2016-08-02 10:48:15 -04:00
let componentFactory : ComponentFactory < C > ;
if ( componentOrFactory instanceof ComponentFactory ) {
componentFactory = componentOrFactory ;
} else {
2017-03-29 12:34:45 -04:00
componentFactory =
this . _componentFactoryResolver . resolveComponentFactory ( componentOrFactory ) ! ;
2016-08-02 10:48:15 -04:00
}
2017-09-08 21:06:33 -04:00
this . componentTypes . push ( componentFactory . componentType ) ;
2017-03-14 19:26:17 -04:00
// Create a factory associated with the current module if it's not bound to some other
2019-08-19 18:05:29 -04:00
const ngModule =
isBoundToModule ( componentFactory ) ? undefined : this . _injector . get ( NgModuleRef ) ;
2017-03-31 10:37:20 -04:00
const selectorOrNode = rootSelectorOrNode || componentFactory . selector ;
const compRef = componentFactory . create ( Injector . NULL , [ ] , selectorOrNode , ngModule ) ;
2017-03-14 19:26:17 -04:00
2016-08-02 10:48:15 -04:00
compRef . onDestroy ( ( ) = > { this . _unloadComponent ( compRef ) ; } ) ;
2016-09-20 17:14:57 -04:00
const testability = compRef . injector . get ( Testability , null ) ;
if ( testability ) {
2016-08-02 10:48:15 -04:00
compRef . injector . get ( TestabilityRegistry )
. registerApplication ( compRef . location . nativeElement , testability ) ;
}
2016-04-14 17:52:35 -04:00
2016-08-02 10:48:15 -04:00
this . _loadComponent ( compRef ) ;
if ( isDevMode ( ) ) {
this . _console . log (
2017-01-03 13:03:58 -05:00
` Angular is running in the development mode. Call enableProdMode() to enable the production mode. ` ) ;
2016-08-02 10:48:15 -04:00
}
return compRef ;
2015-09-02 18:19:26 -04:00
}
2017-09-12 14:45:02 -04:00
/ * *
* Invoke this method to explicitly process change detection and its side - effects .
*
* In development mode , ` tick() ` also performs a second change detection cycle to ensure that no
* further changes are detected . If additional changes are picked up during this second cycle ,
* bindings in the app have side - effects that cannot be resolved in a single change detection
* pass .
* In this case , Angular throws an error , since an Angular application can only have one change
* detection pass during which all change detection must complete .
* /
2015-10-28 13:34:13 -04:00
tick ( ) : void {
if ( this . _runningTick ) {
2016-08-25 03:50:16 -04:00
throw new Error ( 'ApplicationRef.tick is called recursively' ) ;
2015-10-28 13:34:13 -04:00
}
2017-09-12 14:45:02 -04:00
const scope = ApplicationRef . _tickScope ( ) ;
2015-10-28 13:34:13 -04:00
try {
this . _runningTick = true ;
2019-03-27 09:19:15 -04:00
for ( let view of this . _views ) {
view . detectChanges ( ) ;
}
2015-10-28 13:34:13 -04:00
if ( this . _enforceNoNewChanges ) {
2019-03-27 09:19:15 -04:00
for ( let view of this . _views ) {
view . checkNoChanges ( ) ;
}
2015-10-28 13:34:13 -04:00
}
2017-04-28 14:50:45 -04:00
} catch ( e ) {
// Attention: Don't rethrow as it could cancel subscriptions to Observables!
2017-07-21 01:34:19 -04:00
this . _zone . runOutsideAngular ( ( ) = > this . _exceptionHandler . handleError ( e ) ) ;
2015-10-28 13:34:13 -04:00
} finally {
this . _runningTick = false ;
2016-09-20 17:14:57 -04:00
wtfLeave ( scope ) ;
2015-10-28 13:34:13 -04:00
}
}
2017-09-12 14:45:02 -04:00
/ * *
* Attaches a view so that it will be dirty checked .
* The view will be automatically detached when it is destroyed .
* This will throw if the view is already attached to a ViewContainer .
* /
attachView ( viewRef : ViewRef ) : void {
const view = ( viewRef as InternalViewRef ) ;
this . _views . push ( view ) ;
view . attachToAppRef ( this ) ;
}
/ * *
* Detaches a view from dirty checking again .
* /
detachView ( viewRef : ViewRef ) : void {
const view = ( viewRef as InternalViewRef ) ;
remove ( this . _views , view ) ;
view . detachFromAppRef ( ) ;
}
private _loadComponent ( componentRef : ComponentRef < any > ) : void {
this . attachView ( componentRef . hostView ) ;
this . tick ( ) ;
2017-09-08 21:06:33 -04:00
this . components . push ( componentRef ) ;
2017-09-12 14:45:02 -04:00
// Get the listeners lazily to prevent DI cycles.
const listeners =
this . _injector . get ( APP_BOOTSTRAP_LISTENER , [ ] ) . concat ( this . _bootstrapListeners ) ;
listeners . forEach ( ( listener ) = > listener ( componentRef ) ) ;
}
private _unloadComponent ( componentRef : ComponentRef < any > ) : void {
this . detachView ( componentRef . hostView ) ;
2017-09-08 21:06:33 -04:00
remove ( this . components , componentRef ) ;
2017-09-12 14:45:02 -04:00
}
/** @internal */
2016-08-02 05:32:27 -04:00
ngOnDestroy() {
2015-09-02 18:19:26 -04:00
// TODO(alxhub): Dispose of the NgZone.
2016-11-04 14:58:06 -04:00
this . _views . slice ( ) . forEach ( ( view ) = > view . destroy ( ) ) ;
2015-09-02 18:19:26 -04:00
}
2015-10-09 19:22:07 -04:00
2017-09-12 14:45:02 -04:00
/ * *
* Returns the number of attached views .
* /
2016-11-04 14:58:06 -04:00
get viewCount() { return this . _views . length ; }
2015-08-20 20:18:27 -04:00
}
2017-03-01 17:10:59 -05:00
function remove < T > ( list : T [ ] , el : T ) : void {
const index = list . indexOf ( el ) ;
if ( index > - 1 ) {
list . splice ( index , 1 ) ;
}
}
2019-03-03 12:19:27 -05:00
function _mergeArrays ( parts : any [ ] [ ] ) : any [ ] {
const result : any [ ] = [ ] ;
parts . forEach ( ( part ) = > part && result . push ( . . . part ) ) ;
return result ;
}