Skip to content

Commit 0eab4fc

Browse files
committed
feat(core): extract platforms out of core
Currently, core depends on the browser, which means that other platforms (e.g., NativeScript or webworker) cannot use the bootstrapping logic core provides. This PR extract makes bootstrapping logic in core completely platform-independent. The browser-specific code was moved to "angular2/platforms/browser". BREAKING CHANGE A few private helpers (e.g., platformCommon or applicationCommon) were removed or replaced with other helpers. Look at PLATFORM_COMMON_PROVIDERS, APPLICATION_COMMON_PROVIDERS, BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS to see if they export the providers you need. Closes #5219 Closes #5280
1 parent d9f362a commit 0eab4fc

37 files changed

+450
-469
lines changed

modules/angular2/angular2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export * from './common';
22
export * from './core';
33
export * from './profile';
44
export * from './lifecycle_hooks';
5-
export * from './bootstrap';
5+
export * from './platform/browser';
66
export * from './upgrade';

modules/angular2/bootstrap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This file is only used for dart applications and for internal examples
3-
* that compile with both JavaScript and Dart.
2+
* See {@link bootstrap} for more information.
3+
* @deprecated
44
*/
5-
export {bootstrap} from 'angular2/src/core/bootstrap';
5+
export {bootstrap} from 'angular2/platform/browser';

modules/angular2/bootstrap_static.dart

Lines changed: 0 additions & 3 deletions
This file was deleted.

modules/angular2/bootstrap_static.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* See {@link bootstrap} for more information.
3+
* @deprecated
4+
*/
5+
export {bootstrapStatic} from 'angular2/platform/browser_static';

modules/angular2/compiler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
* Starting point to import all compiler APIs.
55
*/
66
export * from './src/compiler/url_resolver';
7-
export * from './src/compiler/xhr';
7+
export * from './src/compiler/xhr';
8+
export * from './src/compiler/compiler';

modules/angular2/core.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export 'package:angular2/src/core/dev_mode.dart';
77
export 'package:angular2/src/core/di.dart';
88
export 'package:angular2/src/common/pipes.dart';
99
export 'package:angular2/src/facade/facade.dart';
10-
// Do not export application for dart. Must import from angular2/bootstrap
11-
//export 'package:angular2/src/core/application.dart';
1210
export 'package:angular2/src/core/application_ref.dart'
1311
hide ApplicationRef_, PlatformRef_;
1412
export 'package:angular2/src/core/services.dart';
@@ -20,3 +18,6 @@ export 'package:angular2/src/common/forms.dart';
2018
export 'package:angular2/src/core/debug.dart';
2119
export 'package:angular2/src/core/change_detection.dart';
2220
export 'package:angular2/src/core/platform_directives_and_pipes.dart';
21+
export 'package:angular2/src/core/platform_common_providers.dart';
22+
export 'package:angular2/src/core/application_common_providers.dart';
23+
export 'package:angular2/src/core/reflection/reflection.dart';

modules/angular2/core.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ export * from './src/core/util';
88
export * from './src/core/di';
99
export * from './src/common/pipes';
1010
export * from './src/facade/facade';
11-
export * from './src/core/application';
12-
export * from './src/core/bootstrap';
1311
export * from './src/core/services';
1412
export * from './src/core/linker';
15-
export {ApplicationRef} from './src/core/application_ref';
13+
export {platform, createNgZone, PlatformRef, ApplicationRef} from './src/core/application_ref';
14+
export {APP_ID, APP_COMPONENT} from './src/core/application_tokens';
1615
export * from './src/core/zone';
1716
export * from './src/core/render';
1817
export * from './src/common/directives';
@@ -21,3 +20,6 @@ export * from './src/core/debug';
2120
export * from './src/core/change_detection';
2221
export * from './src/core/platform_directives_and_pipes';
2322
export * from './src/core/dev_mode';
23+
export * from './src/core/reflection/reflection';
24+
export * from './src/core/application_common_providers';
25+
export * from './src/core/platform_common_providers';

modules/angular2/examples/core/ts/dev_mode/dev_mode_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// #docregion enableDevMode
2-
import {bootstrap, enableDevMode} from 'angular2/core';
2+
import {bootstrap, enableDevMode} from 'angular2/angular2';
33
import {MyComponent} from 'my_component';
44

55
enableDevMode();

modules/angular2/platform/browser.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
export {BROWSER_PROVIDERS} from 'angular2/src/platform/browser_common';
2+
3+
import {Type, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
4+
import {Promise} from 'angular2/src/facade/promise';
5+
import {
6+
BROWSER_PROVIDERS,
7+
BROWSER_APP_COMMON_PROVIDERS,
8+
initDomAdapter
9+
} from 'angular2/src/platform/browser_common';
10+
import {COMPILER_PROVIDERS} from 'angular2/compiler';
11+
import {ComponentRef, platform, reflector} from 'angular2/core';
12+
import {ReflectionCapabilities} from 'angular2/src/core/reflection/reflection_capabilities';
13+
14+
/**
15+
* An array of providers that should be passed into `application()` when bootstrapping a component.
16+
*/
17+
export const BROWSER_APP_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
18+
CONST_EXPR([BROWSER_APP_COMMON_PROVIDERS, COMPILER_PROVIDERS]);
19+
20+
/**
21+
* Bootstrapping for Angular applications.
22+
*
23+
* You instantiate an Angular application by explicitly specifying a component to use
24+
* as the root component for your application via the `bootstrap()` method.
25+
*
26+
* ## Simple Example
27+
*
28+
* Assuming this `index.html`:
29+
*
30+
* ```html
31+
* <html>
32+
* <!-- load Angular script tags here. -->
33+
* <body>
34+
* <my-app>loading...</my-app>
35+
* </body>
36+
* </html>
37+
* ```
38+
*
39+
* An application is bootstrapped inside an existing browser DOM, typically `index.html`.
40+
* Unlike Angular 1, Angular 2 does not compile/process providers in `index.html`. This is
41+
* mainly for security reasons, as well as architectural changes in Angular 2. This means
42+
* that `index.html` can safely be processed using server-side technologies such as
43+
* providers. Bindings can thus use double-curly `{{ syntax }}` without collision from
44+
* Angular 2 component double-curly `{{ syntax }}`.
45+
*
46+
* We can use this script code:
47+
*
48+
* ```
49+
* @Component({
50+
* selector: 'my-app',
51+
* template: 'Hello {{ name }}!'
52+
* })
53+
* class MyApp {
54+
* name:string;
55+
*
56+
* constructor() {
57+
* this.name = 'World';
58+
* }
59+
* }
60+
*
61+
* main() {
62+
* return bootstrap(MyApp);
63+
* }
64+
* ```
65+
*
66+
* When the app developer invokes `bootstrap()` with the root component `MyApp` as its
67+
* argument, Angular performs the following tasks:
68+
*
69+
* 1. It uses the component's `selector` property to locate the DOM element which needs
70+
* to be upgraded into the angular component.
71+
* 2. It creates a new child injector (from the platform injector). Optionally, you can
72+
* also override the injector configuration for an app by invoking `bootstrap` with the
73+
* `componentInjectableBindings` argument.
74+
* 3. It creates a new `Zone` and connects it to the angular application's change detection
75+
* domain instance.
76+
* 4. It creates an emulated or shadow DOM on the selected component's host element and loads the
77+
* template into it.
78+
* 5. It instantiates the specified component.
79+
* 6. Finally, Angular performs change detection to apply the initial data providers for the
80+
* application.
81+
*
82+
*
83+
* ## Bootstrapping Multiple Applications
84+
*
85+
* When working within a browser window, there are many singleton resources: cookies, title,
86+
* location, and others. Angular services that represent these resources must likewise be
87+
* shared across all Angular applications that occupy the same browser window. For this
88+
* reason, Angular creates exactly one global platform object which stores all shared
89+
* services, and each angular application injector has the platform injector as its parent.
90+
*
91+
* Each application has its own private injector as well. When there are multiple
92+
* applications on a page, Angular treats each application injector's services as private
93+
* to that application.
94+
*
95+
* ## API
96+
*
97+
* - `appComponentType`: The root component which should act as the application. This is
98+
* a reference to a `Type` which is annotated with `@Component(...)`.
99+
* - `customProviders`: An additional set of providers that can be added to the
100+
* app injector to override default injection behavior.
101+
*
102+
* Returns a `Promise` of {@link ComponentRef}.
103+
*/
104+
export function bootstrap(
105+
appComponentType: Type,
106+
customProviders?: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef> {
107+
reflector.reflectionCapabilities = new ReflectionCapabilities();
108+
initDomAdapter();
109+
110+
let appProviders =
111+
isPresent(customProviders) ? [BROWSER_APP_PROVIDERS, customProviders] : BROWSER_APP_PROVIDERS;
112+
return platform(BROWSER_PROVIDERS).application(appProviders).bootstrap(appComponentType);
113+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export {BROWSER_PROVIDERS} from 'angular2/src/platform/browser_common';
2+
3+
import {Type, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
4+
import {Promise} from 'angular2/src/facade/promise';
5+
import {
6+
BROWSER_PROVIDERS,
7+
BROWSER_APP_COMMON_PROVIDERS,
8+
initDomAdapter
9+
} from 'angular2/src/platform/browser_common';
10+
import {ComponentRef, platform, reflector} from 'angular2/core';
11+
12+
/**
13+
* An array of providers that should be passed into `application()` when bootstrapping a component
14+
* when all templates
15+
* have been precompiled offline.
16+
*/
17+
export const BROWSER_APP_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
18+
BROWSER_APP_COMMON_PROVIDERS;
19+
20+
/**
21+
* See {@link bootstrap} for more information.
22+
*/
23+
export function bootstrapStatic(appComponentType: Type,
24+
customProviders?: Array<any /*Type | Provider | any[]*/>,
25+
initReflector?: Function): Promise<ComponentRef> {
26+
initDomAdapter();
27+
if (isPresent(initReflector)) {
28+
initReflector();
29+
}
30+
31+
let appProviders =
32+
isPresent(customProviders) ? [BROWSER_APP_PROVIDERS, customProviders] : BROWSER_APP_PROVIDERS;
33+
return platform(BROWSER_PROVIDERS).application(appProviders).bootstrap(appComponentType);
34+
}

modules/angular2/src/compiler/compiler.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export {
88
export {SourceModule, SourceWithImports} from './source_module';
99
export {PLATFORM_DIRECTIVES, PLATFORM_PIPES} from 'angular2/src/core/platform_directives_and_pipes';
1010

11-
import {assertionsEnabled, Type} from 'angular2/src/facade/lang';
11+
import {assertionsEnabled, Type, CONST_EXPR} from 'angular2/src/facade/lang';
1212
import {provide, Provider} from 'angular2/src/core/di';
1313
import {TemplateParser} from 'angular2/src/compiler/template_parser';
1414
import {HtmlParser} from 'angular2/src/compiler/html_parser';
@@ -28,26 +28,27 @@ import {AppRootUrl} from 'angular2/src/compiler/app_root_url';
2828
import {AnchorBasedAppRootUrl} from 'angular2/src/compiler/anchor_based_app_root_url';
2929
import {Parser, Lexer} from 'angular2/src/core/change_detection/change_detection';
3030

31-
export function compilerProviders(): Array<Type | Provider | any[]> {
32-
return [
33-
Lexer,
34-
Parser,
35-
HtmlParser,
36-
TemplateParser,
37-
TemplateNormalizer,
38-
RuntimeMetadataResolver,
39-
StyleCompiler,
40-
CommandCompiler,
41-
ChangeDetectionCompiler,
42-
provide(ChangeDetectorGenConfig,
43-
{useValue: new ChangeDetectorGenConfig(assertionsEnabled(), false, true)}),
44-
TemplateCompiler,
45-
provide(RuntimeCompiler, {useClass: RuntimeCompiler_}),
46-
provide(Compiler, {useExisting: RuntimeCompiler}),
47-
DomElementSchemaRegistry,
48-
provide(ElementSchemaRegistry, {useExisting: DomElementSchemaRegistry}),
49-
AnchorBasedAppRootUrl,
50-
provide(AppRootUrl, {useExisting: AnchorBasedAppRootUrl}),
51-
UrlResolver
52-
];
31+
function _createChangeDetectorGenConfig() {
32+
return new ChangeDetectorGenConfig(assertionsEnabled(), false, true);
5333
}
34+
35+
export const COMPILER_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR([
36+
Lexer,
37+
Parser,
38+
HtmlParser,
39+
TemplateParser,
40+
TemplateNormalizer,
41+
RuntimeMetadataResolver,
42+
StyleCompiler,
43+
CommandCompiler,
44+
ChangeDetectionCompiler,
45+
new Provider(ChangeDetectorGenConfig, {useFactory: _createChangeDetectorGenConfig, deps: []}),
46+
TemplateCompiler,
47+
new Provider(RuntimeCompiler, {useClass: RuntimeCompiler_}),
48+
new Provider(Compiler, {useExisting: RuntimeCompiler}),
49+
DomElementSchemaRegistry,
50+
new Provider(ElementSchemaRegistry, {useExisting: DomElementSchemaRegistry}),
51+
AnchorBasedAppRootUrl,
52+
new Provider(AppRootUrl, {useExisting: AnchorBasedAppRootUrl}),
53+
UrlResolver
54+
]);

modules/angular2/src/core/application.dart

Lines changed: 0 additions & 29 deletions
This file was deleted.

modules/angular2/src/core/application.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)