Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 30f3fa4

Browse files
committed
fix(@schematics/angular): fix import path for generated flat modules
If the target modulePath and the importModulePath are equal, do not add a path seperator to the resulting module import path.
1 parent 22e6a24 commit 30f3fa4

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/schematics/angular/module/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
5353
);
5454
const relativeDir = relative(dirname(modulePath), dirname(importModulePath));
5555
const relativePath = (relativeDir.startsWith('.') ? relativeDir : './' + relativeDir)
56-
+ '/' + basename(importModulePath);
56+
+ (relativeDir.length === 0 ? '' : '/') + basename(importModulePath);
5757
const changes = addImportToModule(source, modulePath,
5858
strings.classify(`${options.name}Module`),
5959
relativePath);

packages/schematics/angular/module/index_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ describe('Module Schematic', () => {
5555
expect(files.indexOf('/projects/bar/src/app/foo/foo.module.ts')).toBeGreaterThanOrEqual(0);
5656
});
5757

58+
it('should import a generated flat module with a correct relative path', () => {
59+
const options = {
60+
...defaultOptions,
61+
appRoot: 'app',
62+
module: 'app',
63+
flat: true,
64+
};
65+
66+
const tree = schematicRunner.runSchematic('module', options, appTree);
67+
const files = tree.files;
68+
expect(files.indexOf('/projects/bar/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
69+
expect(files.indexOf('/projects/bar/src/app/foo.module.ts')).toBeGreaterThanOrEqual(0);
70+
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
71+
expect(moduleContent).toMatch(/import { FooModule } from '\.\/foo.module'/);
72+
});
73+
5874
it('should import into another module', () => {
5975
const options = { ...defaultOptions, module: 'app.module.ts' };
6076

0 commit comments

Comments
 (0)