-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add NgModule, use it for angular1Module bundling
BREAKING CHANGE: bundle() now takes an NgModule decorated class as its first argument instead of a Component. **Before:** ```ts import { Component, bundle } from 'ng-metadata/core' @component({ selector: 'foo', template: '<h1>Foo!</h1>' }) class FooComponent {} const angular1Module = bundle(FooComponent) ``` **After:** ```ts import { NgModule, Component, bundle } from 'ng-metadata/core' @component({ selector: 'foo', template: '<h1>Foo!</h1>' }) class FooComponent {} @NgModule({ declarations: [FooComponent] }) class FooModule {} const angular1Module = bundle(FooModule) ``` BREAKING CHANGE: bootstrapping is now done on an NgModule, not on a Component. **Before:** ```ts import { bootstrap, Component } from 'ng-metadata/core' @component({ selector: 'app' }) class AppComponent {} bootstrap(AppComponent) ``` **After:** ```ts import { platformBrowserDynamic } from 'ng-metadata/platform-browser-dynamic' import { NgModule, Component } from 'ng-metadata/core' @component({ selector: 'app' }) class AppComponent {} @NgModule({ declarations: [AppComponent] }) class AppModule {} platformBrowserDynamic().bootstrapModule(AppModule) ```
- Loading branch information
1 parent
13c29ee
commit ed1c326
Showing
12 changed files
with
284 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,118 @@ | ||
import { global } from '../../facade/lang'; | ||
import { global, isFunction } from '../../facade/lang'; | ||
import { reflector } from '../reflection/reflection'; | ||
import { ComponentMetadata } from '../directives/metadata_directives'; | ||
import { ComponentMetadata, NgModuleMetadata } from '../directives/metadata_directives'; | ||
import { getInjectableName, provide } from '../di/provider'; | ||
import { isNgModule } from '../di/provider_util'; | ||
|
||
import { | ||
_isTypeRegistered, _normalizeProviders, _getNgModuleMetadataByType, | ||
_isTypeRegistered, _normalizeProviders, _getAngular1ModuleMetadataByType, | ||
_registerTypeProvider | ||
} from '../di/reflective_provider'; | ||
import { ListWrapper } from '../../facade/collections'; | ||
|
||
export function bundle( ComponentClass: Type, otherProviders: any[] = [], NgModule?: ng.IModule ): ng.IModule { | ||
function _bundleComponent( ComponentClass: Type, otherProviders: any[] = [], existingAngular1Module?: ng.IModule ): ng.IModule { | ||
|
||
// Support registering downgraded ng2 components directly | ||
const downgradedNgComponentName = reflector.downgradedNg2ComponentName( ComponentClass ); | ||
if (downgradedNgComponentName) { | ||
const ngModule = NgModule || global.angular.module( downgradedNgComponentName, [] ); | ||
ngModule.directive( downgradedNgComponentName, ComponentClass ); | ||
return ngModule; | ||
const angular1Module = existingAngular1Module || global.angular.module( downgradedNgComponentName, [] ); | ||
angular1Module.directive( downgradedNgComponentName, ComponentClass ); | ||
return angular1Module; | ||
} | ||
|
||
const ngModuleName = getInjectableName( ComponentClass ); | ||
const ngModule = NgModule || global.angular.module( ngModuleName, [] ); | ||
const angular1ModuleName = getInjectableName( ComponentClass ); | ||
const angular1Module = existingAngular1Module || global.angular.module( angular1ModuleName, [] ); | ||
const annotations = reflector.annotations( ComponentClass ); | ||
const cmpAnnotation: ComponentMetadata = annotations[ 0 ]; | ||
const { directives = [], pipes = [], providers = [], viewProviders = [] }={} = cmpAnnotation; | ||
|
||
// process component | ||
const [cmpName,cmpFactoryFn] = provide( ComponentClass ); | ||
const { providerName, providerMethod, moduleMethod } = _getNgModuleMetadataByType( ComponentClass ); | ||
const { providerName, providerMethod, moduleMethod } = _getAngular1ModuleMetadataByType( ComponentClass ); | ||
|
||
if ( _isTypeRegistered( cmpName, ngModule, providerName, providerMethod ) ) { | ||
return ngModule; | ||
if ( _isTypeRegistered( cmpName, angular1Module, providerName, providerMethod ) ) { | ||
return angular1Module; | ||
} | ||
|
||
// @TODO register via this once requires are resolved for 3 types of attr directive from template | ||
// _registerTypeProvider( ngModule, ComponentClass, { moduleMethod, name: cmpName, value: cmpFactoryFn } ); | ||
ngModule[moduleMethod]( cmpName, cmpFactoryFn ); | ||
// _registerTypeProvider( angular1Module, ComponentClass, { moduleMethod, name: cmpName, value: cmpFactoryFn } ); | ||
angular1Module[moduleMethod]( cmpName, cmpFactoryFn ); | ||
|
||
// 1. process component/directive decorator providers/viewProviders/pipes | ||
_normalizeProviders( ngModule, providers ); | ||
_normalizeProviders( ngModule, viewProviders ); | ||
_normalizeProviders( ngModule, pipes ); | ||
_normalizeProviders( angular1Module, providers ); | ||
_normalizeProviders( angular1Module, viewProviders ); | ||
_normalizeProviders( angular1Module, pipes ); | ||
|
||
|
||
// step through all directives | ||
ListWrapper.flattenDeep(directives).forEach( ( directiveType: Type ) => { | ||
bundle( directiveType, [], ngModule ); | ||
_bundleComponent( directiveType, [], angular1Module ); | ||
} ); | ||
|
||
// 2. process otherProviders argument | ||
// - providers can be string(ngModule reference), Type, StringMap(providerLiteral) | ||
// - providers can be string(angular1Module reference), Type, StringMap(providerLiteral) | ||
// - directives can't be registered as via global providers only @Injectable,@Pipe,{provide:any,use*:any} | ||
// registerProviders(ngModule, otherProviders); | ||
_normalizeProviders( ngModule, otherProviders ); | ||
// registerProviders(angular1Module, otherProviders); | ||
_normalizeProviders( angular1Module, otherProviders ); | ||
|
||
return angular1Module; | ||
} | ||
|
||
export function bundle( NgModuleClass: Type, otherProviders: any[] = [], existingAngular1Module?: ng.IModule ): ng.IModule { | ||
|
||
const angular1ModuleName = getInjectableName( NgModuleClass ); | ||
const angular1Module = existingAngular1Module || global.angular.module( angular1ModuleName, [] ); | ||
const annotations = reflector.annotations( NgModuleClass ); | ||
const ngModuleAnnotation: NgModuleMetadata = annotations[ 0 ]; | ||
if (!isNgModule(ngModuleAnnotation)) { | ||
throw new Error(`bundle() requires an @NgModule as it's first argument`) | ||
} | ||
const { declarations = [], providers = [], imports = [] }={} = ngModuleAnnotation; | ||
|
||
/** | ||
* Process `declarations` | ||
*/ | ||
ListWrapper.flattenDeep(declarations).forEach( ( directiveType: Type ) => { | ||
_bundleComponent( directiveType, [], angular1Module ); | ||
} ); | ||
|
||
/** | ||
* Process `providers` | ||
*/ | ||
_normalizeProviders( angular1Module, providers ); | ||
|
||
/** | ||
* Process `imports` | ||
*/ | ||
|
||
// 1. imports which are not NgModules | ||
const nonNgModuleImports: any[] = imports.filter((imported) => { | ||
if (!isFunction(imported)) { | ||
return true | ||
} | ||
const annotations = reflector.annotations( imported ); | ||
return !isNgModule(ngModuleAnnotation) | ||
}) | ||
|
||
_normalizeProviders( angular1Module, nonNgModuleImports ); | ||
|
||
// 2.imports which are NgModules | ||
const NgModuleImports: any[] = imports.filter((imported) => { | ||
if (!isFunction(imported)) { | ||
return false | ||
} | ||
const annotations = reflector.annotations( imported ); | ||
return isNgModule(ngModuleAnnotation) | ||
}) | ||
|
||
NgModuleImports.forEach(( importedNgModule: Type ) => { | ||
bundle(importedNgModule, [], angular1Module) | ||
}) | ||
|
||
/** | ||
* Process `otherProviders` | ||
*/ | ||
_normalizeProviders( angular1Module, otherProviders ); | ||
|
||
return ngModule; | ||
return angular1Module; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { createBootstrapFn } from './browser_utils' | ||
|
||
export const bootstrap = createBootstrapFn() | ||
|
||
export * from './title'; | ||
|
||
export const platformBrowserDynamic = () => { | ||
return { | ||
bootstrapModule: createBootstrapFn(), | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.