diff --git a/packages/schematics/angular/library/files/tsconfig.lib.json.template b/packages/schematics/angular/library/files/tsconfig.lib.json.template index 869212b917f4..79a2168d3492 100644 --- a/packages/schematics/angular/library/files/tsconfig.lib.json.template +++ b/packages/schematics/angular/library/files/tsconfig.lib.json.template @@ -9,6 +9,9 @@ "inlineSources": true, "types": [] }, + "include": [ + "src/**/*.ts" + ], "exclude": [ "**/*.spec.ts" ] diff --git a/packages/schematics/angular/library/files/tsconfig.spec.json.template b/packages/schematics/angular/library/files/tsconfig.spec.json.template index 4fd01f8f600c..11ab3b8614ff 100644 --- a/packages/schematics/angular/library/files/tsconfig.spec.json.template +++ b/packages/schematics/angular/library/files/tsconfig.spec.json.template @@ -9,7 +9,6 @@ ] }, "include": [ - "**/*.spec.ts", - "**/*.d.ts" + "src/**/*.ts" ] } diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index 52ca74bc65ce..b409d986dd57 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -43,6 +43,21 @@ function updateTsConfig(packageName: string, ...paths: string[]) { }; } +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); + }; +} + function addDependenciesToPackageJson() { return (host: Tree) => { [ @@ -162,6 +177,12 @@ export default function (options: LibraryOptions): Rule { addLibToWorkspaceFile(options, libDir, packageName), options.skipPackageJson ? noop() : addDependenciesToPackageJson(), options.skipTsConfig ? noop() : updateTsConfig(packageName, './' + distRoot), + options.skipTsConfig + ? noop() + : addTsProjectReference( + './' + join(libDir, 'tsconfig.lib.json'), + './' + join(libDir, 'tsconfig.spec.json'), + ), options.standalone ? noop() : schematic('module', { diff --git a/packages/schematics/angular/library/index_spec.ts b/packages/schematics/angular/library/index_spec.ts index caedfb2b1739..1d5503d70b38 100644 --- a/packages/schematics/angular/library/index_spec.ts +++ b/packages/schematics/angular/library/index_spec.ts @@ -293,6 +293,24 @@ describe('Library Schematic', () => { const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json'); expect(tsConfigJson.compilerOptions.paths).toBeUndefined(); + expect(tsConfigJson.references).not.toContain( + jasmine.objectContaining({ path: './projects/foo/tsconfig.lib.json' }), + ); + expect(tsConfigJson.references).not.toContain( + jasmine.objectContaining({ path: './projects/foo/tsconfig.spec.json' }), + ); + }); + + it('should add project references in the root tsconfig.json', async () => { + const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree); + + const { references } = getJsonFileContent(tree, '/tsconfig.json'); + expect(references).toContain( + jasmine.objectContaining({ path: './projects/foo/tsconfig.lib.json' }), + ); + expect(references).toContain( + jasmine.objectContaining({ path: './projects/foo/tsconfig.spec.json' }), + ); }); });