Skip to content

Commit 6b02b99

Browse files
Daniel Leibfilipesilva
Daniel Leib
authored andcommitted
feature(build): search for router children in node modules
Close angular#2718
1 parent bb9cfb9 commit 6b02b99

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

packages/angular-cli/models/find-lazy-modules.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs';
22
import * as glob from 'glob';
33
import * as path from 'path';
44
import * as ts from 'typescript';
5+
const resolve = require('resolve');
56

67
import {getSource, findNodes, getContentOfKeyLiteral} from '../utilities/ast-utils';
78

@@ -49,21 +50,26 @@ export function findLoadChildren(tsFilePath: string): string[] {
4950
}));
5051
}
5152

52-
5353
export function findLazyModules(projectRoot: any): {[key: string]: string} {
5454
const result: {[key: string]: string} = {};
5555
glob.sync(path.join(projectRoot, '/**/*.ts'))
5656
.forEach(tsPath => {
5757
findLoadChildren(tsPath).forEach(moduleName => {
58-
let fileName = path.resolve(projectRoot, moduleName) + '.ts';
59-
// If path is a relative path
60-
if (moduleName.startsWith('.')) {
61-
const tsPathInfo = path.parse(tsPath);
62-
fileName = path.resolve(tsPathInfo.dir, moduleName) + '.ts';
63-
}
58+
let fileName = moduleName.startsWith('.')
59+
? path.resolve(path.dirname(tsPath), moduleName) + '.ts'
60+
: path.resolve(projectRoot, moduleName) + '.ts';
61+
6462
if (fs.existsSync(fileName)) {
6563
// Put the moduleName as relative to the main.ts.
6664
result[moduleName] = fileName;
65+
} else {
66+
try {
67+
let res = resolve.sync(moduleName, { basedir: projectRoot });
68+
if (res) {
69+
result[moduleName] = res;
70+
}
71+
} catch (e) {
72+
}
6773
}
6874
});
6975
});
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const mockFs = require('mock-fs');
2+
const expect = require('chai').expect;
3+
import * as path from 'path';
4+
import {findLoadChildren, findLazyModules} from 'angular-cli/models/find-lazy-modules';
5+
6+
describe('find-lazy-modules', () => {
7+
beforeEach(() => {
8+
let mockDrive = {
9+
'node_modules/feature-module': {
10+
'package.json': '{ "main": "index.js" }',
11+
'index.js': ''
12+
},
13+
'src/app': {
14+
'app.module.ts': `RouterModule.forRoot([
15+
{ path: 'relative', loadChildren: './feature-a/feature-a.module' },
16+
{ path: 'absolute', loadChildren: 'src/app/feature-b/feature-b.module' },
17+
{ path: 'module', loadChildren: 'feature-module' },
18+
{ path: 'module2', loadChildren: 'feature-module/index.js' },
19+
{ path: 'invalid', loadChildren: 'invalid' }
20+
]);`,
21+
'feature-a': {
22+
'feature-a.module.ts': ''
23+
},
24+
'feature-b': {
25+
'feature-b.module.ts': ''
26+
}
27+
}
28+
};
29+
mockFs(mockDrive);
30+
});
31+
afterEach(() => {
32+
mockFs.restore();
33+
});
34+
35+
it('should find children', () => {
36+
let children = findLoadChildren('src/app/app.module.ts');
37+
expect(children.length).to.equal(5);
38+
expect(children.sort()).to.deep.equal([
39+
'./feature-a/feature-a.module',
40+
'feature-module',
41+
'feature-module/index.js',
42+
'invalid',
43+
'src/app/feature-b/feature-b.module'
44+
]);
45+
});
46+
47+
it('should find lazy modules', () => {
48+
let modules = findLazyModules('.');
49+
expect(modules).to.deep.equal({
50+
'./feature-a/feature-a.module':
51+
path.join(__dirname, '../../src/app/feature-a/feature-a.module.ts'),
52+
'src/app/feature-b/feature-b.module':
53+
path.join(__dirname, '../../src/app/feature-b/feature-b.module.ts'),
54+
'feature-module':
55+
path.join(__dirname, '../../node_modules/feature-module/index.js'),
56+
'feature-module/index.js':
57+
path.join(__dirname, '../../node_modules/feature-module/index.js')
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)