Skip to content

Commit 16244c0

Browse files
committed
feat(@schematics/angular): generate applications using TypeScript project references
When generating a project (either via `ng new` or `ng generate application`), the created TypeScript configuration files (`tsconfig.app.json`/`tsconfig.spec.json`) will be setup as composite projects and added as project references to in the root `tsconfig.json`. This transforms the root `tsconfig.json` into a "solution" style configuration. This allows IDEs to more accurately discover and provide type information for the varying types of files (test, application, etc.) within each project. The Angular build process is otherwise unaffected by these changes.
1 parent 427bd84 commit 16244c0

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

packages/schematics/angular/application/files/common-files/tsconfig.app.json.template

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
{
44
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.json",
55
"compilerOptions": {
6+
"composite": true,
67
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/app",
78
"types": []
89
},
9-
"files": [
10-
"src/main.ts"
11-
],
1210
"include": [
13-
"src/**/*.d.ts"
11+
"src/**/*.ts"
12+
],
13+
"exclude": [
14+
"src/**/*.spec.ts"
1415
]
1516
}

packages/schematics/angular/application/files/common-files/tsconfig.spec.json.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
{
44
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.json",
55
"compilerOptions": {
6+
"composite": true,
67
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/spec",
78
"types": [
89
"jasmine"
910
]
1011
},
1112
"include": [
12-
"src/**/*.spec.ts",
13-
"src/**/*.d.ts"
13+
"src/**/*.ts"
1414
]
1515
}

packages/schematics/angular/application/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,39 @@ import {
2626
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2727
import { Schema as ComponentOptions } from '../component/schema';
2828
import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies';
29+
import { JSONFile } from '../utility/json-file';
2930
import { latestVersions } from '../utility/latest-versions';
3031
import { relativePathToWorkspaceRoot } from '../utility/paths';
3132
import { getWorkspace, updateWorkspace } from '../utility/workspace';
3233
import { Builders, ProjectType } from '../utility/workspace-models';
3334
import { Schema as ApplicationOptions, Style } from './schema';
3435

36+
function updateTsConfig(...paths: string[]) {
37+
return (host: Tree) => {
38+
if (!host.exists('tsconfig.json')) {
39+
return host;
40+
}
41+
42+
const newReferences = paths.map((path) => ({ path }));
43+
44+
const file = new JSONFile(host, 'tsconfig.json');
45+
const jsonPath = ['references'];
46+
const value = file.get(jsonPath);
47+
file.modify(jsonPath, Array.isArray(value) ? [...value, ...newReferences] : newReferences);
48+
};
49+
}
50+
3551
export default function (options: ApplicationOptions): Rule {
3652
return async (host: Tree, context: SchematicContext) => {
3753
const { appDir, appRootSelector, componentOptions, folderName, sourceDir } =
3854
await getAppOptions(host, options);
3955

4056
return chain([
4157
addAppToWorkspaceFile(options, appDir, folderName),
58+
updateTsConfig(
59+
join(normalize(appDir), 'tsconfig.app.json'),
60+
join(normalize(appDir), 'tsconfig.spec.json'),
61+
),
4262
options.standalone
4363
? noop()
4464
: schematic('module', {

packages/schematics/angular/application/index_spec.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ describe('Application Schematic', () => {
9393
it('should set the right paths in the tsconfig.app.json', async () => {
9494
const tree = await schematicRunner.runSchematic('application', defaultOptions, workspaceTree);
9595

96-
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
97-
expect(files).toEqual(['src/main.ts']);
96+
const {
97+
include,
98+
exclude,
99+
extends: _extends,
100+
} = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
101+
expect(include).toEqual(['src/**/*.ts']);
102+
expect(exclude).toEqual(['src/**/*.spec.ts']);
98103
expect(_extends).toBe('../../tsconfig.json');
99104
});
100105

packages/schematics/angular/workspace/files/tsconfig.json.template

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"strictInputAccessModifiers": true,
2323
"typeCheckHostBindings": true,
2424
"strictTemplates": true<% } %>
25-
}
25+
},
26+
"files": []
2627
}

0 commit comments

Comments
 (0)