Skip to content

Commit 562ec50

Browse files
committed
fix(@angular/build): enforce library compilerOptions and skip unused .tsbuildinfo emit
Ensure library compilations explicitly set `noEmit: false`, `emitDeclarationOnly: false`, and `emitDecoratorMetadata: false` in `transformCompilerOptions` so that configurations extending a root `tsconfig.json` with `noEmit` or `emitDeclarationOnly` still emit both JavaScript and declaration files. Additionally, skip the application-specific `target < ES2022` Browserslist warning and `useDefineForClassFields = false` fallback when compiling libraries, and avoid serializing and writing on-disk `.tsbuildinfo` files during library builds since library outputs are emitted into in-memory maps on cold builds and watch rebuilds reuse the in-memory `BuilderProgram` directly.
1 parent 0fd4404 commit 562ec50

3 files changed

Lines changed: 81 additions & 19 deletions

File tree

‎packages/angular/build/src/builders/library/tests/behavior/build_spec.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
237237
);
238238
});
239239

240-
it('should write and reuse .tsbuildinfo in the persistent cache directory when cache is enabled', async () => {
240+
it('should succeed without emitting .tsbuildinfo in the persistent cache directory when cache is enabled', async () => {
241241
harness.useProject('lib', {
242242
root: 'projects/lib',
243243
sourceRoot: 'projects/lib/src',
@@ -258,7 +258,7 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
258258
expect(firstBuild.result?.success).toBeTrue();
259259
harness
260260
.expectFile('.angular/cache/0.0.0-PLACEHOLDER/lib/angular-build-library/.tsbuildinfo')
261-
.toExist();
261+
.toNotExist();
262262

263263
await harness.writeFile(
264264
'projects/lib/src/public-api.ts',
@@ -278,5 +278,32 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
278278
expect(dtsContent).toContain('LibComponent');
279279
expect(dtsContent).toContain('CACHED_BUILD');
280280
});
281+
282+
it('should override noEmit and emitDeclarationOnly in tsconfig.lib.json and emit both JS and .d.ts files', async () => {
283+
await harness.modifyFile('projects/lib/tsconfig.lib.json', (content) => {
284+
const tsconfig = JSON.parse(content);
285+
tsconfig.compilerOptions = {
286+
...tsconfig.compilerOptions,
287+
noEmit: true,
288+
emitDeclarationOnly: true,
289+
emitDecoratorMetadata: true,
290+
};
291+
292+
return JSON.stringify(tsconfig, null, 2);
293+
});
294+
295+
harness.useTarget('build', {
296+
...BASE_OPTIONS,
297+
});
298+
299+
const { result } = await harness.executeOnce();
300+
expect(result?.error).toBeUndefined();
301+
expect(result?.success).toBeTrue();
302+
303+
const fesmContent = harness.readFile('dist/lib/fesm2022/lib.mjs');
304+
expect(fesmContent).toContain('LibComponent');
305+
const dtsContent = harness.readFile('dist/lib/types/lib.d.ts');
306+
expect(dtsContent).toContain('LibComponent');
307+
});
281308
});
282309
});

‎packages/angular/build/src/tools/angular/compilation/angular-compilation_spec.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,32 @@ describe('AngularCompilation', () => {
479479
expect(compilerOptions.noEmitOnError).toBe(false);
480480
expect(compilerOptions.composite).toBe(false);
481481
});
482+
483+
it('enforces library compiler options and skips app-only target warning and .tsbuildinfo', () => {
484+
const { compilerOptions, warnings } = transformCompilerOptions(
485+
ts,
486+
{
487+
target: ts.ScriptTarget.ES2020,
488+
noEmit: true,
489+
emitDeclarationOnly: true,
490+
emitDecoratorMetadata: true,
491+
isolatedModules: true,
492+
},
493+
{
494+
cachePath: '/tmp/cache',
495+
},
496+
'tsconfig.lib.json',
497+
'library',
498+
);
499+
500+
expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2022);
501+
expect(compilerOptions.useDefineForClassFields).toBeUndefined();
502+
expect(compilerOptions.noEmit).toBe(false);
503+
expect(compilerOptions.emitDeclarationOnly).toBe(false);
504+
expect(compilerOptions.emitDecoratorMetadata).toBe(false);
505+
expect(compilerOptions.incremental).toBe(false);
506+
expect(compilerOptions.tsBuildInfoFile).toBeUndefined();
507+
expect(warnings).toEqual([]);
508+
});
482509
});
483510
});

‎packages/angular/build/src/tools/angular/compilation/compiler-options.ts‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ export function transformCompilerOptions(
3737
): { compilerOptions: ng.CompilerOptions; warnings: PartialMessage[] } {
3838
const compilerOptions = { ...baseCompilerOptions };
3939
const warnings: PartialMessage[] = [];
40-
const isLibrary = buildType === 'library';
40+
const isLibrary = buildType === 'library' || !!baseCompilerOptions.declaration;
4141

4242
if (
43-
compilerOptions.target === undefined ||
44-
compilerOptions.target < typeScript.ScriptTarget.ES2022
43+
!isLibrary &&
44+
(compilerOptions.target === undefined ||
45+
compilerOptions.target < typeScript.ScriptTarget.ES2022)
4546
) {
4647
// If 'useDefineForClassFields' is already defined in the users project leave the value as is.
4748
// Otherwise fallback to false due to https://github.com/microsoft/TypeScript/issues/45995
@@ -77,13 +78,32 @@ export function transformCompilerOptions(
7778
compilerOptions.compilationMode ??= 'partial';
7879
}
7980

80-
// Enable incremental compilation by default if caching is enabled and incremental is not explicitly disabled
81-
if (compilerOptions.incremental !== false && overrides?.cachePath) {
81+
// Enable incremental compilation by default if caching is enabled and incremental is not explicitly disabled.
82+
// Library compilation does not reuse on-disk .tsbuildinfo files across builds because all JS and .d.ts
83+
// outputs must be emitted into in-memory maps on every cold build, while watch mode uses in-memory
84+
// BuilderProgram state directly.
85+
if (!isLibrary && compilerOptions.incremental !== false && overrides?.cachePath) {
8286
compilerOptions.incremental = true;
8387
// Set the build info file location to the configured cache directory
8488
compilerOptions.tsBuildInfoFile = path.join(overrides.cachePath, '.tsbuildinfo');
8589
} else {
8690
compilerOptions.incremental = false;
91+
compilerOptions.tsBuildInfoFile = undefined;
92+
}
93+
94+
if (isLibrary) {
95+
compilerOptions.target = typeScript.ScriptTarget.ES2022;
96+
compilerOptions.module = typeScript.ModuleKind.ES2022;
97+
compilerOptions.moduleResolution = typeScript.ModuleResolutionKind.Bundler;
98+
compilerOptions.importHelpers = true;
99+
compilerOptions.declaration = true;
100+
compilerOptions.declarationMap = !!(
101+
overrides?.declarationMap ?? compilerOptions.declarationMap
102+
);
103+
compilerOptions.declarationDir = undefined;
104+
compilerOptions.noEmit = false;
105+
compilerOptions.emitDeclarationOnly = false;
106+
compilerOptions.emitDecoratorMetadata = false;
87107
}
88108

89109
if (
@@ -112,18 +132,6 @@ export function transformCompilerOptions(
112132
});
113133
}
114134

115-
if (isLibrary) {
116-
compilerOptions.target = typeScript.ScriptTarget.ES2022;
117-
compilerOptions.module = typeScript.ModuleKind.ES2022;
118-
compilerOptions.moduleResolution = typeScript.ModuleResolutionKind.Bundler;
119-
compilerOptions.importHelpers = true;
120-
compilerOptions.declaration = true;
121-
compilerOptions.declarationMap = !!(
122-
overrides?.declarationMap ?? compilerOptions.declarationMap
123-
);
124-
compilerOptions.declarationDir = undefined;
125-
}
126-
127135
// Synchronize custom resolve conditions.
128136
// Set if using the supported bundler resolution mode (bundler is the default in new projects)
129137
if (

0 commit comments

Comments
 (0)