Skip to content

Commit 0551360

Browse files
alan-agius4mgechev
authored andcommitted
fix(@schematics/angular): exclusively list the files in tsconfigs (#15030)
* test: update tests to work with new stricter tsconfig file inclusions * fix(@schematics/angular): exclusively list the files in tsconfigs With the omission of `includes` or `files` in tsconfig files tsc will pick up all the files under the rootDir and make them as part of the compilation. In certain cases, redundant files will be picked up which will cause a slower compilations. Related to: TOOL-949
1 parent d0f2cc3 commit 0551360

File tree

19 files changed

+127
-39
lines changed

19 files changed

+127
-39
lines changed

packages/angular_devkit/build_angular/test/browser/errors_spec_large.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ describe('Browser Builder errors', () => {
2323
it('shows error when files are not part of the compilation', async () => {
2424
host.replaceInFile(
2525
'src/tsconfig.app.json',
26-
'"compilerOptions": {',
27-
`
28-
"files": ["main.ts"],
29-
"compilerOptions": {
30-
`,
26+
/,\r?\n?\s*"polyfills\.ts"/,
27+
'',
28+
);
29+
host.replaceInFile(
30+
'src/tsconfig.app.json',
31+
'"**/*.ts"',
32+
'"**/*.d.ts"',
3133
);
3234
const logger = new logging.Logger('');
3335
const logs: string[] = [];

packages/angular_devkit/build_angular/test/browser/lazy-module_spec_large.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { TestLogger } from '@angular-devkit/architect/testing';
10+
import { TestLogger, TestProjectHost } from '@angular-devkit/architect/testing';
1111
import { take, tap, timeout } from 'rxjs/operators';
1212
import {
1313
browserBuild,
@@ -30,6 +30,21 @@ describe('Browser Builder lazy modules', () => {
3030
});
3131
afterEach(async () => host.restore().toPromise());
3232

33+
function addLazyLoadedModulesInTsConfig(host: TestProjectHost, lazyModuleFiles: Record<string, string>) {
34+
const files = [
35+
...Object.keys(lazyModuleFiles),
36+
'main.ts',
37+
]
38+
.map(f => '"' + f.replace('src/', '') + '"')
39+
.join(', ');
40+
41+
host.replaceInFile(
42+
'src/tsconfig.app.json',
43+
'"main.ts"',
44+
`${files}`,
45+
);
46+
}
47+
3348
const cases: [string, Record<string, string>][] = [
3449
['string', lazyModuleStringImport],
3550
['function', lazyModuleFnImport],
@@ -40,6 +55,10 @@ describe('Browser Builder lazy modules', () => {
4055
host.writeMultipleFiles(lazyModuleFiles);
4156
host.writeMultipleFiles(imports);
4257

58+
if (name === 'string') {
59+
addLazyLoadedModulesInTsConfig(host, lazyModuleFiles);
60+
}
61+
4362
const { files } = await browserBuild(architect, host, target);
4463
expect('lazy-lazy-module.js' in files).toBe(true);
4564
});
@@ -91,6 +110,7 @@ describe('Browser Builder lazy modules', () => {
91110
it('supports lazy bundle for lazy routes with AOT', async () => {
92111
host.writeMultipleFiles(lazyModuleFiles);
93112
host.writeMultipleFiles(imports);
113+
addLazyLoadedModulesInTsConfig(host, lazyModuleFiles);
94114

95115
const { files } = await browserBuild(architect, host, target, { aot: true });
96116
if (ivyEnabled) {
@@ -131,10 +151,13 @@ describe('Browser Builder lazy modules', () => {
131151
});
132152

133153
it(`supports lazy bundle for System.import() calls`, async () => {
134-
host.writeMultipleFiles({
154+
const lazyfiles = {
135155
'src/lazy-module.ts': 'export const value = 42;',
136156
'src/main.ts': `declare var System: any; System.import('./lazy-module');`,
137-
});
157+
};
158+
159+
host.writeMultipleFiles(lazyfiles);
160+
addLazyLoadedModulesInTsConfig(host, lazyfiles);
138161

139162
const { files } = await browserBuild(architect, host, target);
140163
expect(files['lazy-module.js']).not.toBeUndefined();
@@ -199,6 +222,7 @@ describe('Browser Builder lazy modules', () => {
199222
}`,
200223
});
201224
host.replaceInFile('src/tsconfig.app.json', `"module": "es2015"`, `"module": "esnext"`);
225+
addLazyLoadedModulesInTsConfig(host, lazyModuleFiles);
202226

203227
const { files } = await browserBuild(architect, host, target, {
204228
lazyModules: ['src/app/lazy/lazy.module'],
@@ -227,6 +251,7 @@ describe('Browser Builder lazy modules', () => {
227251
}`,
228252
});
229253
host.replaceInFile('src/tsconfig.app.json', `"module": "es2015"`, `"module": "esnext"`);
254+
addLazyLoadedModulesInTsConfig(host, lazyModuleFiles);
230255
const { files } = await browserBuild(architect, host, target, {
231256
lazyModules: ['src/app/lazy/lazy.module'],
232257
aot: true,

packages/angular_devkit/build_angular/test/browser/output-hashing_spec_large.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
createArchitect,
1414
host,
1515
lazyModuleFiles,
16-
lazyModuleStringImport,
16+
lazyModuleFnImport,
1717
} from '../utils';
1818

1919
describe('Browser Builder output hashing', () => {
@@ -68,7 +68,7 @@ describe('Browser Builder output hashing', () => {
6868
let newHashes: Map<string, string>;
6969

7070
host.writeMultipleFiles(lazyModuleFiles);
71-
host.writeMultipleFiles(lazyModuleStringImport);
71+
host.writeMultipleFiles(lazyModuleFnImport);
7272

7373
const overrides = { outputHashing: 'all', extractCss: true };
7474

@@ -79,7 +79,7 @@ describe('Browser Builder output hashing', () => {
7979
// Save the current hashes.
8080
oldHashes = generateFileHashMap();
8181
host.writeMultipleFiles(lazyModuleFiles);
82-
host.writeMultipleFiles(lazyModuleStringImport);
82+
host.writeMultipleFiles(lazyModuleFnImport);
8383

8484
await browserBuild(architect, host, target, overrides);
8585
newHashes = generateFileHashMap();
@@ -117,7 +117,7 @@ describe('Browser Builder output hashing', () => {
117117
it('supports options', async () => {
118118
host.writeMultipleFiles({ 'src/styles.css': `h1 { background: url('./spectrum.png')}` });
119119
host.writeMultipleFiles(lazyModuleFiles);
120-
host.writeMultipleFiles(lazyModuleStringImport);
120+
host.writeMultipleFiles(lazyModuleFnImport);
121121

122122
// We must do several builds instead of a single one in watch mode, so that the output
123123
// path is deleted on each run and only contains the most recent files.

packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
host,
1616
ivyEnabled,
1717
lazyModuleFiles,
18-
lazyModuleStringImport,
18+
lazyModuleFnImport,
1919
} from '../utils';
2020

2121
describe('Browser Builder rebuilds', () => {
@@ -84,7 +84,7 @@ describe('Browser Builder rebuilds', () => {
8484
// No lazy chunk should exist.
8585
if (!hasLazyChunk) {
8686
phase = 2;
87-
host.writeMultipleFiles({ ...lazyModuleFiles, ...lazyModuleStringImport });
87+
host.writeMultipleFiles({ ...lazyModuleFiles, ...lazyModuleFnImport });
8888
}
8989
break;
9090

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/app",
55
"types": []
66
},
7+
"files": [
8+
"src/main.ts",
9+
"src/polyfills.ts"
10+
],<% if (!enableIvy) { %>
711
"include": [
812
"src/**/*.ts"
913
],
1014
"exclude": [
1115
"src/test.ts",
1216
"src/**/*.spec.ts"
13-
]<% if (enableIvy) { %>,
17+
]<% } %><% if (enableIvy) { %>
18+
"include": [
19+
"src/**/*.d.ts"
20+
],
1421
"angularCompilerOptions": {
1522
"enableIvy": true
1623
}<% } %>

packages/schematics/angular/application/index_spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,27 @@ describe('Application Schematic', () => {
232232
expect(workspace.projects.foo.architect.build.options.aot).toEqual(true);
233233
});
234234

235+
it('should set the right files, exclude, include in the tsconfig for VE projects', async () => {
236+
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
237+
.toPromise();
238+
const path = '/projects/foo/tsconfig.app.json';
239+
const tsConfig = JSON.parse(tree.readContent(path));
240+
expect(tsConfig.files).toEqual(['src/main.ts', 'src/polyfills.ts']);
241+
expect(tsConfig.exclude).toEqual(['src/test.ts', 'src/**/*.spec.ts']);
242+
expect(tsConfig.include).toEqual(['src/**/*.ts']);
243+
});
244+
245+
it('should set the right files, exclude, include in the tsconfig for Ivy projects', async () => {
246+
const options = { ...defaultOptions, enableIvy: true };
247+
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
248+
.toPromise();
249+
const path = '/projects/foo/tsconfig.app.json';
250+
const tsConfig = JSON.parse(tree.readContent(path));
251+
expect(tsConfig.files).toEqual(['src/main.ts', 'src/polyfills.ts']);
252+
expect(tsConfig.exclude).toBeUndefined();
253+
expect(tsConfig.include).toEqual(['src/**/*.d.ts']);
254+
});
255+
235256
describe(`update package.json`, () => {
236257
it(`should add build-angular to devDependencies`, async () => {
237258
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)

packages/schematics/angular/universal/files/root/__tsconfigFileName__.json.template

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"outDir": "<%= outDir %>-server",
55
"module": "commonjs"
66
},
7+
"files": [
8+
"src/main.server.ts"
9+
],
710
"angularCompilerOptions": {
811
"entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
912
}

packages/schematics/angular/universal/index_spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe('Universal Schematic', () => {
9292
outDir: './out-tsc/app-server',
9393
module: 'commonjs',
9494
},
95+
files: [
96+
"src/main.server.ts"
97+
],
9598
angularCompilerOptions: {
9699
entryModule: './src/app/app.server.module#AppServerModule',
97100
},
@@ -113,6 +116,9 @@ describe('Universal Schematic', () => {
113116
outDir: '../../out-tsc/app-server',
114117
module: 'commonjs',
115118
},
119+
files: [
120+
"src/main.server.ts"
121+
],
116122
angularCompilerOptions: {
117123
entryModule: './src/app/app.server.module#AppServerModule',
118124
},

tests/angular_devkit/build_angular/hello-world-app-ivy/src/tsconfig.app.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
"outDir": "../out-tsc/app",
55
"types": []
66
},
7-
"exclude": [
8-
"test.ts",
9-
"**/*.spec.ts"
7+
"files": [
8+
"main.ts",
9+
"polyfills.ts"
10+
],
11+
"include": [
12+
"**/*.d.ts"
1013
],
1114
"angularCompilerOptions": {
1215
"enableIvy": true

tests/angular_devkit/build_angular/hello-world-app-ivy/src/tsconfig.server.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
2-
"extends": "../tsconfig.json",
2+
"extends": "./tsconfig.app.json",
33
"compilerOptions": {
44
"outDir": "../dist-server",
55
"baseUrl": "./",
66
"module": "commonjs",
77
"types": []
88
},
9-
"exclude": [
10-
"test.ts",
11-
"**/*.spec.ts"
9+
"files": [
10+
"main.server.ts"
1211
],
1312
"angularCompilerOptions": {
1413
"entryModule": "app/app.server.module#AppServerModule",

tests/angular_devkit/build_angular/hello-world-app/src/tsconfig.app.json

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"outDir": "../out-tsc/app",
55
"types": []
66
},
7+
"files": [
8+
"main.ts",
9+
"polyfills.ts"
10+
],
11+
"include": [
12+
"**/*.ts"
13+
],
714
"exclude": [
815
"test.ts",
916
"**/*.spec.ts"

tests/angular_devkit/build_angular/hello-world-app/src/tsconfig.server.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
2-
"extends": "../tsconfig.json",
2+
"extends": "./tsconfig.app.json",
33
"compilerOptions": {
44
"outDir": "../dist-server",
5-
"baseUrl": "./",
65
"module": "commonjs",
76
"types": []
87
},
9-
"exclude": [
10-
"test.ts",
11-
"**/*.spec.ts"
8+
"files": [
9+
"main.server.ts"
1210
],
1311
"angularCompilerOptions": {
1412
"entryModule": "app/app.server.module#AppServerModule"

tests/legacy-cli/e2e/tests/basic/rebuild.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function() {
3737
FormsModule,
3838
HttpClientModule,
3939
RouterModule.forRoot([
40-
{ path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' }
40+
{ path: 'lazy', loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule) }
4141
])
4242
],
4343
providers: [],

tests/legacy-cli/e2e/tests/build/build-app-shell.ts

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export default function() {
5252
"module": "commonjs",
5353
"types": []
5454
},
55+
"files": [
56+
"src/main.server.ts"
57+
],
58+
"include": [
59+
"src/**/*.d.ts"
60+
],
5561
"angularCompilerOptions": {
5662
"entryModule": "src/app/app.server.module#AppServerModule"
5763
}

tests/legacy-cli/e2e/tests/build/build-errors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export default function() {
5050
.then(() =>
5151
updateJsonFile('./tsconfig.app.json', configJson => {
5252
configJson.include = ['src/**/*.ts'];
53+
configJson.exclude = ['**/**.spec.ts'];
5354
configJson.files = undefined;
5455
}),
5556
)

tests/legacy-cli/e2e/tests/build/dynamic-import.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export default async function() {
1414
];
1515
});
1616

17+
await updateJsonFile('tsconfig.app.json', tsConfig => {
18+
tsConfig.files.push('src/app/lazy/lazy.module.ts');
19+
});
20+
1721
// Update the app component to use the lazy module
1822
await writeFile('src/app/app.component.ts', `
1923
import { Component, SystemJsNgModuleLoader } from '@angular/core';

tests/legacy-cli/e2e/tests/build/lazy-load-syntax.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ export default async function () {
8282
buildTarget['configurations']['production'] = { aot: true };
8383
});
8484

85-
// Test string import.
86-
// Both Ivy and View Engine should support it.
87-
await replaceLoadChildren(`'./lazy/lazy.module#LazyModule'`);
88-
await ng('e2e');
89-
await ng('e2e', '--prod');
90-
9185
// Test `import()` style lazy load.
9286
// Both Ivy and View Engine should support it.
9387
await replaceLoadChildren(`() => import('./lazy/lazy.module').then(m => m.LazyModule)`);
9488

95-
// TODO: remove cast after https://github.com/angular/angular/pull/29832 is released.
96-
await replaceInFile(appRoutingModulePath, '];', '] as Routes;');
89+
await ng('e2e');
90+
await ng('e2e', '--prod');
9791

92+
// Test string import.
93+
// Both Ivy and View Engine should support it.
94+
await updateJsonFile('tsconfig.app.json', tsConfig => {
95+
tsConfig.files.push('src/app/lazy/lazy.module.ts');
96+
});
97+
await replaceLoadChildren(`'./lazy/lazy.module#LazyModule'`);
9898
await ng('e2e');
9999
await ng('e2e', '--prod');
100100
}

tests/legacy-cli/e2e/tests/build/platform-server.ts

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export default function() {
5050
"module": "commonjs",
5151
"types": []
5252
},
53+
"files": [
54+
"src/main.server.ts"
55+
],
56+
"include": [
57+
"src/**/*.d.ts"
58+
],
5359
"angularCompilerOptions": {
5460
"entryModule": "src/app/app.server.module#AppServerModule"
5561
}

tests/legacy-cli/e2e/tests/misc/lazy-module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export default function() {
1515
import { RouterModule } from '@angular/router';
1616
`))
1717
.then(() => replaceInFile('src/app/app.module.ts', 'imports: [', `imports: [
18-
RouterModule.forRoot([{ path: "lazy", loadChildren: 'src/app/lazy/lazy.module#LazyModule' }]),
19-
RouterModule.forRoot([{ path: "lazy1", loadChildren: './lazy/lazy.module#LazyModule' }]),
20-
RouterModule.forRoot([{ path: "lazy2", loadChildren: './too/lazy/lazy.module#LazyModule' }]),
18+
RouterModule.forRoot([{ path: "lazy", loadChildren: () => import('src/app/lazy/lazy.module').then(m => m.LazyModule) }]),
19+
RouterModule.forRoot([{ path: "lazy1", loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule) }]),
20+
RouterModule.forRoot([{ path: "lazy2", loadChildren: () => import('./too/lazy/lazy.module').then(m => m.LazyModule) }]),
2121
`))
2222
.then(() => ng('build', '--named-chunks'))
2323
.then(() => readdirSync('dist/test-project'))

0 commit comments

Comments
 (0)