Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/angular-cli/models/find-lazy-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
}
}
});
});
Expand Down
60 changes: 60 additions & 0 deletions tests/models/find-lazy-modules.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
});
});
});