Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import path from 'node:path';
import ts from 'typescript';
import type { AngularHostOptions } from '../angular-host';
import { CompilerOptionOverrides, transformCompilerOptions } from './compiler-options';
Expand Down Expand Up @@ -476,8 +477,36 @@ describe('AngularCompilation', () => {
expect(compilerOptions['_useTypeScriptTranspilation']).toBe(true);
expect(compilerOptions.supportTestBed).toBe(true);
expect(compilerOptions.supportJitMode).toBe(true);
expect(compilerOptions.noEmit).toBe(false);
expect(compilerOptions.noEmitOnError).toBe(false);
expect(compilerOptions.emitDeclarationOnly).toBe(false);
expect(compilerOptions.composite).toBe(false);
});

it('enforces library compiler options and skips app-only target warning', () => {
const { compilerOptions, warnings } = transformCompilerOptions(
ts,
{
target: ts.ScriptTarget.ES2020,
noEmit: true,
emitDeclarationOnly: true,
isolatedModules: true,
},
{
cachePath: '/tmp/cache',
},
'tsconfig.lib.json',
'library',
);

expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2022);
expect(compilerOptions.module).toBe(ts.ModuleKind.Preserve);
expect(compilerOptions.useDefineForClassFields).toBeUndefined();
expect(compilerOptions.noEmit).toBe(false);
expect(compilerOptions.emitDeclarationOnly).toBe(false);
expect(compilerOptions.incremental).toBe(true);
expect(compilerOptions.tsBuildInfoFile).toBe(path.join('/tmp/cache', '.tsbuildinfo'));
expect(warnings).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ export function transformCompilerOptions(
const warnings: PartialMessage[] = [];
const isLibrary = buildType === 'library';

if (isLibrary) {
compilerOptions.target = typeScript.ScriptTarget.ES2022;
compilerOptions.module = typeScript.ModuleKind.Preserve;
compilerOptions.importHelpers = true;
compilerOptions.declaration = true;
compilerOptions.declarationMap = !!(
overrides?.declarationMap ?? compilerOptions.declarationMap
);
compilerOptions.declarationDir = undefined;
}

if (
compilerOptions.target === undefined ||
compilerOptions.target < typeScript.ScriptTarget.ES2022
Expand Down Expand Up @@ -112,18 +123,6 @@ export function transformCompilerOptions(
});
}

if (isLibrary) {
compilerOptions.target = typeScript.ScriptTarget.ES2022;
compilerOptions.module = typeScript.ModuleKind.ES2022;
compilerOptions.moduleResolution = typeScript.ModuleResolutionKind.Bundler;
compilerOptions.importHelpers = true;
compilerOptions.declaration = true;
compilerOptions.declarationMap = !!(
overrides?.declarationMap ?? compilerOptions.declarationMap
);
compilerOptions.declarationDir = undefined;
}

// Synchronize custom resolve conditions.
// Set if using the supported bundler resolution mode (bundler is the default in new projects)
if (
Expand All @@ -137,7 +136,9 @@ export function transformCompilerOptions(
compilerOptions: {
...compilerOptions,
basePath: overrides?.basePath ? toPosixPath(overrides.basePath) : compilerOptions.basePath,
noEmit: false,
noEmitOnError: false,
emitDeclarationOnly: false,
composite: false,
inlineSources: !!overrides?.sourcemap,
inlineSourceMap: !isLibrary && !!overrides?.sourcemap,
Expand Down
Loading