Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/schematics/angular/app-shell/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ describe('App Shell Schematic', () => {
});

it('should not fail when AppModule have imported RouterModule already', async () => {
const updateRecorder = appTree.beginUpdate('/projects/bar/src/app/app.module.ts');
const updateRecorder = appTree.beginUpdate('/projects/bar/src/app/app-module.ts');
updateRecorder.insertLeft(0, "import { RouterModule } from '@angular/router';");
appTree.commitUpdate(updateRecorder);

const tree = await schematicRunner.runSchematic('app-shell', defaultOptions, appTree);
const filePath = '/projects/bar/src/app/app.module.ts';
const filePath = '/projects/bar/src/app/app-module.ts';
const content = tree.readContent(filePath);
expect(content).toMatch(/import { RouterModule } from '@angular\/router';/);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule<% if(experimentalZoneless) { %>, provideExperimentalZonelessChangeDetection<% } %> } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
<% if (routing) { %>
import { AppRoutingModule } from './app-routing.module';<% } %>
import { AppRoutingModule } from './app-routing-module';<% } %>
import { App } from './app';

@NgModule({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if(!!viewEncapsulation) { %>import { ViewEncapsulation } from '@angular/core';
<% }%>import { platformBrowser } from '@angular/platform-browser';
import { AppModule } from './app/app.module';
import { AppModule } from './app/app-module';

platformBrowser().bootstrapModule(AppModule, {
<% if(!experimentalZoneless) { %>ngZoneEventCoalescing: true,<% } %><% if(!!viewEncapsulation) { %>
Expand Down
30 changes: 15 additions & 15 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Application Schematic', () => {
'/projects/foo/src/index.html',
'/projects/foo/src/main.ts',
'/projects/foo/src/styles.css',
'/projects/foo/src/app/app.module.ts',
'/projects/foo/src/app/app-module.ts',
'/projects/foo/src/app/app.css',
'/projects/foo/src/app/app.ng.html',
'/projects/foo/src/app/app.spec.ts',
Expand Down Expand Up @@ -542,7 +542,7 @@ describe('Application Schematic', () => {
const options = { ...defaultOptions, standalone: true };

const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
const moduleFiles = tree.files.filter((file) => file.endsWith('.module.ts'));
const moduleFiles = tree.files.filter((file) => file.endsWith('-module.ts'));
expect(moduleFiles.length).toEqual(0);
});

Expand Down Expand Up @@ -625,11 +625,11 @@ describe('Application Schematic', () => {
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);

const files = tree.files;
expect(files).toContain('/projects/foo/src/app/app.module.ts');
expect(files).toContain('/projects/foo/src/app/app-routing.module.ts');
const moduleContent = tree.readContent('/projects/foo/src/app/app.module.ts');
expect(moduleContent).toMatch(/import { AppRoutingModule } from '.\/app-routing.module'/);
const routingModuleContent = tree.readContent('/projects/foo/src/app/app-routing.module.ts');
expect(files).toContain('/projects/foo/src/app/app-module.ts');
expect(files).toContain('/projects/foo/src/app/app-routing-module.ts');
const moduleContent = tree.readContent('/projects/foo/src/app/app-module.ts');
expect(moduleContent).toMatch(/import { AppRoutingModule } from '.\/app-routing-module'/);
const routingModuleContent = tree.readContent('/projects/foo/src/app/app-routing-module.ts');
expect(routingModuleContent).toMatch(/RouterModule.forRoot\(routes\)/);
});

Expand All @@ -640,7 +640,7 @@ describe('Application Schematic', () => {
workspaceTree,
);

const path = '/projects/foo/src/app/app.module.ts';
const path = '/projects/foo/src/app/app-module.ts';
const content = tree.readContent(path);
expect(content).toMatch(/import { BrowserModule } from '@angular\/platform-browser';/);
});
Expand All @@ -652,7 +652,7 @@ describe('Application Schematic', () => {
workspaceTree,
);

const path = '/projects/foo/src/app/app.module.ts';
const path = '/projects/foo/src/app/app-module.ts';
const content = tree.readContent(path);
expect(content).toMatch(/import { App } from '\.\/app';/);
});
Expand All @@ -669,8 +669,8 @@ describe('Application Schematic', () => {
'/projects/foo/tsconfig.spec.json',
'/projects/foo/src/main.ts',
'/projects/foo/src/styles.css',
'/projects/foo/src/app/app-routing.module.ts',
'/projects/foo/src/app/app.module.ts',
'/projects/foo/src/app/app-routing-module.ts',
'/projects/foo/src/app/app-module.ts',
'/projects/foo/src/app/app.css',
'/projects/foo/src/app/app.ng.html',
'/projects/foo/src/app/app.spec.ts',
Expand All @@ -696,7 +696,7 @@ describe('Application Schematic', () => {
);
});

it('should add provideExperimentalZonelessChangeDetection() in app.module.ts when experimentalZoneless is true', async () => {
it('should add provideExperimentalZonelessChangeDetection() in app-module.ts when experimentalZoneless is true', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
Expand All @@ -706,12 +706,12 @@ describe('Application Schematic', () => {
},
workspaceTree,
);
const path = '/projects/foo/src/app/app.module.ts';
const path = '/projects/foo/src/app/app-module.ts';
const fileContent = tree.readContent(path);
expect(fileContent).toContain('provideExperimentalZonelessChangeDetection()');
});

it('should not add provideExperimentalZonelessChangeDetection() in app.module.ts when experimentalZoneless is false', async () => {
it('should not add provideExperimentalZonelessChangeDetection() in app-module.ts when experimentalZoneless is false', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
Expand All @@ -721,7 +721,7 @@ describe('Application Schematic', () => {
},
workspaceTree,
);
const path = '/projects/foo/src/app/app.module.ts';
const path = '/projects/foo/src/app/app-module.ts';
const fileContent = tree.readContent(path);
expect(fileContent).not.toContain('provideExperimentalZonelessChangeDetection()');
});
Expand Down
10 changes: 9 additions & 1 deletion packages/schematics/angular/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ export default function (options: ComponentOptions): Rule {
options.path = buildDefaultPath(project);
}

options.module = findModuleFromOptions(host, options);
try {
options.module = findModuleFromOptions(host, options);
} catch {
options.module = findModuleFromOptions(host, {
...options,
moduleExt: '-module.ts',
routingModuleExt: '-routing-module.ts',
});
}

// Schematic templates require a defined type value
options.type ??= '';
Expand Down
22 changes: 11 additions & 11 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ describe('Component Schematic', () => {
it('should create a standalone component', async () => {
const options = { ...defaultOptions, standalone: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
const moduleContent = tree.readContent('/projects/bar/src/app/app-module.ts');
const componentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(componentContent).toContain('class FooComponent');
expect(moduleContent).not.toContain('FooComponent');
Expand Down Expand Up @@ -416,13 +416,13 @@ describe('Component Schematic', () => {
'/projects/baz/src/app/foo/foo.component.ts',
]),
);
const moduleContent = tree.readContent('/projects/baz/src/app/app.module.ts');
const moduleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
});

it('should use the module flag even if the module is a routing module', async () => {
const routingFileName = 'app-routing.module.ts';
const routingFileName = 'app-routing-module.ts';
const routingModulePath = `/projects/baz/src/app/${routingFileName}`;
const newTree = createAppModule(appTree, routingModulePath);
const options = { ...defaultNonStandaloneOptions, module: routingFileName };
Expand All @@ -435,7 +435,7 @@ describe('Component Schematic', () => {
const options = { ...defaultNonStandaloneOptions, name: 'dir/test-component' };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/baz/src/app/app.module.ts');
const content = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(content).toMatch(
/import { TestComponentComponent } from '\.\/dir\/test-component\/test-component.component'/,
);
Expand All @@ -455,15 +455,15 @@ describe('Component Schematic', () => {
};
appTree = await schematicRunner.runSchematic('component', options, appTree);

const content = appTree.readContent('/projects/baz/src/app/admin/module/module.module.ts');
const content = appTree.readContent('/projects/baz/src/app/admin/module/module-module.ts');
expect(content).toMatch(
/import { TestComponentComponent } from '..\/..\/other\/test-component\/test-component.component'/,
);
});

it('should find the closest module', async () => {
const options = { ...defaultNonStandaloneOptions };
const fooModule = '/projects/baz/src/app/foo/foo.module.ts';
const fooModule = '/projects/baz/src/app/foo/foo-module.ts';
appTree.create(
fooModule,
`
Expand All @@ -486,15 +486,15 @@ describe('Component Schematic', () => {
const options = { ...defaultNonStandaloneOptions, export: true };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const appModuleContent = tree.readContent('/projects/baz/src/app/app.module.ts');
const appModuleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(appModuleContent).toMatch(/exports: \[\n(\s*) {2}FooComponent\n\1\]/);
});

it('should import into a specified module', async () => {
const options = { ...defaultNonStandaloneOptions, module: 'app.module.ts' };
const options = { ...defaultNonStandaloneOptions, module: 'app-module.ts' };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const appModule = tree.readContent('/projects/baz/src/app/app.module.ts');
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');

expect(appModule).toMatch(/import { FooComponent } from '.\/foo\/foo.component'/);
});
Expand All @@ -511,8 +511,8 @@ describe('Component Schematic', () => {

// move the module
appTree.rename(
'/projects/baz/src/app/app.module.ts',
'/projects/baz/custom/app/app.module.ts',
'/projects/baz/src/app/app-module.ts',
'/projects/baz/custom/app/app-module.ts',
);
appTree = await schematicRunner.runSchematic(
'component',
Expand Down
11 changes: 9 additions & 2 deletions packages/schematics/angular/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ export default function (options: DirectiveOptions): Rule {
options.path = buildDefaultPath(project);
}

options.module = findModuleFromOptions(host, options);

try {
options.module = findModuleFromOptions(host, options);
} catch {
options.module = findModuleFromOptions(host, {
...options,
moduleExt: '-module.ts',
routingModuleExt: '-routing-module.ts',
});
}
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
Expand Down
14 changes: 7 additions & 7 deletions packages/schematics/angular/directive/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('Directive Schematic', () => {
const files = tree.files;
expect(files).toContain('/projects/baz/src/app/foo.spec.ts');
expect(files).toContain('/projects/baz/src/app/foo.ts');
const moduleContent = tree.readContent('/projects/baz/src/app/app.module.ts');
const moduleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+Foo\r?\n/m);
});
Expand All @@ -176,8 +176,8 @@ describe('Directive Schematic', () => {

// move the module
appTree.rename(
'/projects/baz/src/app/app.module.ts',
'/projects/baz/custom/app/app.module.ts',
'/projects/baz/src/app/app-module.ts',
'/projects/baz/custom/app/app-module.ts',
);
appTree = await schematicRunner.runSchematic(
'directive',
Expand All @@ -190,7 +190,7 @@ describe('Directive Schematic', () => {

it('should find the closest module', async () => {
const options = { ...defaultNonStandaloneOptions, flat: false };
const fooModule = '/projects/baz/src/app/foo/foo.module.ts';
const fooModule = '/projects/baz/src/app/foo/foo-module.ts';
appTree.create(
fooModule,
`
Expand All @@ -213,15 +213,15 @@ describe('Directive Schematic', () => {
const options = { ...defaultNonStandaloneOptions, export: true };

const tree = await schematicRunner.runSchematic('directive', options, appTree);
const appModuleContent = tree.readContent('/projects/baz/src/app/app.module.ts');
const appModuleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(appModuleContent).toMatch(/exports: \[\n(\s*) {2}Foo\n\1\]/);
});

it('should import into a specified module', async () => {
const options = { ...defaultNonStandaloneOptions, module: 'app.module.ts' };
const options = { ...defaultNonStandaloneOptions, module: 'app-module.ts' };

const tree = await schematicRunner.runSchematic('directive', options, appTree);
const appModule = tree.readContent('/projects/baz/src/app/app.module.ts');
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');

expect(appModule).toMatch(/import { Foo } from '.\/foo'/);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*/

export * from './lib/<%= dasherize(name) %>';<% if (!standalone) { %>
export * from './lib/<%= dasherize(name) %>.module';<% } %>
export * from './lib/<%= dasherize(name) %>-module';<% } %>
6 changes: 3 additions & 3 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Library Schematic', () => {

it('should not add reference to module file in entry-file', async () => {
const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
expect(tree.readContent('/projects/foo/src/my-index.ts')).not.toContain('foo.module');
expect(tree.readContent('/projects/foo/src/my-index.ts')).not.toContain('foo-module');
});

it('should create a standalone component', async () => {
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('Library Schematic', () => {
workspaceTree,
);

const fileContent = getFileContent(tree, '/projects/foo/src/lib/foo.module.ts');
const fileContent = getFileContent(tree, '/projects/foo/src/lib/foo-module.ts');
expect(fileContent).toMatch(/exports: \[\n(\s*) {2}Foo\n\1\]/);
});

Expand All @@ -420,7 +420,7 @@ describe('Library Schematic', () => {
'/projects/foo/tsconfig.lib.json',
'/projects/foo/tsconfig.lib.prod.json',
'/projects/foo/src/my-index.ts',
'/projects/foo/src/lib/foo.module.ts',
'/projects/foo/src/lib/foo-module.ts',
'/projects/foo/src/lib/foo.spec.ts',
'/projects/foo/src/lib/foo.ts',
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NgModule } from '@angular/core';<% if (commonModule) { %>
import { CommonModule } from '@angular/common';<% } %><% if (lazyRouteWithoutRouteModule) { %>
import { Routes, RouterModule } from '@angular/router';<% } %>
<% if ((!lazyRoute && routing) || lazyRouteWithRouteModule) { %>
import { <%= classify(name) %>RoutingModule } from './<%= dasherize(name) %>-routing.module';<% } %>
import { <%= classify(name) %>RoutingModule } from './<%= dasherize(name) %>-routing<%= typeSeparator %>module';<% } %>
<% if (lazyRouteWithoutRouteModule) { %>
const routes: Routes = [
{ path: '', component: <%= classify(name) %> }
Expand Down
25 changes: 18 additions & 7 deletions packages/schematics/angular/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function buildRelativeModulePath(options: ModuleOptions, modulePath: string): st
const importModulePath = join(
options.path ?? '',
options.flat ? '' : strings.dasherize(options.name),
strings.dasherize(options.name) + '.module',
strings.dasherize(options.name) + options.typeSeparator + 'module',
);

return buildRelativePath(modulePath, importModulePath);
Expand Down Expand Up @@ -113,9 +113,12 @@ function addRouteDeclarationToNgModule(
}

function getRoutingModulePath(host: Tree, modulePath: string): string | undefined {
const routingModulePath = modulePath.endsWith(ROUTING_MODULE_EXT)
? modulePath
: modulePath.replace(MODULE_EXT, ROUTING_MODULE_EXT);
const routingModulePath =
modulePath.endsWith(ROUTING_MODULE_EXT) || modulePath.endsWith('-routing-module.ts')
? modulePath
: modulePath
.replace(MODULE_EXT, ROUTING_MODULE_EXT)
.replace('-module.ts', '-routing-module.ts');

return host.exists(routingModulePath) ? routingModulePath : undefined;
}
Expand All @@ -135,7 +138,15 @@ export default function (options: ModuleOptions): Rule {
}

if (options.module) {
options.module = findModuleFromOptions(host, options);
try {
options.module = findModuleFromOptions(host, options);
} catch {
options.module = findModuleFromOptions(host, {
...options,
moduleExt: '-module.ts',
routingModuleExt: '-routing-module.ts',
});
}
}

let routingModulePath;
Expand All @@ -153,7 +164,7 @@ export default function (options: ModuleOptions): Rule {
const templateSource = apply(url('./files'), [
options.routing || (isLazyLoadedModuleGen && routingModulePath)
? noop()
: filter((path) => !path.endsWith('-routing.module.ts.template')),
: filter((path) => !path.includes('-routing')),
applyTemplates({
...strings,
'if-flat': (s: string) => (options.flat ? '' : s),
Expand All @@ -167,7 +178,7 @@ export default function (options: ModuleOptions): Rule {
const moduleDasherized = strings.dasherize(options.name);
const modulePath = `${
!options.flat ? moduleDasherized + '/' : ''
}${moduleDasherized}.module.ts`;
}${moduleDasherized}${options.typeSeparator}module.ts`;

const componentOptions: ComponentOptions = {
module: modulePath,
Expand Down
Loading