fix(vite): add missing types for tsconfigs (#15260)

This commit is contained in:
Caleb Ukle 2023-03-02 13:25:57 -06:00 committed by GitHub
parent bf08714ad5
commit 2e449da177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import { import {
checkFilesDoNotExist, checkFilesDoNotExist,
checkFilesExist, checkFilesExist,
cleanupProject,
getSize, getSize,
killPorts, killPorts,
newProject, newProject,
@ -107,7 +108,7 @@ describe('Build React libraries and apps', () => {
afterEach(() => { afterEach(() => {
killPorts(); killPorts();
// cleanupProject(); cleanupProject();
}); });
describe('Buildable libraries', () => { describe('Buildable libraries', () => {

View File

@ -44,6 +44,26 @@ describe('app', () => {
expect(projects.get('my-app-e2e').root).toEqual('apps/my-app-e2e'); expect(projects.get('my-app-e2e').root).toEqual('apps/my-app-e2e');
}); });
it('should add vite types to tsconfigs', async () => {
await applicationGenerator(appTree, {
...schema,
bundler: 'vite',
unitTestRunner: 'vitest',
});
const tsconfigApp = readJson(appTree, 'apps/my-app/tsconfig.app.json');
expect(tsconfigApp.compilerOptions.types).toEqual([
'node',
'vite/client',
]);
const tsconfigSpec = readJson(appTree, 'apps/my-app/tsconfig.spec.json');
expect(tsconfigSpec.compilerOptions.types).toEqual([
'vitest/globals',
'vitest/importMeta',
'vite/client',
'node',
]);
});
it('should not overwrite default project if already set', async () => { it('should not overwrite default project if already set', async () => {
const nxJson = readNxJson(appTree); const nxJson = readNxJson(appTree);
nxJson.defaultProject = 'some-awesome-project'; nxJson.defaultProject = 'some-awesome-project';

View File

@ -61,6 +61,26 @@ describe('lib', () => {
}); });
}); });
it('should add vite types to tsconfigs', async () => {
await libraryGenerator(tree, {
...defaultSchema,
bundler: 'vite',
unitTestRunner: 'vitest',
});
const tsconfigApp = readJson(tree, 'libs/my-lib/tsconfig.lib.json');
expect(tsconfigApp.compilerOptions.types).toEqual([
'node',
'vite/client',
]);
const tsconfigSpec = readJson(tree, 'libs/my-lib/tsconfig.spec.json');
expect(tsconfigSpec.compilerOptions.types).toEqual([
'vitest/globals',
'vitest/importMeta',
'vite/client',
'node',
]);
});
it('should update tags', async () => { it('should update tags', async () => {
await libraryGenerator(tree, { ...defaultSchema, tags: 'one,two' }); await libraryGenerator(tree, { ...defaultSchema, tags: 'one,two' });
const project = readProjectConfiguration(tree, 'my-lib'); const project = readProjectConfiguration(tree, 'my-lib');

View File

@ -1,6 +1,6 @@
import { Tree } from 'nx/src/generators/tree'; import { Tree } from 'nx/src/generators/tree';
import * as shared from '@nrwl/js/src/utils/typescript/create-ts-config'; import * as shared from '@nrwl/js/src/utils/typescript/create-ts-config';
import { writeJson } from 'nx/src/generators/utils/json'; import { updateJson, writeJson } from 'nx/src/generators/utils/json';
export function createTsConfig( export function createTsConfig(
host: Tree, host: Tree,
@ -56,6 +56,21 @@ export function createTsConfig(
} }
writeJson(host, `${projectRoot}/tsconfig.json`, json); writeJson(host, `${projectRoot}/tsconfig.json`, json);
const tsconfigProjectPath = `${projectRoot}/tsconfig.${type}.json`;
if (options.bundler === 'vite' && host.exists(tsconfigProjectPath)) {
updateJson(host, tsconfigProjectPath, (json) => {
json.compilerOptions ??= {};
const types = new Set(json.compilerOptions.types ?? []);
types.add('node');
types.add('vite/client');
json.compilerOptions.types = Array.from(types);
return json;
});
}
} }
export function extractTsConfigBase(host: Tree) { export function extractTsConfigBase(host: Tree) {

View File

@ -2,7 +2,7 @@
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../../dist/out-tsc", "outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "node"] "types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
}, },
"include": [ "include": [
"vite.config.ts", "vite.config.ts",

View File

@ -93,6 +93,8 @@ describe('vitest generator', () => {
"outDir": "../../dist/out-tsc", "outDir": "../../dist/out-tsc",
"types": Array [ "types": Array [
"vitest/globals", "vitest/globals",
"vitest/importMeta",
"vite/client",
"node", "node",
], ],
}, },