Skip to content

Commit

Permalink
fix(vite): add typecheck inferred target for vite plugin #27501
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Aug 20, 2024
1 parent 186e4c7 commit 86ddc30
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,20 @@ describe('Vite - Convert Executors To Plugin', () => {
},
"plugin": "@nx/vite/plugin",
},
{
"include": [
"existing/**/*",
],
"options": {
"buildTargetName": "build",
"previewTargetName": "preview",
"serveStaticTargetName": "serve-static",
"serveTargetName": "serve",
"testTargetName": "test",
"typecheckTargetName": "typecheck",
},
"plugin": "@nx/vite/plugin",
},
{
"include": [
"myapp/**/*",
Expand All @@ -542,6 +556,7 @@ describe('Vite - Convert Executors To Plugin', () => {
"serveStaticTargetName": "serve-static",
"serveTargetName": "serve",
"testTargetName": "test",
"typecheckTargetName": "typecheck",
},
"plugin": "@nx/vite/plugin",
},
Expand All @@ -555,6 +570,7 @@ describe('Vite - Convert Executors To Plugin', () => {
"serveStaticTargetName": "serve-static",
"serveTargetName": "serve",
"testTargetName": "test",
"typecheckTargetName": "typecheck",
},
"plugin": "@nx/vite/plugin",
},
Expand Down
22 changes: 22 additions & 0 deletions packages/vite/src/plugins/__snapshots__/plugin-vitest.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = `
"{projectRoot}/coverage",
],
},
"typecheck": {
"cache": true,
"command": "tsc --noEmit",
"inputs": [
"production",
"^production",
],
"metadata": {
"description": "Run Typechecking",
"help": {
"command": "npx tsc --help",
"example": {
"options": {
"noEmit": true,
},
},
},
},
"options": {
"cwd": ".",
},
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ exports[`@nx/vite/plugin with test node root project should create nodes - with
"{projectRoot}/coverage",
],
},
"typecheck": {
"cache": true,
"command": "tsc --noEmit",
"inputs": [
"production",
"^production",
],
"metadata": {
"description": "Run Typechecking",
"help": {
"command": "npx tsc --help",
"example": {
"options": {
"noEmit": true,
},
},
},
},
"options": {
"cwd": ".",
},
},
},
},
},
Expand Down
31 changes: 31 additions & 0 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { getLockFileName } from '@nx/js';
import { loadViteDynamicImport } from '../utils/executor-utils';
import { hashObject } from 'nx/src/hasher/file-hasher';
import { minimatch } from 'minimatch';

const pmc = getPackageManagerCommand();

Expand All @@ -30,6 +31,7 @@ export interface VitePluginOptions {
serveTargetName?: string;
previewTargetName?: string;
serveStaticTargetName?: string;
typecheckTargetName?: string;
}

type ViteTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;
Expand Down Expand Up @@ -97,6 +99,9 @@ async function createNodesInternal(
return {};
}

const tsConfigFiles =
siblingFiles.filter((p) => minimatch(p, 'tsconfig*{.json,.*.json}')) ?? [];

const normalizedOptions = normalizeOptions(options);

// We do not want to alter how the hash is calculated, so appending the config file path to the hash
Expand All @@ -113,6 +118,7 @@ async function createNodesInternal(
configFilePath,
projectRoot,
normalizedOptions,
tsConfigFiles,
context
);
targetsCache[hash] ??= viteTargets;
Expand Down Expand Up @@ -141,6 +147,7 @@ async function buildViteTargets(
configFilePath: string,
projectRoot: string,
options: VitePluginOptions,
tsConfigFiles: string[],
context: CreateNodesContext
): Promise<ViteTargets & { isLibrary: boolean }> {
const absoluteConfigFilePath = joinPathFragments(
Expand Down Expand Up @@ -198,6 +205,29 @@ async function buildViteTargets(
}
}

if (tsConfigFiles.length) {
const tsconfigToUse = tsConfigFiles.includes('tsconfig.lib.json')
? 'tsconfig.lib.json'
: tsConfigFiles[0];
targets[options.typecheckTargetName] = {
cache: true,
inputs: ['production', '^production'],
command: 'tsc --noEmit',
options: { cwd: joinPathFragments(projectRoot) },
metadata: {
description: `Run Typechecking`,
help: {
command: `${pmc.exec} tsc --help -p ${tsconfigToUse}`,
example: {
options: {
noEmit: true,
},
},
},
},
};
}

// if file is vitest.config or vite.config has definition for test, create target for test
if (configFilePath.includes('vitest.config') || hasTest) {
targets[options.testTargetName] = await testTarget(
Expand Down Expand Up @@ -420,5 +450,6 @@ function normalizeOptions(options: VitePluginOptions): VitePluginOptions {
options.previewTargetName ??= 'preview';
options.testTargetName ??= 'test';
options.serveStaticTargetName ??= 'serve-static';
options.typecheckTargetName ??= 'typecheck';
return options;
}

0 comments on commit 86ddc30

Please sign in to comment.