mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 02:54:21 +08:00
fix(@schematics/angular): module
imports paths are incorrect when flat
option is used
Closes #12169
This commit is contained in:
parent
b8b36ba339
commit
d07e68aa95
@ -5,7 +5,7 @@
|
|||||||
* Use of this source code is governed by an MIT-style license that can be
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import { basename, dirname, normalize, relative, strings } from '@angular-devkit/core';
|
import { normalize, strings } from '@angular-devkit/core';
|
||||||
import {
|
import {
|
||||||
Rule,
|
Rule,
|
||||||
SchematicsException,
|
SchematicsException,
|
||||||
@ -24,7 +24,7 @@ import * as ts from 'typescript';
|
|||||||
import { addImportToModule } from '../utility/ast-utils';
|
import { addImportToModule } from '../utility/ast-utils';
|
||||||
import { InsertChange } from '../utility/change';
|
import { InsertChange } from '../utility/change';
|
||||||
import { getWorkspace } from '../utility/config';
|
import { getWorkspace } from '../utility/config';
|
||||||
import { findModuleFromOptions } from '../utility/find-module';
|
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
|
||||||
import { parseName } from '../utility/parse-name';
|
import { parseName } from '../utility/parse-name';
|
||||||
import { buildDefaultPath } from '../utility/project';
|
import { buildDefaultPath } from '../utility/project';
|
||||||
import { Schema as ModuleOptions } from './schema';
|
import { Schema as ModuleOptions } from './schema';
|
||||||
@ -36,7 +36,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
|
|||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
const modulePath = normalize('/' + options.module);
|
const modulePath = options.module;
|
||||||
|
|
||||||
const text = host.read(modulePath);
|
const text = host.read(modulePath);
|
||||||
if (text === null) {
|
if (text === null) {
|
||||||
@ -51,10 +51,9 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
|
|||||||
+ strings.dasherize(options.name)
|
+ strings.dasherize(options.name)
|
||||||
+ '.module',
|
+ '.module',
|
||||||
);
|
);
|
||||||
const relativeDir = relative(dirname(modulePath), dirname(importModulePath));
|
const relativePath = buildRelativePath(modulePath, importModulePath);
|
||||||
const relativePath = (relativeDir.startsWith('.') ? relativeDir : './' + relativeDir)
|
const changes = addImportToModule(source,
|
||||||
+ '/' + basename(importModulePath);
|
modulePath,
|
||||||
const changes = addImportToModule(source, modulePath,
|
|
||||||
strings.classify(`${options.name}Module`),
|
strings.classify(`${options.name}Module`),
|
||||||
relativePath);
|
relativePath);
|
||||||
|
|
||||||
|
@ -63,6 +63,15 @@ describe('Module Schematic', () => {
|
|||||||
expect(content).toMatch(/imports: \[[^\]]*FooModule[^\]]*\]/m);
|
expect(content).toMatch(/imports: \[[^\]]*FooModule[^\]]*\]/m);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should import into another module when using flat', () => {
|
||||||
|
const options = { ...defaultOptions, flat: true, module: 'app.module.ts' };
|
||||||
|
|
||||||
|
const tree = schematicRunner.runSchematic('module', options, appTree);
|
||||||
|
const content = tree.readContent('/projects/bar/src/app/app.module.ts');
|
||||||
|
expect(content).toMatch(/import { FooModule } from '.\/foo.module'/);
|
||||||
|
expect(content).toMatch(/imports: \[[^\]]*FooModule[^\]]*\]/m);
|
||||||
|
});
|
||||||
|
|
||||||
it('should import into another module (deep)', () => {
|
it('should import into another module (deep)', () => {
|
||||||
let tree = appTree;
|
let tree = appTree;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user