diff --git a/docs/generated/packages/js/generators/init.json b/docs/generated/packages/js/generators/init.json index 4de61e9142090..940d6fdb5bc32 100644 --- a/docs/generated/packages/js/generators/init.json +++ b/docs/generated/packages/js/generators/init.json @@ -19,6 +19,11 @@ "description": "Skip formatting files.", "default": true, "x-priority": "internal" + }, + "tsConfigName": { + "type": "string", + "description": "Customize the generated tsconfig file name.", + "x-priority": "internal" } }, "presets": [] diff --git a/e2e/workspace-create/src/create-nx-workspace.test.ts b/e2e/workspace-create/src/create-nx-workspace.test.ts index df0c805f00c62..f837062a7a1a5 100644 --- a/e2e/workspace-create/src/create-nx-workspace.test.ts +++ b/e2e/workspace-create/src/create-nx-workspace.test.ts @@ -32,6 +32,7 @@ describe('create-nx-workspace', () => { checkFilesExist('package.json'); checkFilesExist('src/app/app.routes.ts'); + checkFilesDoNotExist('tsconfig.base.json'); checkFilesExist('project.json'); }); @@ -83,6 +84,7 @@ describe('create-nx-workspace', () => { checkFilesExist('package.json'); checkFilesExist('project.json'); checkFilesExist('vite.config.ts'); + checkFilesDoNotExist('tsconfig.base.json'); }); it('should create a workspace with a single react app with webpack at the root', () => { @@ -99,6 +101,7 @@ describe('create-nx-workspace', () => { checkFilesExist('package.json'); checkFilesExist('project.json'); checkFilesExist('webpack.config.js'); + checkFilesDoNotExist('tsconfig.base.json'); }); it('should be able to create an empty workspace built for apps', () => { diff --git a/packages/angular/src/generators/init/angular-v14/init.ts b/packages/angular/src/generators/init/angular-v14/init.ts index 0eb7f707c4bd3..7c56689b7e990 100755 --- a/packages/angular/src/generators/init/angular-v14/init.ts +++ b/packages/angular/src/generators/init/angular-v14/init.ts @@ -28,6 +28,8 @@ export async function angularInitGenerator( const options = normalizeOptions(rawOptions); setDefaults(host, options); await jsInitGenerator(host, { + ...options, + tsConfigName: options.rootProject ? 'tsconfig.json' : 'tsconfig.base.json', js: false, skipFormat: true, }); @@ -78,6 +80,7 @@ function normalizeOptions(options: Schema): Required { skipPackageJson: options.skipPackageJson ?? false, style: options.style ?? 'css', unitTestRunner: options.unitTestRunner ?? UnitTestRunner.Jest, + rootProject: options.rootProject, }; } diff --git a/packages/angular/src/generators/init/angular-v14/schema.d.ts b/packages/angular/src/generators/init/angular-v14/schema.d.ts index 8e50f0aa89a89..171b5d834e187 100644 --- a/packages/angular/src/generators/init/angular-v14/schema.d.ts +++ b/packages/angular/src/generators/init/angular-v14/schema.d.ts @@ -10,4 +10,5 @@ export interface Schema { style?: Styles; linter?: Linter; skipPackageJson?: boolean; + rootProject?: boolean; } diff --git a/packages/angular/src/generators/init/init.ts b/packages/angular/src/generators/init/init.ts index b7424f070eb3e..9fd2120d943a6 100755 --- a/packages/angular/src/generators/init/init.ts +++ b/packages/angular/src/generators/init/init.ts @@ -70,6 +70,8 @@ export async function angularInitGenerator( const options = normalizeOptions(rawOptions); setDefaults(tree, options); await jsInitGenerator(tree, { + ...options, + tsConfigName: options.rootProject ? 'tsconfig.json' : 'tsconfig.base.json', js: false, skipFormat: true, }); @@ -102,6 +104,7 @@ function normalizeOptions(options: Schema): Required { skipPackageJson: options.skipPackageJson ?? false, style: options.style ?? 'css', unitTestRunner: options.unitTestRunner ?? UnitTestRunner.Jest, + rootProject: options.rootProject, }; } diff --git a/packages/angular/src/generators/init/schema.d.ts b/packages/angular/src/generators/init/schema.d.ts index 8e50f0aa89a89..171b5d834e187 100644 --- a/packages/angular/src/generators/init/schema.d.ts +++ b/packages/angular/src/generators/init/schema.d.ts @@ -10,4 +10,5 @@ export interface Schema { style?: Styles; linter?: Linter; skipPackageJson?: boolean; + rootProject?: boolean; } diff --git a/packages/js/src/generators/init/files/tsconfig.base.json b/packages/js/src/generators/init/files/__fileName__ similarity index 100% rename from packages/js/src/generators/init/files/tsconfig.base.json rename to packages/js/src/generators/init/files/__fileName__ diff --git a/packages/js/src/generators/init/init.ts b/packages/js/src/generators/init/init.ts index b771a2d8adc32..4519d09071354 100644 --- a/packages/js/src/generators/init/init.ts +++ b/packages/js/src/generators/init/init.ts @@ -19,8 +19,10 @@ export async function initGenerator( } // add tsconfig.base.json - if (!getRootTsConfigFileName()) { - generateFiles(host, joinPathFragments(__dirname, './files'), '.', {}); + if (!getRootTsConfigFileName(host)) { + generateFiles(host, joinPathFragments(__dirname, './files'), '.', { + fileName: schema.tsConfigName ?? 'tsconfig.base.json', + }); } if (!schema.skipFormat) { diff --git a/packages/js/src/generators/init/schema.d.ts b/packages/js/src/generators/init/schema.d.ts index 6fef81d0b67dc..53f33ec06a152 100644 --- a/packages/js/src/generators/init/schema.d.ts +++ b/packages/js/src/generators/init/schema.d.ts @@ -1,4 +1,5 @@ export interface InitSchema { js?: boolean; skipFormat?: boolean; + tsConfigName?: string; } diff --git a/packages/js/src/generators/init/schema.json b/packages/js/src/generators/init/schema.json index 3b4fe1cba57ae..ca257c4c2d15c 100644 --- a/packages/js/src/generators/init/schema.json +++ b/packages/js/src/generators/init/schema.json @@ -16,6 +16,11 @@ "description": "Skip formatting files.", "default": true, "x-priority": "internal" + }, + "tsConfigName": { + "type": "string", + "description": "Customize the generated tsconfig file name.", + "x-priority": "internal" } } } diff --git a/packages/js/src/utils/typescript/ts-config.ts b/packages/js/src/utils/typescript/ts-config.ts index a8b3fc4f75fdc..e52f146a3a210 100644 --- a/packages/js/src/utils/typescript/ts-config.ts +++ b/packages/js/src/utils/typescript/ts-config.ts @@ -42,10 +42,9 @@ export function getRelativePathToRootTsConfig( return offsetFromRoot(targetPath) + getRootTsConfigPathInTree(tree); } -export function getRootTsConfigFileName(): string | null { +export function getRootTsConfigFileName(tree: Tree): string | null { for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) { - const tsConfigPath = join(workspaceRoot, tsConfigName); - if (existsSync(tsConfigPath)) { + if (tree.exists(tsConfigName)) { return tsConfigName; } } @@ -53,12 +52,6 @@ export function getRootTsConfigFileName(): string | null { return null; } -export function getRootTsConfigPath(): string | null { - const tsConfigFileName = getRootTsConfigFileName(); - - return tsConfigFileName ? join(workspaceRoot, tsConfigFileName) : null; -} - export function updateRootTsConfig( host: Tree, options: { diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index 79422b4676e08..5ed81aafc8743 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -335,7 +335,7 @@ export async function applicationGenerator(tree: Tree, schema: Schema) { const tasks: GeneratorCallback[] = []; const initTask = await initGenerator(tree, { - ...options, + ...schema, skipFormat: true, }); tasks.push(initTask); diff --git a/packages/node/src/generators/init/init.ts b/packages/node/src/generators/init/init.ts index 0147cb4124b79..e0d01515404f2 100644 --- a/packages/node/src/generators/init/init.ts +++ b/packages/node/src/generators/init/init.ts @@ -38,6 +38,8 @@ function normalizeOptions(schema: Schema) { export async function initGenerator(tree: Tree, schema: Schema) { const options = normalizeOptions(schema); await jsInitGenerator(tree, { + ...schema, + tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json', js: schema.js, skipFormat: true, }); diff --git a/packages/node/src/generators/init/schema.d.ts b/packages/node/src/generators/init/schema.d.ts index 2216c6dbc28be..ef7515a08cbfa 100644 --- a/packages/node/src/generators/init/schema.d.ts +++ b/packages/node/src/generators/init/schema.d.ts @@ -2,4 +2,5 @@ export interface Schema { unitTestRunner?: 'jest' | 'none'; skipFormat?: boolean; js?: boolean; + rootProject?: boolean; } diff --git a/packages/react/src/generators/init/init.ts b/packages/react/src/generators/init/init.ts index 9675642a6fa97..c0af386ef17d9 100755 --- a/packages/react/src/generators/init/init.ts +++ b/packages/react/src/generators/init/init.ts @@ -87,6 +87,8 @@ function initRootBabelConfig(tree: Tree, schema: InitSchema) { export async function reactInitGenerator(host: Tree, schema: InitSchema) { await jsInitGenerator(host, { + ...schema, + tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json', js: schema.js, skipFormat: true, }); diff --git a/packages/react/src/generators/init/schema.d.ts b/packages/react/src/generators/init/schema.d.ts index 1d339905bcb6a..b89e5ad49986f 100644 --- a/packages/react/src/generators/init/schema.d.ts +++ b/packages/react/src/generators/init/schema.d.ts @@ -6,4 +6,6 @@ export interface InitSchema { skipPackageJson?: boolean; skipHelperLibs?: boolean; js?: boolean; + + rootProject?: boolean; }