feat(router): add the ANALYZE_FOR_PRECOMPILE provider to make dev ergonomics better

This commit is contained in:
vsavkin 2016-07-07 17:53:26 -07:00
parent 46e105f3ab
commit 96a9e66616
5 changed files with 24 additions and 25 deletions

View File

@ -7,7 +7,7 @@
*/ */
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common'; import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {APP_INITIALIZER, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core'; import {ANALYZE_FOR_PRECOMPILE, APP_INITIALIZER, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
import {Routes} from './config'; import {Routes} from './config';
import {Router} from './router'; import {Router} from './router';
@ -84,6 +84,7 @@ export function setupRouterInitializer(injector: Injector) {
*/ */
export function provideRouter(routes: Routes, config: ExtraOptions): any[] { export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
return [ return [
{provide: ANALYZE_FOR_PRECOMPILE, multi: true, useValue: routes},
{provide: ROUTES, useExisting: ROUTER_CONFIG}, {provide: ROUTER_CONFIG, useValue: routes}, {provide: ROUTES, useExisting: ROUTER_CONFIG}, {provide: ROUTER_CONFIG, useValue: routes},
{provide: ROUTER_CONFIGURATION, useValue: config}, Location, {provide: ROUTER_CONFIGURATION, useValue: config}, Location,
@ -125,7 +126,10 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
* @experimental * @experimental
*/ */
export function provideRoutes(routes: Routes): any { export function provideRoutes(routes: Routes): any {
return {provide: ROUTES, useValue: routes}; return [
{provide: ANALYZE_FOR_PRECOMPILE, multi: true, useValue: routes},
{provide: ROUTES, useValue: routes}
];
} }
/** /**

View File

@ -74,15 +74,12 @@ export class RouterOutlet {
} }
} catch (e) { } catch (e) {
if (!(e instanceof NoComponentFactoryError)) throw e; if (!(e instanceof NoComponentFactoryError)) throw e;
const componentName = component ? component.name : null;
// TODO: vsavkin uncomment this once ComponentResolver is deprecated console.warn(
// const componentName = component ? component.name : null; `'${componentName}' not found in precompile array. To ensure all components referred
// console.warn( to by the Routes are compiled, you must add '${componentName}' to the
// `'${componentName}' not found in precompile array. To ensure all components referred 'precompile' array of your application component. This will be required in a future
// to by the Routes are compiled, you must add '${componentName}' to the release of the router.`);
// 'precompile' array of your application component. This will be required in a future
// release of the router.`);
factory = snapshot._resolvedComponentFactory; factory = snapshot._resolvedComponentFactory;
} }

View File

@ -36,15 +36,6 @@ describe('Integration', () => {
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = createRoot(tcb, router, RootCmp); const fixture = createRoot(tcb, router, RootCmp);
@Component({
selector: 'child',
template: '<div *ngIf="alwaysTrue"><router-outlet></router-outlet></div>',
directives: ROUTER_DIRECTIVES
})
class LinkInNgIf {
alwaysTrue = true;
}
router.resetConfig([{ router.resetConfig([{
path: 'child', path: 'child',
component: LinkInNgIf, component: LinkInNgIf,
@ -1303,6 +1294,15 @@ class RelativeLinkInIfCmp {
show: boolean = false; show: boolean = false;
} }
@Component({
selector: 'child',
template: '<div *ngIf="alwaysTrue"><router-outlet></router-outlet></div>',
directives: ROUTER_DIRECTIVES
})
class LinkInNgIf {
alwaysTrue = true;
}
@Component({ @Component({
selector: 'root-cmp', selector: 'root-cmp',
template: `<router-outlet></router-outlet>`, template: `<router-outlet></router-outlet>`,
@ -1310,7 +1310,7 @@ class RelativeLinkInIfCmp {
precompile: [ precompile: [
BlankCmp, SimpleCmp, TeamCmp, UserCmp, StringLinkCmp, DummyLinkCmp, AbsoluteLinkCmp, BlankCmp, SimpleCmp, TeamCmp, UserCmp, StringLinkCmp, DummyLinkCmp, AbsoluteLinkCmp,
RelativeLinkCmp, DummyLinkWithParentCmp, LinkWithQueryParamsAndFragment, CollectParamsCmp, RelativeLinkCmp, DummyLinkWithParentCmp, LinkWithQueryParamsAndFragment, CollectParamsCmp,
QueryParamsAndFragmentCmp, StringLinkButtonCmp, WrapperCmp QueryParamsAndFragmentCmp, StringLinkButtonCmp, WrapperCmp, LinkInNgIf
] ]
}) })
class RootCmp { class RootCmp {

View File

@ -140,7 +140,6 @@ export const ROUTER_CONFIG = [
selector: 'inbox-app', selector: 'inbox-app',
viewProviders: [DbService], viewProviders: [DbService],
templateUrl: 'app/inbox-app.html', templateUrl: 'app/inbox-app.html',
directives: ROUTER_DIRECTIVES, directives: ROUTER_DIRECTIVES
precompile: [InboxCmp, DraftsCmp]
}) })
export class InboxApp {} export class InboxApp {}

View File

@ -25,7 +25,6 @@ export class InboxDetailCmp {
} }
@AppModule({ @AppModule({
providers: [provideRoutes([{path: ':id', component: InboxDetailCmp}])], providers: [provideRoutes([{path: ':id', component: InboxDetailCmp}])]
precompile: [InboxDetailCmp]
}) })
export default class InboxDetailModule {} export default class InboxDetailModule {}