UNPKG

@nestjs/schematics

Version:

The generators behind the Nest CLI: scaffold NestJS applications, libraries, modules, controllers, services and CRUD resources.

92 lines (91 loc) • 5.89 kB
import { getPackageJsonDependency } from '../../../utils/dependencies.utils.js'; import { readJsonFile } from '../upgrade.utils.js'; const NODENEXT_SNIPPET = `{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext", "resolvePackageJsonExports": true, "target": "ES2023" } }`; const GLOBAL_TYPES = { node: '"process" (error TS2591)', jest: '"describe" (error TS2593)', }; const DEPRECATED_OPTIONS = [ ['target', (value) => String(value).toLowerCase() === 'es5'], ['downlevelIteration', () => true], ['outFile', () => true], ['esModuleInterop', (value) => value === false], ['allowSyntheticDefaultImports', (value) => value === false], ['alwaysStrict', (value) => value === false], ]; export function checkTsConfig(report) { return (tree) => { const tsconfig = readJsonFile(tree, 'tsconfig.json'); const compilerOptions = tsconfig?.compilerOptions; if (!compilerOptions || typeof compilerOptions !== 'object') { return tree; } const module = String(compilerOptions.module ?? '').toLowerCase(); const moduleResolution = String(compilerOptions.moduleResolution ?? '').toLowerCase(); const legacyResolution = moduleResolution === '' || moduleResolution === 'node' || moduleResolution === 'node10' || moduleResolution === 'classic'; if (module === 'commonjs' && legacyResolution) { report.action('tsconfig.json uses "module": "commonjs" with a legacy module resolution, which cannot resolve the ESM-only NestJS 12 packages. ' + `Update "compilerOptions" as follows (this is what the v11 CLI generates; your emitted code stays CommonJS as long as package.json has no "type": "module"):\n${NODENEXT_SNIPPET}`); } else if (legacyResolution && moduleResolution !== '') { report.action(`tsconfig.json uses "moduleResolution": "${compilerOptions.moduleResolution}", which TypeScript 6 no longer supports and which cannot resolve the ESM-only NestJS 12 packages. Switch to "nodenext" (or "bundler").`); } const buildConfig = readJsonFile(tree, 'tsconfig.build.json'); if (buildConfig) { const buildCompilerOptions = buildConfig.compilerOptions; const hasRootDir = (buildCompilerOptions && typeof buildCompilerOptions === 'object' && 'rootDir' in buildCompilerOptions) || String(compilerOptions.rootDir ?? '').replace(/^\.\/|\/$/g, '') === 'src'; if (!hasRootDir) { report.action('tsconfig.build.json does not set "rootDir". TypeScript 6 requires it explicitly (error TS5011); add `"rootDir": "./src"` to its "compilerOptions" to keep the dist layout unchanged.'); } } if (!('rootDir' in compilerOptions) && getPackageJsonDependency(tree, 'ts-jest')) { const build = buildConfig ? ' (tsconfig.build.json keeps "./src" for the build)' : ''; report.action(`tsconfig.json does not set "rootDir". ts-jest compiles the specs with it, and TypeScript 6 fails every suite with error TS5011; add \`"rootDir": "."\` to its "compilerOptions"${build}.`); } if ('baseUrl' in compilerOptions && compilerOptions.ignoreDeprecations !== '6.0') { const baseUrl = String(compilerOptions.baseUrl); const fix = ['.', './'].includes(baseUrl) ? 'Remove it, prefix any "paths" targets with "./" and map imports that rely on it (such as "src/app.service") with a `"src/*": ["./src/*"]` entry in "paths".' : 'Rewrite "paths" targets relative to tsconfig.json (with a leading "./") and remove it.'; report.action(`tsconfig.json sets "baseUrl": "${baseUrl}", which TypeScript 6 rejects (error TS5101). ${fix} Or add \`"ignoreDeprecations": "6.0"\` to keep it until TypeScript 7.`); } const types = Object.keys(GLOBAL_TYPES).filter((name) => getPackageJsonDependency(tree, `@types/${name}`)); if (types.length > 0 && !('types' in compilerOptions)) { report.action(`tsconfig.json does not set "types", which TypeScript 6 defaults to [], so globals such as ${types.map((name) => GLOBAL_TYPES[name]).join(' and ')} are not found. Add \`"types": [${types.map((name) => `"${name}"`).join(', ')}]\` to its "compilerOptions".`); } if (!('strict' in compilerOptions)) { report.action('tsconfig.json does not set "strict", which TypeScript 6 enables by default, so code that compiled before can fail with errors such as TS2564 (properties without an initializer, e.g. in DTO classes) and TS18046 ("catch" variables are "unknown"). ' + 'Add `"strict": false` to its "compilerOptions" to keep the v11 behaviour, or adopt it with `"strictPropertyInitialization": false` like new v12 projects.'); } const deprecated = DEPRECATED_OPTIONS.filter(([name, isDeprecated]) => name in compilerOptions && isDeprecated(compilerOptions[name])).map(([name]) => name); if (deprecated.length > 0 && compilerOptions.ignoreDeprecations !== '6.0') { const options = deprecated .map((name) => `"${name}": ${JSON.stringify(compilerOptions[name])}`) .join(', '); const pronoun = deprecated.length > 1 ? 'them' : 'it'; const target = deprecated.includes('target') ? ' (set "target" to "ES2023" like new projects)' : ''; report.action(`tsconfig.json sets ${options}, which TypeScript 6 deprecates (errors TS5101/TS5107). Remove ${pronoun}${target}, or add \`"ignoreDeprecations": "6.0"\` to keep ${pronoun} until TypeScript 7.`); } return tree; }; }