Skip to content

Commit

Permalink
fix(vite): add typecheck inferred target for vite plugin #27501 (#27531)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Vite intentionally does not run typechecking on projects. They recommend
running `tsc --noEmit` separately.
The `@nx/vite/plugin` could make this easier by adding a `typecheck`
when it detects a `tsconfig` file in the `projectRoot`.
This can then be added to the build pipeline on CI. Or users can add it
to a `dependsOn` for the `build` target if they wish.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The `@nx/vite/plugin` could make this easier by adding a `typecheck`
when it detects a `tsconfig` file in the `projectRoot`.
This can then be added to the build pipeline on CI. Or users can add it
to a `dependsOn` for the `build` target if they wish.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27501

(cherry picked from commit 6d963fd)
  • Loading branch information
Coly010 authored and FrozenPandaz committed Aug 21, 2024
1 parent 5441638 commit ceff541
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@swc/helpers": "~0.5.0",
"enquirer": "~2.3.6",
"@nx/js": "file:../js",
"tsconfig-paths": "^4.1.2"
"tsconfig-paths": "^4.1.2",
"minimatch": "9.0.3"
},
"peerDependencies": {
"vite": "^5.0.0",
Expand Down
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 -p tsconfig.lib.json",
"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 -p tsconfig.lib.json",
"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 ceff541

Please sign in to comment.