Skip to content

Commit fcea244

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@schematics/angular): lazy loading module generation routing module lookup
Fixes: #15552
1 parent b204dd8 commit fcea244

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

packages/schematics/angular/module/index.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
2424
import { addImportToModule, addRouteDeclarationToModule } from '../utility/ast-utils';
2525
import { InsertChange } from '../utility/change';
26-
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
26+
import { MODULE_EXT, ROUTING_MODULE_EXT, buildRelativePath, findModuleFromOptions } from '../utility/find-module';
2727
import { applyLintFix } from '../utility/lint-fix';
2828
import { parseName } from '../utility/parse-name';
2929
import { createDefaultPath } from '../utility/workspace';
@@ -112,17 +112,12 @@ function addRouteDeclarationToNgModule(
112112
};
113113
}
114114

115-
function getRoutingModulePath(host: Tree, options: ModuleOptions): Path | undefined {
116-
let path: Path | undefined;
117-
const modulePath = options.module as string;
118-
const routingModuleName = modulePath.split('.')[0] + '-routing';
119-
const { module, ...rest } = options;
115+
function getRoutingModulePath(host: Tree, modulePath: string): Path | undefined {
116+
const routingModulePath = modulePath.endsWith(ROUTING_MODULE_EXT)
117+
? modulePath
118+
: modulePath.replace(MODULE_EXT, ROUTING_MODULE_EXT);
120119

121-
try {
122-
path = findModuleFromOptions(host, { module: routingModuleName, ...rest });
123-
} catch {}
124-
125-
return path;
120+
return host.exists(routingModulePath) ? normalize(routingModulePath) : undefined;
126121
}
127122

128123
function buildRoute(options: ModuleOptions, modulePath: string) {
@@ -143,17 +138,17 @@ export default function (options: ModuleOptions): Rule {
143138
options.module = findModuleFromOptions(host, options);
144139
}
145140

146-
const parsedPath = parseName(options.path, options.name);
147-
options.name = parsedPath.name;
148-
options.path = parsedPath.path;
149-
150141
let routingModulePath: Path | undefined;
151142
const isLazyLoadedModuleGen = options.route && options.module;
152143
if (isLazyLoadedModuleGen) {
153144
options.routingScope = RoutingScope.Child;
154-
routingModulePath = getRoutingModulePath(host, options);
145+
routingModulePath = getRoutingModulePath(host, options.module as string);
155146
}
156147

148+
const parsedPath = parseName(options.path, options.name);
149+
options.name = parsedPath.name;
150+
options.path = parsedPath.path;
151+
157152
const templateSource = apply(url('./files'), [
158153
options.routing || isLazyLoadedModuleGen && !!routingModulePath
159154
? noop()

packages/schematics/angular/module/index_spec.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Schema as ApplicationOptions } from '../application/schema';
1010
import { Schema as WorkspaceOptions } from '../workspace/schema';
1111
import { Schema as ModuleOptions } from './schema';
1212

13+
// tslint:disable-next-line: no-big-function
1314
describe('Module Schematic', () => {
1415
const schematicRunner = new SchematicTestRunner(
1516
'@schematics/angular',
@@ -129,11 +130,13 @@ describe('Module Schematic', () => {
129130
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
130131
const files = tree.files;
131132

132-
expect(files).toContain('/projects/bar/src/app/foo/foo.module.ts');
133-
expect(files).toContain('/projects/bar/src/app/foo/foo-routing.module.ts');
134-
expect(files).toContain('/projects/bar/src/app/foo/foo.component.ts');
135-
expect(files).toContain('/projects/bar/src/app/foo/foo.component.html');
136-
expect(files).toContain('/projects/bar/src/app/foo/foo.component.css');
133+
expect(files).toEqual(jasmine.arrayContaining([
134+
'/projects/bar/src/app/foo/foo.module.ts',
135+
'/projects/bar/src/app/foo/foo-routing.module.ts',
136+
'/projects/bar/src/app/foo/foo.component.ts',
137+
'/projects/bar/src/app/foo/foo.component.html',
138+
'/projects/bar/src/app/foo/foo.component.css',
139+
]));
137140

138141
const appRoutingModuleContent = tree.readContent('/projects/bar/src/app/app-routing.module.ts');
139142
expect(appRoutingModuleContent).toMatch(
@@ -196,16 +199,55 @@ describe('Module Schematic', () => {
196199
).toPromise();
197200
const files = tree.files;
198201

199-
expect(files).toContain('/projects/bar/src/app/foo.module.ts');
200-
expect(files).toContain('/projects/bar/src/app/foo-routing.module.ts');
201-
expect(files).toContain('/projects/bar/src/app/foo.component.ts');
202-
expect(files).toContain('/projects/bar/src/app/foo.component.html');
203-
expect(files).toContain('/projects/bar/src/app/foo.component.css');
202+
expect(files).toEqual(jasmine.arrayContaining([
203+
'/projects/bar/src/app/foo.module.ts',
204+
'/projects/bar/src/app/foo-routing.module.ts',
205+
'/projects/bar/src/app/foo.component.ts',
206+
'/projects/bar/src/app/foo.component.html',
207+
'/projects/bar/src/app/foo.component.css',
208+
]));
204209

205210
const appRoutingModuleContent = tree.readContent('/projects/bar/src/app/app-routing.module.ts');
206211
expect(appRoutingModuleContent).toMatch(
207212
/path: '\/new-route', loadChildren: \(\) => import\('.\/foo.module'\).then\(m => m.FooModule\)/,
208213
);
209214
});
215+
216+
it('should generate a lazy loaded module and add route in another parallel routing module', async () => {
217+
await schematicRunner.runSchematicAsync(
218+
'module',
219+
{
220+
...defaultOptions,
221+
name: 'foo',
222+
routing: true,
223+
},
224+
appTree,
225+
).toPromise();
226+
227+
const tree = await schematicRunner.runSchematicAsync(
228+
'module',
229+
{
230+
...defaultOptions,
231+
name: 'bar',
232+
module: 'foo',
233+
route: 'new-route',
234+
},
235+
appTree,
236+
).toPromise();
237+
238+
expect(tree.files).toEqual(jasmine.arrayContaining([
239+
'/projects/bar/src/app/foo/foo-routing.module.ts',
240+
'/projects/bar/src/app/foo/foo.module.ts',
241+
'/projects/bar/src/app/bar/bar-routing.module.ts',
242+
'/projects/bar/src/app/bar/bar.module.ts',
243+
'/projects/bar/src/app/bar/bar.component.ts',
244+
]));
245+
246+
const barRoutingModuleContent = tree.readContent('/projects/bar/src/app/bar/bar-routing.module.ts');
247+
expect(barRoutingModuleContent).toContain(`path: '', component: BarComponent `);
248+
249+
const fooRoutingModuleContent = tree.readContent('/projects/bar/src/app/foo/foo-routing.module.ts');
250+
expect(fooRoutingModuleContent).toContain(`loadChildren: () => import('../bar/bar.module').then(m => m.BarModule)`);
251+
});
210252
});
211253
});

packages/schematics/angular/utility/find-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export interface ModuleOptions {
2626
routingModuleExt?: string;
2727
}
2828

29-
const MODULE_EXT = '.module.ts';
30-
const ROUTING_MODULE_EXT = '-routing.module.ts';
29+
export const MODULE_EXT = '.module.ts';
30+
export const ROUTING_MODULE_EXT = '-routing.module.ts';
3131

3232
/**
3333
* Find the module referred by a set of options passed to the schematics.

0 commit comments

Comments
 (0)