Skip to content

Commit ea2c772

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(js): remove redundant vite.config.ts generation for vitest projects (#34603)
## 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: ``` vite.config.ts:5:9 - error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output. 5 root: import.meta.dirname, ~~~~~~~~~~~ ``` ## Expected Behavior Only `vitest.config.mts` should be generated. The vitest `configurationGenerator` already handles creating the correct config file with `root: __dirname`. The second `createOrEditViteConfig` call from `@nx/vite` was redundant and produced the conflicting file. ## Related Issue(s) Fixes #34399 (cherry picked from commit 1e3f8e0)
1 parent 63d6652 commit ea2c772

3 files changed

Lines changed: 9 additions & 17 deletions

File tree

packages/js/src/generators/library/library.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,28 +1773,30 @@ describe('lib', () => {
17731773
});
17741774

17751775
describe('--testEnvironment', () => {
1776-
it('should generate a vite config with testEnvironment set to node', async () => {
1776+
it('should generate a vitest config with testEnvironment set to node', async () => {
17771777
await libraryGenerator(tree, {
17781778
...defaultOptions,
17791779
directory: 'my-node-lib',
17801780
unitTestRunner: 'vitest',
17811781
testEnvironment: 'node',
17821782
});
17831783

1784-
const content = tree.read('my-node-lib/vite.config.ts', 'utf-8');
1784+
expect(tree.exists('my-node-lib/vite.config.ts')).toBe(false);
1785+
const content = tree.read('my-node-lib/vitest.config.mts', 'utf-8');
17851786

17861787
expect(content).toContain(`environment: 'node'`);
17871788
});
17881789

1789-
it('should generate a vite config with testEnvironment set to jsdom by default', async () => {
1790+
it('should generate a vitest config with testEnvironment set to jsdom by default', async () => {
17901791
await libraryGenerator(tree, {
17911792
...defaultOptions,
17921793
directory: 'my-jsdom-lib',
17931794
unitTestRunner: 'vitest',
17941795
testEnvironment: undefined,
17951796
});
17961797

1797-
const content = tree.read('my-jsdom-lib/vite.config.ts', 'utf-8');
1798+
expect(tree.exists('my-jsdom-lib/vite.config.ts')).toBe(false);
1799+
const content = tree.read('my-jsdom-lib/vitest.config.mts', 'utf-8');
17981800

17991801
expect(content).toContain(`environment: 'jsdom'`);
18001802
});

packages/js/src/generators/library/library.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ export async function libraryGeneratorInternal(
173173
options.unitTestRunner === 'vitest' &&
174174
options.bundler !== 'vite' // Test would have been set up already
175175
) {
176-
const { createOrEditViteConfig } = ensurePackage('@nx/vite', nxVersion);
177176
ensurePackage('@nx/vitest', nxVersion);
178177
// nx-ignore-next-line
179178
const { configurationGenerator } = require('@nx/vitest/generators');
@@ -188,16 +187,6 @@ export async function libraryGeneratorInternal(
188187
addPlugin: options.addPlugin,
189188
});
190189
tasks.push(vitestTask);
191-
createOrEditViteConfig(
192-
tree,
193-
{
194-
project: options.name,
195-
includeLib: false,
196-
includeVitest: true,
197-
testEnvironment: options.testEnvironment,
198-
},
199-
true
200-
);
201190
}
202191

203192
if (!schema.skipTsConfig && !options.isUsingTsSolutionConfig) {

packages/plugin/src/generators/plugin/plugin.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('NxPlugin Plugin Generator', () => {
236236
});
237237

238238
describe('vitest', () => {
239-
it('should generate test files with vite.config.ts', async () => {
239+
it('should generate test files with vitest.config.mts', async () => {
240240
await pluginGenerator(
241241
tree,
242242
getSchema({
@@ -245,7 +245,8 @@ describe('NxPlugin Plugin Generator', () => {
245245
})
246246
);
247247

248-
['my-plugin/vite.config.ts'].forEach((path) =>
248+
expect(tree.exists('my-plugin/vite.config.ts')).toBeFalsy();
249+
['my-plugin/vitest.config.mts'].forEach((path) =>
249250
expect(tree.exists(path)).toBeTruthy()
250251
);
251252

0 commit comments

Comments
 (0)