Skip to content

fix(angular): update tsconfig files generation to better support angular v20 #31357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,6 @@ exports[`app --strict should enable strict type checking: app tsconfig.json 1`]
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.editor.json",
},
{
"path": "./tsconfig.app.json",
},
Expand Down Expand Up @@ -1162,9 +1159,6 @@ exports[`app not nested should generate files: tsconfig.json 1`] = `
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.editor.json",
},
{
"path": "./tsconfig.app.json",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Tree,
updateJson,
updateNxJson,
updateProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as enquirer from 'enquirer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
},
"files": [],
"include": [],
"references": [
"references": [<% if (angularMajorVersion < 20) { %>
{
"path": "./tsconfig.editor.json"
},
},<% } %>
{
"path": "./tsconfig.app.json"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from '@nx/devkit';
import { getRootTsConfigFileName } from '@nx/js';
import { getNeededCompilerOptionOverrides } from '@nx/js/src/utils/typescript/configuration';
import { gte, lt } from 'semver';
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
import { gte, lt } from 'semver';
import { updateAppEditorTsConfigExcludedFiles } from '../../utils/update-app-editor-tsconfig-excluded-files';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
import { enableStrictTypeChecking } from './enable-strict-type-checking';
Expand Down Expand Up @@ -59,7 +59,11 @@ export function updateTsconfigFiles(tree: Tree, options: NormalizedSchema) {
}
}

updateJson(tree, `${options.appProjectRoot}/tsconfig.json`, (json) => {
const tsconfigPath = joinPathFragments(
options.appProjectRoot,
'tsconfig.json'
);
updateJson(tree, tsconfigPath, (json) => {
json.compilerOptions = {
...json.compilerOptions,
...compilerOptions,
Expand All @@ -71,9 +75,37 @@ export function updateTsconfigFiles(tree: Tree, options: NormalizedSchema) {
);
return json;
});

if (options.unitTestRunner === 'jest') {
const tsconfigSpecPath = joinPathFragments(
options.appProjectRoot,
'tsconfig.spec.json'
);
updateJson(tree, tsconfigSpecPath, (json) => {
json.compilerOptions = {
...json.compilerOptions,
module: 'commonjs',
moduleResolution: 'node10',
};
json.compilerOptions = getNeededCompilerOptionOverrides(
tree,
json.compilerOptions,
tsconfigPath
);
return json;
});
}
}

function updateEditorTsConfig(tree: Tree, options: NormalizedSchema) {
const tsconfigEditorPath = joinPathFragments(
options.appProjectRoot,
'tsconfig.editor.json'
);
if (!tree.exists(tsconfigEditorPath)) {
return;
}

const appTsConfig = readJson<TsConfig>(
tree,
joinPathFragments(options.appProjectRoot, 'tsconfig.app.json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function updateTsConfigFiles(
experimentalDecorators: true,
importHelpers: true,
target: 'es2022',
moduleResolution: 'bundler',
...(options.strict
? {
strict: true,
Expand All @@ -46,11 +47,11 @@ export function updateTsConfigFiles(
if (angularMajorVersion >= 20) {
compilerOptions.module = 'preserve';
} else {
compilerOptions.moduleResolution = 'bundler';
compilerOptions.module = 'es2022';
}

updateJson(tree, `${options.projectRoot}/tsconfig.json`, (json) => {
const tsconfigPath = joinPathFragments(options.projectRoot, 'tsconfig.json');
updateJson(tree, tsconfigPath, (json) => {
json.compilerOptions = {
...json.compilerOptions,
...compilerOptions,
Expand All @@ -73,6 +74,26 @@ export function updateTsConfigFiles(

return json;
});

if (options.unitTestRunner === 'jest') {
const tsconfigSpecPath = joinPathFragments(
options.projectRoot,
'tsconfig.spec.json'
);
updateJson(tree, tsconfigSpecPath, (json) => {
json.compilerOptions = {
...json.compilerOptions,
module: 'commonjs',
moduleResolution: 'node10',
};
json.compilerOptions = getNeededCompilerOptionOverrides(
tree,
json.compilerOptions,
tsconfigPath
);
return json;
});
}
}

function updateProjectConfig(
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ describe('lib', () => {
experimentalDecorators: true,
importHelpers: true,
module: 'preserve',
moduleResolution: 'bundler',
skipLibCheck: true,
noFallthroughCasesInSwitch: true,
noPropertyAccessFromIndexSignature: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export async function libraryGenerator(
const project = await addProject(tree, libraryOptions);

createFiles(tree, options, project);
updateTsConfigFiles(tree, libraryOptions);
await addUnitTestRunner(tree, libraryOptions);
updateTsConfigFiles(tree, libraryOptions);
updateNpmScopeIfBuildableOrPublishable(tree, libraryOptions);
setGeneratorDefaults(tree, options);

Expand Down
Loading