refactor: format & lint

This commit is contained in:
Victor Berchet 2016-12-13 17:44:52 -08:00
parent 7256d0ede5
commit a0a05041ac
11 changed files with 274 additions and 238 deletions

View File

@ -1,3 +1,11 @@
/**
* @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
*/
import {Component, ViewEncapsulation} from '@angular/core'; import {Component, ViewEncapsulation} from '@angular/core';
@ -7,4 +15,5 @@ import {Component, ViewEncapsulation} from '@angular/core';
styleUrls: ['app.component.css'], styleUrls: ['app.component.css'],
encapsulation: ViewEncapsulation.None encapsulation: ViewEncapsulation.None
}) })
export class AppComponent { } export class AppComponent {
}

View File

@ -1,23 +1,26 @@
import { NgModule, Component } from '@angular/core'; /**
import { BrowserModule } from '@angular/platform-browser'; * @license
import { RouterModule } from '@angular/router'; * Copyright Google Inc. All Rights Reserved.
import { AppComponent } from './app.component'; *
* 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
*/
@Component({ import {Component, NgModule} from '@angular/core';
selector: 'home-view', import {BrowserModule} from '@angular/platform-browser';
template: 'home!' import {RouterModule} from '@angular/router';
})
export class HomeView {} import {AppComponent} from './app.component';
@Component({selector: 'home-view', template: 'home!'})
export class HomeView {
}
@NgModule({ @NgModule({
declarations: [ declarations: [AppComponent, HomeView],
AppComponent,
HomeView
],
imports: [ imports: [
BrowserModule, BrowserModule, RouterModule.forRoot([
RouterModule.forRoot([
{path: 'lazy', loadChildren: './lazy.module#LazyModule'}, {path: 'lazy', loadChildren: './lazy.module#LazyModule'},
{path: 'feature2', loadChildren: 'feature2/feature2.module#Feature2Module'}, {path: 'feature2', loadChildren: 'feature2/feature2.module#Feature2Module'},
{path: '', component: HomeView} {path: '', component: HomeView}
@ -25,4 +28,5 @@ export class HomeView {}
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule {
}

View File

@ -1,20 +1,21 @@
import {NgModule, Component} from '@angular/core'; /**
* @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
*/
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router'; import {RouterModule} from '@angular/router';
@Component({ @Component({selector: 'feature-component', template: 'foo.html'})
selector: 'feature-component', export class FeatureComponent {
template: 'foo.html' }
})
export class FeatureComponent {}
@NgModule({ @NgModule({
declarations: [ declarations: [FeatureComponent],
FeatureComponent imports: [RouterModule.forChild([{path: '', component: FeatureComponent}])]
],
imports: [
RouterModule.forChild([
{ path: '', component: FeatureComponent}
])
]
}) })
export class FeatureModule {} export class FeatureModule {
}

View File

@ -1,19 +1,23 @@
import {NgModule, Component} from '@angular/core'; /**
* @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
*/
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router'; import {RouterModule} from '@angular/router';
@Component({ @Component({selector: 'lazy-feature-comp', template: 'lazy feature!'})
selector: 'lazy-feature-comp', export class LazyFeatureComponent {
template: 'lazy feature!' }
})
export class LazyFeatureComponent {}
@NgModule({ @NgModule({
imports: [ imports: [RouterModule.forChild([
RouterModule.forChild([ {path: '', component: LazyFeatureComponent, pathMatch: 'full'},
{path: '', component: LazyFeatureComponent, pathMatch: 'full'}, {path: 'feature', loadChildren: './feature.module#FeatureModule'}
{path: 'feature', loadChildren: './feature.module#FeatureModule'} ])],
])
],
declarations: [LazyFeatureComponent] declarations: [LazyFeatureComponent]
}) })
export class LazyFeatureModule { export class LazyFeatureModule {

View File

@ -1,20 +1,23 @@
import {NgModule, Component} from '@angular/core'; /**
* @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
*/
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router'; import {RouterModule} from '@angular/router';
@Component({ @Component({selector: 'feature-component', template: 'foo.html'})
selector: 'feature-component', export class FeatureComponent {
template: 'foo.html' }
})
export class FeatureComponent {}
@NgModule({ @NgModule({
declarations: [ declarations: [FeatureComponent],
FeatureComponent imports: [RouterModule.forChild([
], {path: '', component: FeatureComponent},
imports: [ ])]
RouterModule.forChild([
{ path: '', component: FeatureComponent },
])
]
}) })
export default class DefaultModule {} export default class DefaultModule {
}

View File

@ -1,22 +1,26 @@
import {NgModule, Component} from '@angular/core'; /**
* @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
*/
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router'; import {RouterModule} from '@angular/router';
@Component({ @Component({selector: 'feature-component', template: 'foo.html'})
selector: 'feature-component', export class FeatureComponent {
template: 'foo.html' }
})
export class FeatureComponent {}
@NgModule({ @NgModule({
declarations: [ declarations: [FeatureComponent],
FeatureComponent imports: [RouterModule.forChild([
], {path: '', component: FeatureComponent}, {path: 'd', loadChildren: './default.module'} {
imports: [ path: 'e',
RouterModule.forChild([ loadChildren: 'feature/feature.module#FeatureModule'
{ path: '', component: FeatureComponent }, }
{ path: 'd', loadChildren: './default.module' } ])]
{ path: 'e', loadChildren: 'feature/feature.module#FeatureModule' }
])
]
}) })
export class Feature2Module {} export class Feature2Module {
}

View File

@ -1,20 +1,24 @@
import {NgModule, Component} from '@angular/core'; /**
* @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
*/
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router'; import {RouterModule} from '@angular/router';
@Component({ @Component({selector: 'lazy-comp', template: 'lazy!'})
selector: 'lazy-comp', export class LazyComponent {
template: 'lazy!' }
})
export class LazyComponent {}
@NgModule({ @NgModule({
imports: [ imports: [RouterModule.forChild([
RouterModule.forChild([ {path: '', component: LazyComponent, pathMatch: 'full'},
{path: '', component: LazyComponent, pathMatch: 'full'}, {path: 'feature', loadChildren: './feature/feature.module#FeatureModule'},
{path: 'feature', loadChildren: './feature/feature.module#FeatureModule'}, {path: 'lazy-feature', loadChildren: './feature/lazy-feature.module#LazyFeatureModule'}
{path: 'lazy-feature', loadChildren: './feature/lazy-feature.module#LazyFeatureModule'} ])],
])
],
declarations: [LazyComponent] declarations: [LazyComponent]
}) })
export class LazyModule { export class LazyModule {

View File

@ -15,13 +15,7 @@ import * as path from 'path';
import * as ts from 'typescript'; import * as ts from 'typescript';
import * as assert from 'assert'; import * as assert from 'assert';
import {tsc} from '@angular/tsc-wrapped/src/tsc'; import {tsc} from '@angular/tsc-wrapped/src/tsc';
import { import {AngularCompilerOptions, CodeGenerator, CompilerHostContext, NodeCompilerHostContext, __NGTOOLS_PRIVATE_API_2} from '@angular/compiler-cli';
AngularCompilerOptions,
CodeGenerator,
CompilerHostContext,
NodeCompilerHostContext,
__NGTOOLS_PRIVATE_API_2
} from '@angular/compiler-cli';
const glob = require('glob'); const glob = require('glob');
@ -35,17 +29,17 @@ function main() {
console.log(`testing ngtools API...`); console.log(`testing ngtools API...`);
Promise.resolve() Promise.resolve()
.then(() => codeGenTest()) .then(() => codeGenTest())
.then(() => lazyRoutesTest()) .then(() => lazyRoutesTest())
.then(() => { .then(() => {
console.log('All done!'); console.log('All done!');
process.exit(0); process.exit(0);
}) })
.catch((err) => { .catch((err) => {
console.error(err.stack); console.error(err.stack);
console.error('Test failed'); console.error('Test failed');
process.exit(1); process.exit(1);
}); });
} }
@ -69,52 +63,53 @@ function codeGenTest() {
config.ngOptions.basePath = basePath; config.ngOptions.basePath = basePath;
console.log(`>>> running codegen for ${project}`); console.log(`>>> running codegen for ${project}`);
return __NGTOOLS_PRIVATE_API_2.codeGen({ return __NGTOOLS_PRIVATE_API_2
basePath, .codeGen({
compilerOptions: config.parsed.options, basePath,
program, compilerOptions: config.parsed.options, program, host,
host,
angularCompilerOptions: config.ngOptions, angularCompilerOptions: config.ngOptions,
// i18n options. // i18n options.
i18nFormat: null, i18nFormat: null,
i18nFile: null, i18nFile: null,
locale: null, locale: null,
readResource: (fileName: string) => { readResource: (fileName: string) => {
readResources.push(fileName); readResources.push(fileName);
return hostContext.readResource(fileName); return hostContext.readResource(fileName);
}
})
.then(() => {
console.log(`>>> codegen done, asserting read and wrote files`);
// Assert for each file that it has been read and each `ts` has a written file associated.
const allFiles = glob.sync(path.join(basePath, '**/*'), { nodir: true });
allFiles.forEach((fileName: string) => {
// Skip tsconfig.
if (fileName.match(/tsconfig-build.json$/)) {
return;
} }
})
.then(() => {
console.log(`>>> codegen done, asserting read and wrote files`);
// Assert that file was read. // Assert for each file that it has been read and each `ts` has a written file associated.
if (fileName.match(/\.module\.ts$/)) { const allFiles = glob.sync(path.join(basePath, '**/*'), {nodir: true});
const factory = fileName.replace(/\.module\.ts$/, '.module.ngfactory.ts');
assert(wroteFiles.indexOf(factory) != -1, `Expected file "${factory}" to be written.`); allFiles.forEach((fileName: string) => {
} else if (fileName.match(/\.css$/) || fileName.match(/\.html$/)) { // Skip tsconfig.
assert(readResources.indexOf(fileName) != -1, `Expected resource "${fileName}" to be read.`); if (fileName.match(/tsconfig-build.json$/)) {
} return;
}
// Assert that file was read.
if (fileName.match(/\.module\.ts$/)) {
const factory = fileName.replace(/\.module\.ts$/, '.module.ngfactory.ts');
assert(wroteFiles.indexOf(factory) != -1, `Expected file "${factory}" to be written.`);
} else if (fileName.match(/\.css$/) || fileName.match(/\.html$/)) {
assert(
readResources.indexOf(fileName) != -1,
`Expected resource "${fileName}" to be read.`);
}
});
console.log(`done, no errors.`);
})
.catch((e: any) => {
console.error(e.stack);
console.error('Compilation failed');
throw e;
}); });
console.log(`done, no errors.`);
})
.catch((e: any) => {
console.error(e.stack);
console.error('Compilation failed');
throw e;
});
} }
@ -146,17 +141,18 @@ function lazyRoutesTest() {
Object.keys(lazyRoutes).forEach((route: string) => { Object.keys(lazyRoutes).forEach((route: string) => {
assert(route in expectations, `Found a route that was not expected: "${route}".`); assert(route in expectations, `Found a route that was not expected: "${route}".`);
assert(lazyRoutes[route] == path.join(basePath, expectations[route]), assert(
`Route "${route}" does not point to the expected absolute path ` lazyRoutes[route] == path.join(basePath, expectations[route]),
+ `"${path.join(basePath, expectations[route])}". It points to "${lazyRoutes[route]}"`); `Route "${route}" does not point to the expected absolute path ` +
`"${path.join(basePath, expectations[route])}". It points to "${lazyRoutes[route]}"`);
}); });
// Verify that all expectations were met. // Verify that all expectations were met.
assert.deepEqual(Object.keys(lazyRoutes), Object.keys(expectations), assert.deepEqual(
`Expected routes listed to be: \n` Object.keys(lazyRoutes), Object.keys(expectations), `Expected routes listed to be: \n` +
+ ` ${JSON.stringify(Object.keys(expectations))}\n` ` ${JSON.stringify(Object.keys(expectations))}\n` +
+ `Actual:\n` `Actual:\n` +
+ ` ${JSON.stringify(Object.keys(lazyRoutes))}\n`); ` ${JSON.stringify(Object.keys(lazyRoutes))}\n`);
} }
main(); main();

View File

@ -1,3 +1,11 @@
/**
* @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
*/
/** /**
* This is a private API for the ngtools toolkit. * This is a private API for the ngtools toolkit.
* *
@ -5,9 +13,9 @@
* something else. * something else.
*/ */
import * as ts from 'typescript'; import {AotCompilerHost, StaticReflector} from '@angular/compiler';
import {StaticReflector, AotCompilerHost} from '@angular/compiler';
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped'; import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
import * as ts from 'typescript';
import {CodeGenerator} from './codegen'; import {CodeGenerator} from './codegen';
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host'; import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
@ -43,9 +51,7 @@ export interface NgTools_InternalApi_NG2_ListLazyRoutes_Options {
} }
export interface NgTools_InternalApi_NG_2_LazyRouteMap { export interface NgTools_InternalApi_NG_2_LazyRouteMap { [route: string]: string; }
[route: string]: string;
}
/** /**
@ -53,14 +59,12 @@ export interface NgTools_InternalApi_NG_2_LazyRouteMap {
* passed in the interface. * passed in the interface.
*/ */
class CustomLoaderModuleResolutionHostAdapter extends ModuleResolutionHostAdapter { class CustomLoaderModuleResolutionHostAdapter extends ModuleResolutionHostAdapter {
constructor(private _readResource: (path: string) => Promise<string>, constructor(
host: ts.ModuleResolutionHost) { private _readResource: (path: string) => Promise<string>, host: ts.ModuleResolutionHost) {
super(host); super(host);
} }
readResource(path: string) { readResource(path: string) { return this._readResource(path); }
return this._readResource(path);
}
} }
@ -74,9 +78,8 @@ export class NgTools_InternalApi_NG_2 {
* @private * @private
*/ */
static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<void> { static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<void> {
const hostContext: CompilerHostContext = new CustomLoaderModuleResolutionHostAdapter( const hostContext: CompilerHostContext =
options.readResource, options.host new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);
);
const cliOptions: NgcCliOptions = { const cliOptions: NgcCliOptions = {
i18nFormat: options.i18nFormat, i18nFormat: options.i18nFormat,
i18nFile: options.i18nFile, i18nFile: options.i18nFile,
@ -86,12 +89,7 @@ export class NgTools_InternalApi_NG_2 {
// Create the Code Generator. // Create the Code Generator.
const codeGenerator = CodeGenerator.create( const codeGenerator = CodeGenerator.create(
options.angularCompilerOptions, options.angularCompilerOptions, cliOptions, options.program, options.host, hostContext);
cliOptions,
options.program,
options.host,
hostContext
);
return codeGenerator.codegen(); return codeGenerator.codegen();
} }
@ -101,24 +99,26 @@ export class NgTools_InternalApi_NG_2 {
* @internal * @internal
* @private * @private
*/ */
static listLazyRoutes(options: NgTools_InternalApi_NG2_ListLazyRoutes_Options) static listLazyRoutes(options: NgTools_InternalApi_NG2_ListLazyRoutes_Options):
: NgTools_InternalApi_NG_2_LazyRouteMap { NgTools_InternalApi_NG_2_LazyRouteMap {
const angularCompilerOptions = options.angularCompilerOptions; const angularCompilerOptions = options.angularCompilerOptions;
const program = options.program; const program = options.program;
const moduleResolutionHost = new ModuleResolutionHostAdapter(options.host); const moduleResolutionHost = new ModuleResolutionHostAdapter(options.host);
const usePathMapping = !!angularCompilerOptions.rootDirs && angularCompilerOptions.rootDirs.length > 0; const usePathMapping =
const ngCompilerHost: AotCompilerHost = usePathMapping !!angularCompilerOptions.rootDirs && angularCompilerOptions.rootDirs.length > 0;
? new PathMappedCompilerHost(program, angularCompilerOptions, moduleResolutionHost) const ngCompilerHost: AotCompilerHost = usePathMapping ?
: new CompilerHost(program, angularCompilerOptions, moduleResolutionHost); new PathMappedCompilerHost(program, angularCompilerOptions, moduleResolutionHost) :
new CompilerHost(program, angularCompilerOptions, moduleResolutionHost);
const staticReflector = new StaticReflector(ngCompilerHost); const staticReflector = new StaticReflector(ngCompilerHost);
const routeMap = listLazyRoutesOfModule(options.entryModule, ngCompilerHost, staticReflector); const routeMap = listLazyRoutesOfModule(options.entryModule, ngCompilerHost, staticReflector);
return Object.keys(routeMap) return Object.keys(routeMap).reduce(
.reduce((acc: NgTools_InternalApi_NG_2_LazyRouteMap, route: string) => { (acc: NgTools_InternalApi_NG_2_LazyRouteMap, route: string) => {
acc[route] = routeMap[route].absoluteFilePath; acc[route] = routeMap[route].absoluteFilePath;
return acc; return acc;
}, {}); },
{});
} }
} }

View File

@ -1,11 +1,20 @@
/**
* @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
*/
/** /**
* This is a private API for the ngtools toolkit. * This is a private API for the ngtools toolkit.
* *
* This API should be stable for NG 2. It can be removed in NG 4..., but should be replaced by * This API should be stable for NG 2. It can be removed in NG 4..., but should be replaced by
* something else. * something else.
*/ */
import {AotCompilerHost, StaticReflector, StaticSymbol} from '@angular/compiler';
import {NgModule} from '@angular/core'; import {NgModule} from '@angular/core';
import {StaticSymbol, StaticReflector, AotCompilerHost} from '@angular/compiler';
// We cannot depend directly to @angular/router. // We cannot depend directly to @angular/router.
@ -19,7 +28,9 @@ export interface LazyRoute {
routeDef: RouteDef; routeDef: RouteDef;
absoluteFilePath: string; absoluteFilePath: string;
} }
export type LazyRouteMap = {[route: string]: LazyRoute}; export type LazyRouteMap = {
[route: string]: LazyRoute
};
// A route definition. Normally the short form 'path/to/module#ModuleClassName' is used by // A route definition. Normally the short form 'path/to/module#ModuleClassName' is used by
// the user, and this is a helper class to extract information from it. // the user, and this is a helper class to extract information from it.
@ -27,9 +38,9 @@ export class RouteDef {
private constructor(public readonly path: string, public readonly className: string = null) {} private constructor(public readonly path: string, public readonly className: string = null) {}
toString() { toString() {
return (this.className === null || this.className == 'default') return (this.className === null || this.className == 'default') ?
? this.path this.path :
: `${this.path}#${this.className}`; `${this.path}#${this.className}`;
} }
static fromString(entry: string): RouteDef { static fromString(entry: string): RouteDef {
@ -44,9 +55,8 @@ export class RouteDef {
* @returns {LazyRouteMap} * @returns {LazyRouteMap}
* @private * @private
*/ */
export function listLazyRoutesOfModule(entryModule: string, export function listLazyRoutesOfModule(
host: AotCompilerHost, entryModule: string, host: AotCompilerHost, reflector: StaticReflector): LazyRouteMap {
reflector: StaticReflector): LazyRouteMap {
const entryRouteDef = RouteDef.fromString(entryModule); const entryRouteDef = RouteDef.fromString(entryModule);
const containingFile = _resolveModule(entryRouteDef.path, entryRouteDef.path, host); const containingFile = _resolveModule(entryRouteDef.path, entryRouteDef.path, host);
const modulePath = `./${containingFile.replace(/^(.*)\//, '')}`; const modulePath = `./${containingFile.replace(/^(.*)\//, '')}`;
@ -55,27 +65,25 @@ export function listLazyRoutesOfModule(entryModule: string,
// List loadChildren of this single module. // List loadChildren of this single module.
const staticSymbol = reflector.findDeclaration(modulePath, className, containingFile); const staticSymbol = reflector.findDeclaration(modulePath, className, containingFile);
const ROUTES = reflector.findDeclaration(ROUTER_MODULE_PATH, ROUTER_ROUTES_SYMBOL_NAME); const ROUTES = reflector.findDeclaration(ROUTER_MODULE_PATH, ROUTER_ROUTES_SYMBOL_NAME);
const lazyRoutes: LazyRoute[] = _extractLazyRoutesFromStaticModule( const lazyRoutes: LazyRoute[] =
staticSymbol, reflector, host, ROUTES); _extractLazyRoutesFromStaticModule(staticSymbol, reflector, host, ROUTES);
const routes: LazyRouteMap = {}; const routes: LazyRouteMap = {};
lazyRoutes lazyRoutes.forEach((lazyRoute: LazyRoute) => {
.forEach((lazyRoute: LazyRoute) => { const route: string = lazyRoute.routeDef.toString();
const route: string = lazyRoute.routeDef.toString(); _assertRoute(routes, lazyRoute);
_assertRoute(routes, lazyRoute); routes[route] = lazyRoute;
routes[route] = lazyRoute;
const lazyModuleSymbol = reflector.findDeclaration( const lazyModuleSymbol = reflector.findDeclaration(
lazyRoute.absoluteFilePath, lazyRoute.routeDef.className || 'default'); lazyRoute.absoluteFilePath, lazyRoute.routeDef.className || 'default');
const subRoutes = _extractLazyRoutesFromStaticModule( const subRoutes = _extractLazyRoutesFromStaticModule(lazyModuleSymbol, reflector, host, ROUTES);
lazyModuleSymbol, reflector, host, ROUTES);
// Populate the map using the routes we just found. // Populate the map using the routes we just found.
subRoutes.forEach(subRoute => { subRoutes.forEach(subRoute => {
_assertRoute(routes, subRoute); _assertRoute(routes, subRoute);
routes[subRoute.routeDef.toString()] = subRoute; routes[subRoute.routeDef.toString()] = subRoute;
});
}); });
});
return routes; return routes;
} }
@ -101,10 +109,11 @@ function _resolveModule(modulePath: string, containingFile: string, host: AotCom
function _assertRoute(map: LazyRouteMap, route: LazyRoute) { function _assertRoute(map: LazyRouteMap, route: LazyRoute) {
const r = route.routeDef.toString(); const r = route.routeDef.toString();
if (map[r] && map[r].absoluteFilePath != route.absoluteFilePath) { if (map[r] && map[r].absoluteFilePath != route.absoluteFilePath) {
throw new Error(`Duplicated path in loadChildren detected: "${r}" is used in 2 loadChildren, ` throw new Error(
+ `but they point to different modules "(${map[r].absoluteFilePath} and ` `Duplicated path in loadChildren detected: "${r}" is used in 2 loadChildren, ` +
+ `"${route.absoluteFilePath}"). Webpack cannot distinguish on context and would fail to ` `but they point to different modules "(${map[r].absoluteFilePath} and ` +
+ 'load the proper one.'); `"${route.absoluteFilePath}"). Webpack cannot distinguish on context and would fail to ` +
'load the proper one.');
} }
} }
@ -114,34 +123,35 @@ function _assertRoute(map: LazyRouteMap, route: LazyRoute) {
* module and all statically referred modules. * module and all statically referred modules.
* @private * @private
*/ */
function _extractLazyRoutesFromStaticModule(staticSymbol: StaticSymbol, function _extractLazyRoutesFromStaticModule(
reflector: StaticReflector, staticSymbol: StaticSymbol, reflector: StaticReflector, host: AotCompilerHost,
host: AotCompilerHost, ROUTES: StaticSymbol): LazyRoute[] {
ROUTES: StaticSymbol): LazyRoute[] {
const moduleMetadata = _getNgModuleMetadata(staticSymbol, reflector); const moduleMetadata = _getNgModuleMetadata(staticSymbol, reflector);
const allRoutes: any = (moduleMetadata.imports || []) const allRoutes: any =
.filter(i => 'providers' in i) (moduleMetadata.imports || [])
.reduce((mem: Route[], m: any) => { .filter(i => 'providers' in i)
return mem.concat(_collectRoutes(m.providers || [], reflector, ROUTES)); .reduce((mem: Route[], m: any) => {
}, _collectRoutes(moduleMetadata.providers || [], reflector, ROUTES)); return mem.concat(_collectRoutes(m.providers || [], reflector, ROUTES));
}, _collectRoutes(moduleMetadata.providers || [], reflector, ROUTES));
const lazyRoutes: LazyRoute[] = _collectLoadChildren(allRoutes) const lazyRoutes: LazyRoute[] =
.reduce((acc: LazyRoute[], route: string) => { _collectLoadChildren(allRoutes).reduce((acc: LazyRoute[], route: string) => {
const routeDef = RouteDef.fromString(route); const routeDef = RouteDef.fromString(route);
const absoluteFilePath = _resolveModule(routeDef.path, staticSymbol.filePath, host); const absoluteFilePath = _resolveModule(routeDef.path, staticSymbol.filePath, host);
acc.push({ routeDef, absoluteFilePath }); acc.push({routeDef, absoluteFilePath});
return acc; return acc;
}, []); }, []);
const importedSymbols = ((moduleMetadata.imports || []) as any[]) const importedSymbols = ((moduleMetadata.imports || []) as any[])
.filter(i => i instanceof StaticSymbol) as StaticSymbol[]; .filter(i => i instanceof StaticSymbol) as StaticSymbol[];
return importedSymbols return importedSymbols
.reduce((acc: LazyRoute[], i: StaticSymbol) => { .reduce(
return acc.concat(_extractLazyRoutesFromStaticModule( (acc: LazyRoute[], i: StaticSymbol) => {
i, reflector, host, ROUTES)); return acc.concat(_extractLazyRoutesFromStaticModule(i, reflector, host, ROUTES));
}, []) },
.concat(lazyRoutes); [])
.concat(lazyRoutes);
} }
@ -150,7 +160,7 @@ function _extractLazyRoutesFromStaticModule(staticSymbol: StaticSymbol,
* @private * @private
*/ */
function _getNgModuleMetadata(staticSymbol: StaticSymbol, reflector: StaticReflector): NgModule { function _getNgModuleMetadata(staticSymbol: StaticSymbol, reflector: StaticReflector): NgModule {
const ngModules = reflector.annotations(staticSymbol).filter((s: any) => s instanceof NgModule); const ngModules = reflector.annotations(staticSymbol).filter((s: any) => s instanceof NgModule);
if (ngModules.length === 0) { if (ngModules.length === 0) {
throw new Error(`${staticSymbol.name} is not an NgModule`); throw new Error(`${staticSymbol.name} is not an NgModule`);
} }
@ -162,8 +172,8 @@ function _getNgModuleMetadata(staticSymbol: StaticSymbol, reflector: StaticRefle
* Return the routes from the provider list. * Return the routes from the provider list.
* @private * @private
*/ */
function _collectRoutes(providers: any[], reflector: StaticReflector, function _collectRoutes(
ROUTES: StaticSymbol): Route[] { providers: any[], reflector: StaticReflector, ROUTES: StaticSymbol): Route[] {
return providers.reduce((routeList: Route[], p: any) => { return providers.reduce((routeList: Route[], p: any) => {
if (p.provide === ROUTES) { if (p.provide === ROUTES) {
return routeList.concat(p.useValue); return routeList.concat(p.useValue);

View File

@ -341,7 +341,8 @@ export class JitCompiler implements Compiler {
if (!this._compilerConfig.useJit) { if (!this._compilerConfig.useJit) {
return interpretStatements(result.statements, result.stylesVar); return interpretStatements(result.statements, result.stylesVar);
} else { } else {
return jitStatements(`/${result.meta.moduleUrl}.ngstyle.js`, result.statements, result.stylesVar); return jitStatements(
`/${result.meta.moduleUrl}.ngstyle.js`, result.statements, result.stylesVar);
} }
} }
} }