Skip to content

Commit d15733d

Browse files
barbados-clemensFrozenPandaz
authored andcommitted
fix(vite): add missing types for tsconfigs (#15260)
(cherry picked from commit 2e449da)
1 parent eba45a9 commit d15733d

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

Diff for: e2e/react/src/react-package.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
checkFilesDoNotExist,
33
checkFilesExist,
4+
cleanupProject,
45
getSize,
56
killPorts,
67
newProject,
@@ -107,7 +108,7 @@ describe('Build React libraries and apps', () => {
107108

108109
afterEach(() => {
109110
killPorts();
110-
// cleanupProject();
111+
cleanupProject();
111112
});
112113

113114
describe('Buildable libraries', () => {

Diff for: packages/react/src/generators/application/application.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ describe('app', () => {
4444
expect(projects.get('my-app-e2e').root).toEqual('apps/my-app-e2e');
4545
});
4646

47+
it('should add vite types to tsconfigs', async () => {
48+
await applicationGenerator(appTree, {
49+
...schema,
50+
bundler: 'vite',
51+
unitTestRunner: 'vitest',
52+
});
53+
const tsconfigApp = readJson(appTree, 'apps/my-app/tsconfig.app.json');
54+
expect(tsconfigApp.compilerOptions.types).toEqual([
55+
'node',
56+
'vite/client',
57+
]);
58+
const tsconfigSpec = readJson(appTree, 'apps/my-app/tsconfig.spec.json');
59+
expect(tsconfigSpec.compilerOptions.types).toEqual([
60+
'vitest/globals',
61+
'vitest/importMeta',
62+
'vite/client',
63+
'node',
64+
]);
65+
});
66+
4767
it('should not overwrite default project if already set', async () => {
4868
const nxJson = readNxJson(appTree);
4969
nxJson.defaultProject = 'some-awesome-project';

Diff for: packages/react/src/generators/library/library.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ describe('lib', () => {
6161
});
6262
});
6363

64+
it('should add vite types to tsconfigs', async () => {
65+
await libraryGenerator(tree, {
66+
...defaultSchema,
67+
bundler: 'vite',
68+
unitTestRunner: 'vitest',
69+
});
70+
const tsconfigApp = readJson(tree, 'libs/my-lib/tsconfig.lib.json');
71+
expect(tsconfigApp.compilerOptions.types).toEqual([
72+
'node',
73+
'vite/client',
74+
]);
75+
const tsconfigSpec = readJson(tree, 'libs/my-lib/tsconfig.spec.json');
76+
expect(tsconfigSpec.compilerOptions.types).toEqual([
77+
'vitest/globals',
78+
'vitest/importMeta',
79+
'vite/client',
80+
'node',
81+
]);
82+
});
83+
6484
it('should update tags', async () => {
6585
await libraryGenerator(tree, { ...defaultSchema, tags: 'one,two' });
6686
const project = readProjectConfiguration(tree, 'my-lib');

Diff for: packages/react/src/utils/create-ts-config.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Tree } from 'nx/src/generators/tree';
22
import * as shared from '@nrwl/js/src/utils/typescript/create-ts-config';
3-
import { writeJson } from 'nx/src/generators/utils/json';
3+
import { updateJson, writeJson } from 'nx/src/generators/utils/json';
44

55
export function createTsConfig(
66
host: Tree,
@@ -56,6 +56,21 @@ export function createTsConfig(
5656
}
5757

5858
writeJson(host, `${projectRoot}/tsconfig.json`, json);
59+
60+
const tsconfigProjectPath = `${projectRoot}/tsconfig.${type}.json`;
61+
if (options.bundler === 'vite' && host.exists(tsconfigProjectPath)) {
62+
updateJson(host, tsconfigProjectPath, (json) => {
63+
json.compilerOptions ??= {};
64+
65+
const types = new Set(json.compilerOptions.types ?? []);
66+
types.add('node');
67+
types.add('vite/client');
68+
69+
json.compilerOptions.types = Array.from(types);
70+
71+
return json;
72+
});
73+
}
5974
}
6075

6176
export function extractTsConfigBase(host: Tree) {

Diff for: packages/vite/src/generators/vitest/files/tsconfig.spec.json__tmpl__

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"outDir": "../../dist/out-tsc",
5-
"types": ["vitest/globals", "node"]
5+
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
66
},
77
"include": [
88
"vite.config.ts",

Diff for: packages/vite/src/generators/vitest/vitest.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe('vitest generator', () => {
9393
"outDir": "../../dist/out-tsc",
9494
"types": Array [
9595
"vitest/globals",
96+
"vitest/importMeta",
97+
"vite/client",
9698
"node",
9799
],
98100
},

0 commit comments

Comments
 (0)