diff --git a/packages/angular-cli/models/find-lazy-modules.ts b/packages/angular-cli/models/find-lazy-modules.ts index cb838b6e6f83..37c04c2f637f 100644 --- a/packages/angular-cli/models/find-lazy-modules.ts +++ b/packages/angular-cli/models/find-lazy-modules.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as glob from 'glob'; import * as path from 'path'; import * as ts from 'typescript'; +const resolve = require('resolve'); import {getSource, findNodes, getContentOfKeyLiteral} from '../utilities/ast-utils'; @@ -49,21 +50,26 @@ export function findLoadChildren(tsFilePath: string): string[] { })); } - export function findLazyModules(projectRoot: any): {[key: string]: string} { const result: {[key: string]: string} = {}; glob.sync(path.join(projectRoot, '/**/*.ts')) .forEach(tsPath => { findLoadChildren(tsPath).forEach(moduleName => { - let fileName = path.resolve(projectRoot, moduleName) + '.ts'; - // If path is a relative path - if (moduleName.startsWith('.')) { - const tsPathInfo = path.parse(tsPath); - fileName = path.resolve(tsPathInfo.dir, moduleName) + '.ts'; - } + let fileName = moduleName.startsWith('.') + ? path.resolve(path.dirname(tsPath), moduleName) + '.ts' + : path.resolve(projectRoot, moduleName) + '.ts'; + if (fs.existsSync(fileName)) { // Put the moduleName as relative to the main.ts. result[moduleName] = fileName; + } else { + try { + let res = resolve.sync(moduleName, { basedir: projectRoot }); + if (res) { + result[moduleName] = res; + } + } catch (e) { + } } }); }); diff --git a/tests/models/find-lazy-modules.spec.ts b/tests/models/find-lazy-modules.spec.ts new file mode 100644 index 000000000000..75f80cffd32f --- /dev/null +++ b/tests/models/find-lazy-modules.spec.ts @@ -0,0 +1,60 @@ +const mockFs = require('mock-fs'); +const expect = require('chai').expect; +import * as path from 'path'; +import {findLoadChildren, findLazyModules} from 'angular-cli/models/find-lazy-modules'; + +describe('find-lazy-modules', () => { + beforeEach(() => { + let mockDrive = { + 'node_modules/feature-module': { + 'package.json': '{ "main": "index.js" }', + 'index.js': '' + }, + 'src/app': { + 'app.module.ts': `RouterModule.forRoot([ + { path: 'relative', loadChildren: './feature-a/feature-a.module' }, + { path: 'absolute', loadChildren: 'src/app/feature-b/feature-b.module' }, + { path: 'module', loadChildren: 'feature-module' }, + { path: 'module2', loadChildren: 'feature-module/index.js' }, + { path: 'invalid', loadChildren: 'invalid' } + ]);`, + 'feature-a': { + 'feature-a.module.ts': '' + }, + 'feature-b': { + 'feature-b.module.ts': '' + } + } + }; + mockFs(mockDrive); + }); + afterEach(() => { + mockFs.restore(); + }); + + it('should find children', () => { + let children = findLoadChildren('src/app/app.module.ts'); + expect(children.length).to.equal(5); + expect(children.sort()).to.deep.equal([ + './feature-a/feature-a.module', + 'feature-module', + 'feature-module/index.js', + 'invalid', + 'src/app/feature-b/feature-b.module' + ]); + }); + + it('should find lazy modules', () => { + let modules = findLazyModules('.'); + expect(modules).to.deep.equal({ + './feature-a/feature-a.module': + path.join(__dirname, '../../src/app/feature-a/feature-a.module.ts'), + 'src/app/feature-b/feature-b.module': + path.join(__dirname, '../../src/app/feature-b/feature-b.module.ts'), + 'feature-module': + path.join(__dirname, '../../node_modules/feature-module/index.js'), + 'feature-module/index.js': + path.join(__dirname, '../../node_modules/feature-module/index.js') + }); + }); +});