From 52d6c01f007ec6ca7f1f1753120fd274674465b8 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 25 Feb 2026 10:47:39 -0500 Subject: [PATCH] fix(js): remove redundant vite.config.ts generation for vitest projects ## Current Behavior When generating a library with vitest as the test runner and a non-vite bundler (e.g. tsc), the js library generator creates two config files: - `vitest.config.mts` (from the vitest configurationGenerator) with `root: __dirname` - `vite.config.ts` (from a second createOrEditViteConfig call) with `root: import.meta.dirname` The redundant `vite.config.ts` uses ESM-only `import.meta.dirname` syntax, which causes TS1470 when the project targets CommonJS output. ## Expected Behavior Only `vitest.config.mts` should be generated since the vitest configurationGenerator already handles creating the correct config file. ## Related Issue(s) Fixes #34399 --- packages/js/src/generators/library/library.spec.ts | 10 ++++++---- packages/js/src/generators/library/library.ts | 11 ----------- packages/plugin/src/generators/plugin/plugin.spec.ts | 5 +++-- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/packages/js/src/generators/library/library.spec.ts b/packages/js/src/generators/library/library.spec.ts index 5a80ea163db..4d36f82bf14 100644 --- a/packages/js/src/generators/library/library.spec.ts +++ b/packages/js/src/generators/library/library.spec.ts @@ -1773,7 +1773,7 @@ describe('lib', () => { }); describe('--testEnvironment', () => { - it('should generate a vite config with testEnvironment set to node', async () => { + it('should generate a vitest config with testEnvironment set to node', async () => { await libraryGenerator(tree, { ...defaultOptions, directory: 'my-node-lib', @@ -1781,12 +1781,13 @@ describe('lib', () => { testEnvironment: 'node', }); - const content = tree.read('my-node-lib/vite.config.ts', 'utf-8'); + expect(tree.exists('my-node-lib/vite.config.ts')).toBe(false); + const content = tree.read('my-node-lib/vitest.config.mts', 'utf-8'); expect(content).toContain(`environment: 'node'`); }); - it('should generate a vite config with testEnvironment set to jsdom by default', async () => { + it('should generate a vitest config with testEnvironment set to jsdom by default', async () => { await libraryGenerator(tree, { ...defaultOptions, directory: 'my-jsdom-lib', @@ -1794,7 +1795,8 @@ describe('lib', () => { testEnvironment: undefined, }); - const content = tree.read('my-jsdom-lib/vite.config.ts', 'utf-8'); + expect(tree.exists('my-jsdom-lib/vite.config.ts')).toBe(false); + const content = tree.read('my-jsdom-lib/vitest.config.mts', 'utf-8'); expect(content).toContain(`environment: 'jsdom'`); }); diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 8a80bcf8be8..cd50733912e 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -173,7 +173,6 @@ export async function libraryGeneratorInternal( options.unitTestRunner === 'vitest' && options.bundler !== 'vite' // Test would have been set up already ) { - const { createOrEditViteConfig } = ensurePackage('@nx/vite', nxVersion); ensurePackage('@nx/vitest', nxVersion); // nx-ignore-next-line const { configurationGenerator } = require('@nx/vitest/generators'); @@ -188,16 +187,6 @@ export async function libraryGeneratorInternal( addPlugin: options.addPlugin, }); tasks.push(vitestTask); - createOrEditViteConfig( - tree, - { - project: options.name, - includeLib: false, - includeVitest: true, - testEnvironment: options.testEnvironment, - }, - true - ); } if (!schema.skipTsConfig && !options.isUsingTsSolutionConfig) { diff --git a/packages/plugin/src/generators/plugin/plugin.spec.ts b/packages/plugin/src/generators/plugin/plugin.spec.ts index 49081ef1f9a..541ecfea440 100644 --- a/packages/plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/plugin/src/generators/plugin/plugin.spec.ts @@ -236,7 +236,7 @@ describe('NxPlugin Plugin Generator', () => { }); describe('vitest', () => { - it('should generate test files with vite.config.ts', async () => { + it('should generate test files with vitest.config.mts', async () => { await pluginGenerator( tree, getSchema({ @@ -245,7 +245,8 @@ describe('NxPlugin Plugin Generator', () => { }) ); - ['my-plugin/vite.config.ts'].forEach((path) => + expect(tree.exists('my-plugin/vite.config.ts')).toBeFalsy(); + ['my-plugin/vitest.config.mts'].forEach((path) => expect(tree.exists(path)).toBeTruthy() );