Skip to content

feat(@schematics/angular): generate applications using TypeScript project references #30074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/app",
"types": []
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
"src/**/*.ts"
],
"exclude": [
"src/**/*.spec.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/spec",
"types": [
"jasmine"
]
},
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
"src/**/*.ts"
]
}
20 changes: 20 additions & 0 deletions packages/schematics/angular/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,39 @@ import {
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { Schema as ComponentOptions } from '../component/schema';
import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies';
import { JSONFile } from '../utility/json-file';
import { latestVersions } from '../utility/latest-versions';
import { relativePathToWorkspaceRoot } from '../utility/paths';
import { getWorkspace, updateWorkspace } from '../utility/workspace';
import { Builders, ProjectType } from '../utility/workspace-models';
import { Schema as ApplicationOptions, Style } from './schema';

function addTsProjectReference(...paths: string[]) {
return (host: Tree) => {
if (!host.exists('tsconfig.json')) {
return host;
}

const newReferences = paths.map((path) => ({ path }));

const file = new JSONFile(host, 'tsconfig.json');
const jsonPath = ['references'];
const value = file.get(jsonPath);
file.modify(jsonPath, Array.isArray(value) ? [...value, ...newReferences] : newReferences);
};
}

export default function (options: ApplicationOptions): Rule {
return async (host: Tree, context: SchematicContext) => {
const { appDir, appRootSelector, componentOptions, folderName, sourceDir } =
await getAppOptions(host, options);

return chain([
addAppToWorkspaceFile(options, appDir, folderName),
addTsProjectReference(join(normalize(appDir), 'tsconfig.app.json')),
options.skipTests
? noop()
: addTsProjectReference(join(normalize(appDir), 'tsconfig.spec.json')),
options.standalone
? noop()
: schematic('module', {
Expand Down
37 changes: 35 additions & 2 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ describe('Application Schematic', () => {
it('should set the right paths in the tsconfig.app.json', async () => {
const tree = await schematicRunner.runSchematic('application', defaultOptions, workspaceTree);

const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
expect(files).toEqual(['src/main.ts']);
const {
include,
exclude,
extends: _extends,
} = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
expect(include).toEqual(['src/**/*.ts']);
expect(exclude).toEqual(['src/**/*.spec.ts']);
expect(_extends).toBe('../../tsconfig.json');
});

Expand All @@ -105,6 +110,34 @@ describe('Application Schematic', () => {
expect(_extends).toBe('../../tsconfig.json');
});

it('should add project references in the root tsconfig.json', async () => {
const tree = await schematicRunner.runSchematic('application', defaultOptions, workspaceTree);

const { references } = readJsonFile(tree, '/tsconfig.json');
expect(references).toContain(
jasmine.objectContaining({ path: 'projects/foo/tsconfig.app.json' }),
);
expect(references).toContain(
jasmine.objectContaining({ path: 'projects/foo/tsconfig.spec.json' }),
);
});

it('should not add spec project reference in the root tsconfig.json with "skipTests" enabled', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{ ...defaultOptions, skipTests: true },
workspaceTree,
);

const { references } = readJsonFile(tree, '/tsconfig.json');
expect(references).toContain(
jasmine.objectContaining({ path: 'projects/foo/tsconfig.app.json' }),
);
expect(references).not.toContain(
jasmine.objectContaining({ path: 'projects/foo/tsconfig.spec.json' }),
);
});

it('should install npm dependencies when `skipInstall` is false', async () => {
await schematicRunner.runSchematic(
'application',
Expand Down
4 changes: 0 additions & 4 deletions packages/schematics/angular/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ function updateConfigFileApplicationBuilder(options: ServerOptions): Rule {
function updateTsConfigFile(tsConfigPath: string): Rule {
return (host: Tree) => {
const json = new JSONFile(host, tsConfigPath);
const filesPath = ['files'];
const files = new Set((json.get(filesPath) as string[] | undefined) ?? []);
files.add('src/' + serverMainEntryName);
json.modify(filesPath, [...files]);

const typePath = ['compilerOptions', 'types'];
const types = new Set((json.get(typePath) as string[] | undefined) ?? []);
Expand Down
1 change: 0 additions & 1 deletion packages/schematics/angular/server/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ describe('Server Schematic', () => {
const filePath = '/projects/bar/tsconfig.app.json';
const contents = parseJson(tree.readContent(filePath).toString());
expect(contents.compilerOptions.types).toEqual(['node']);
expect(contents.files).toEqual(['src/main.ts', 'src/main.server.ts']);
});

it(`should add 'provideClientHydration' to the providers list`, async () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/schematics/angular/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ function updateApplicationBuilderTsConfigRule(options: SSROptions): Rule {
}

const json = new JSONFile(host, tsConfigPath);

// Skip adding the files entry if the server entry would already be included
const include = json.get(['include']);
if (Array.isArray(include) && include.includes('src/**/*.ts')) {
return;
}

const filesPath = ['files'];
const files = new Set((json.get(filesPath) as string[] | undefined) ?? []);
files.add('src/server.ts');
Expand Down
19 changes: 17 additions & 2 deletions packages/schematics/angular/ssr/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,28 @@ describe('SSR Schematic', () => {
expect((schematicRunner.tasks[0].options as { command: string }).command).toBe('install');
});

it(`should update 'tsconfig.app.json' files with Express main file`, async () => {
it(`should not update 'tsconfig.app.json' files with Express main file already included`, async () => {
const tree = await schematicRunner.runSchematic('ssr', defaultOptions, appTree);
const { files } = tree.readJson('/projects/test-app/tsconfig.app.json') as {
files: string[];
};

expect(files).toEqual(['src/main.ts', 'src/main.server.ts', 'src/server.ts']);
expect(files).toBeUndefined();
});

it(`should update 'tsconfig.app.json' files with Express main file if not included`, async () => {
const appTsConfigContent = appTree.readJson('/projects/test-app/tsconfig.app.json') as {
include?: string[];
};
appTsConfigContent.include = [];
appTree.overwrite('/projects/test-app/tsconfig.app.json', JSON.stringify(appTsConfigContent));

const tree = await schematicRunner.runSchematic('ssr', defaultOptions, appTree);
const { files } = tree.readJson('/projects/test-app/tsconfig.app.json') as {
files: string[];
};

expect(files).toContain('src/server.ts');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",<% if (strict) { %>
"compilerOptions": {<% if (strict) { %>
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
Expand All @@ -22,5 +21,6 @@
"strictInputAccessModifiers": true,
"typeCheckHostBindings": true,
"strictTemplates": true<% } %>
}
},
"files": []
}
Loading