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

- Remove TS project reference to non-existent `tsconfig.editor.json`
file
- Ensure the `tsconfig.spec.json` file for Jest has the correct
`module`/`moduleResolution` compiler options
This commit is contained in:
Leosvel Pérez Espinosa 2025-05-29 14:33:19 +02:00 committed by GitHub
parent e0ea5be3f6
commit fc0aeb5a4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 61 additions and 14 deletions

View File

@ -599,9 +599,6 @@ exports[`app --strict should enable strict type checking: app tsconfig.json 1`]
"files": [], "files": [],
"include": [], "include": [],
"references": [ "references": [
{
"path": "./tsconfig.editor.json",
},
{ {
"path": "./tsconfig.app.json", "path": "./tsconfig.app.json",
}, },
@ -1162,9 +1159,6 @@ exports[`app not nested should generate files: tsconfig.json 1`] = `
"files": [], "files": [],
"include": [], "include": [],
"references": [ "references": [
{
"path": "./tsconfig.editor.json",
},
{ {
"path": "./tsconfig.app.json", "path": "./tsconfig.app.json",
}, },

View File

@ -9,7 +9,6 @@ import {
Tree, Tree,
updateJson, updateJson,
updateNxJson, updateNxJson,
updateProjectConfiguration,
} from '@nx/devkit'; } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as enquirer from 'enquirer'; import * as enquirer from 'enquirer';

View File

@ -6,10 +6,10 @@
}, },
"files": [], "files": [],
"include": [], "include": [],
"references": [ "references": [<% if (angularMajorVersion < 20) { %>
{ {
"path": "./tsconfig.editor.json" "path": "./tsconfig.editor.json"
}, },<% } %>
{ {
"path": "./tsconfig.app.json" "path": "./tsconfig.app.json"
} }

View File

@ -7,8 +7,8 @@ import {
} from '@nx/devkit'; } from '@nx/devkit';
import { getRootTsConfigFileName } from '@nx/js'; import { getRootTsConfigFileName } from '@nx/js';
import { getNeededCompilerOptionOverrides } from '@nx/js/src/utils/typescript/configuration'; 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 { 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 { updateAppEditorTsConfigExcludedFiles } from '../../utils/update-app-editor-tsconfig-excluded-files';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils'; import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
import { enableStrictTypeChecking } from './enable-strict-type-checking'; import { enableStrictTypeChecking } from './enable-strict-type-checking';
@ -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 = {
...json.compilerOptions, ...json.compilerOptions,
...compilerOptions, ...compilerOptions,
@ -71,9 +75,37 @@ export function updateTsconfigFiles(tree: Tree, options: NormalizedSchema) {
); );
return json; 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) { function updateEditorTsConfig(tree: Tree, options: NormalizedSchema) {
const tsconfigEditorPath = joinPathFragments(
options.appProjectRoot,
'tsconfig.editor.json'
);
if (!tree.exists(tsconfigEditorPath)) {
return;
}
const appTsConfig = readJson<TsConfig>( const appTsConfig = readJson<TsConfig>(
tree, tree,
joinPathFragments(options.appProjectRoot, 'tsconfig.app.json') joinPathFragments(options.appProjectRoot, 'tsconfig.app.json')

View File

@ -27,6 +27,7 @@ export function updateTsConfigFiles(
experimentalDecorators: true, experimentalDecorators: true,
importHelpers: true, importHelpers: true,
target: 'es2022', target: 'es2022',
moduleResolution: 'bundler',
...(options.strict ...(options.strict
? { ? {
strict: true, strict: true,
@ -46,11 +47,11 @@ export function updateTsConfigFiles(
if (angularMajorVersion >= 20) { if (angularMajorVersion >= 20) {
compilerOptions.module = 'preserve'; compilerOptions.module = 'preserve';
} else { } else {
compilerOptions.moduleResolution = 'bundler';
compilerOptions.module = 'es2022'; 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 = {
...json.compilerOptions, ...json.compilerOptions,
...compilerOptions, ...compilerOptions,
@ -73,6 +74,26 @@ export function updateTsConfigFiles(
return json; 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( function updateProjectConfig(

View File

@ -316,6 +316,7 @@ describe('lib', () => {
experimentalDecorators: true, experimentalDecorators: true,
importHelpers: true, importHelpers: true,
module: 'preserve', module: 'preserve',
moduleResolution: 'bundler',
skipLibCheck: true, skipLibCheck: true,
noFallthroughCasesInSwitch: true, noFallthroughCasesInSwitch: true,
noPropertyAccessFromIndexSignature: true, noPropertyAccessFromIndexSignature: true,

View File

@ -72,8 +72,8 @@ export async function libraryGenerator(
const project = await addProject(tree, libraryOptions); const project = await addProject(tree, libraryOptions);
createFiles(tree, options, project); createFiles(tree, options, project);
updateTsConfigFiles(tree, libraryOptions);
await addUnitTestRunner(tree, libraryOptions); await addUnitTestRunner(tree, libraryOptions);
updateTsConfigFiles(tree, libraryOptions);
updateNpmScopeIfBuildableOrPublishable(tree, libraryOptions); updateNpmScopeIfBuildableOrPublishable(tree, libraryOptions);
setGeneratorDefaults(tree, options); setGeneratorDefaults(tree, options);