Skip to content

Commit f8d75f7

Browse files
wan9chiclaude
andauthored
feat: integrate with Vite Task for zero-config build caching (#22453)
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 9ce3036 commit f8d75f7

8 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎packages/vite/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
},
7474
"//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!",
7575
"dependencies": {
76+
"@voidzero-dev/vite-task-client": "^0.1.1",
7677
"lightningcss": "^1.32.0",
7778
"picomatch": "^4.0.4",
7879
"postcss": "^8.5.15",

‎packages/vite/src/node/config.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { inspect, promisify } from 'node:util'
66
import { performance } from 'node:perf_hooks'
77
import { createRequire } from 'node:module'
88
import crypto from 'node:crypto'
9+
import {
10+
getEnv,
11+
ignoreInput,
12+
ignoreOutput,
13+
} from '@voidzero-dev/vite-task-client'
914
import colors from 'picocolors'
1015
import picomatch from 'picomatch'
1116
import {
@@ -1419,6 +1424,14 @@ export async function resolveConfig(
14191424

14201425
let configFileDependencies: string[] = []
14211426
let mode = inlineConfig.mode || defaultMode
1427+
// When `NODE_ENV` isn't set locally, ask Vite Task for it; the runner
1428+
// also records the env in the build's cache key.
1429+
if (process.env.NODE_ENV === undefined) {
1430+
const nodeEnv = getEnv('NODE_ENV')
1431+
if (nodeEnv !== undefined) {
1432+
process.env.NODE_ENV = nodeEnv
1433+
}
1434+
}
14221435
const isNodeEnvSet = !!process.env.NODE_ENV
14231436
const packageCache: PackageCache = new Map()
14241437

@@ -2617,6 +2630,11 @@ async function loadConfigFromBundledFile(
26172630
`.vite-temp/${path.basename(fileName)}.${hash}.mjs`,
26182631
)
26192632
: `${fileName}.${hash}.mjs`
2633+
2634+
// Tell Vite Task to ignore this transient file as both input and output,
2635+
// so the read-write of this file doesn't affect the cache fingerprints.
2636+
ignoreInput(tempFileName)
2637+
ignoreOutput(tempFileName)
26202638
await fsp.writeFile(tempFileName, bundledCode)
26212639
try {
26222640
return (await import(pathToFileURL(tempFileName).href)).default

‎packages/vite/src/node/env.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- our supported nodejs range supports `parseEnv` but in experimental state, which is fine
44
import { parseEnv } from 'node:util'
5+
import { getEnvs } from '@voidzero-dev/vite-task-client'
56
import { type DotenvPopulateInput, expand } from 'dotenv-expand'
67
import colors from 'picocolors'
78
import { arraify, createDebugger, normalizePath, tryStatSync } from './utils'
@@ -82,6 +83,12 @@ export function loadEnv(
8283
}
8384
}
8485

86+
// Vite Task may know prefixed envs not present here; fetch them and let
87+
// the runner record each glob in the build's cache key.
88+
for (const prefix of prefixes) {
89+
Object.assign(env, getEnvs(`${prefix}*`))
90+
}
91+
8592
// check if there are actual env variables starting with VITE_*
8693
// these are typically provided inline and should be prioritized
8794
for (const key in process.env) {

‎packages/vite/src/node/optimizer/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fsp from 'node:fs/promises'
33
import path from 'node:path'
44
import { promisify } from 'node:util'
55
import { performance } from 'node:perf_hooks'
6+
import { ignoreInput, ignoreOutput } from '@voidzero-dev/vite-task-client'
67
import colors from 'picocolors'
78
import { init, parse } from 'es-module-lexer'
89
import { isDynamicPattern } from 'tinyglobby'
@@ -397,6 +398,14 @@ export async function loadCachedDepOptimizationMetadata(
397398

398399
const depsCacheDir = getDepsCacheDir(environment)
399400

401+
// When run inside Vite Task, the dep optimizer cache is both read and
402+
// written under this directory (metadata + pre-bundled deps). Tell Vite
403+
// Task to treat it as neither a build input nor a build output: the
404+
// lockfile hash stored in the metadata already drives re-optimization,
405+
// and the cache is process-local scratch space, not a build artifact.
406+
ignoreInput(depsCacheDir)
407+
ignoreOutput(depsCacheDir)
408+
400409
if (!force) {
401410
let cachedMetadata: DepOptimizationMetadata | undefined
402411
try {

‎packages/vite/src/node/plugins/prepareOutDir.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3+
import { ignoreInput } from '@voidzero-dev/vite-task-client'
34
import colors from 'picocolors'
45
import type { Plugin } from '../plugin'
56
import { getResolvedOutDirs, resolveEmptyOutDir } from '../watch'
@@ -51,6 +52,11 @@ function prepareOutDir(
5152
const { publicDir } = environment.config
5253
const outDirsArray = [...outDirs]
5354
for (const outDir of outDirs) {
55+
// When run inside Vite Task, `emptyDir` below reads the entries of
56+
// `outDir`. Without this, those reads would be recorded as build inputs
57+
// and mix with the writes that follow, tripping Vite Task's read-write
58+
// overlap check.
59+
ignoreInput(outDir)
5460
if (emptyOutDir !== false && fs.existsSync(outDir)) {
5561
// skip those other outDirs which are nested in current outDir
5662
const skipDirs = outDirsArray

‎packages/vite/src/node/preview.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import sirv from 'sirv'
44
import compression from '@polka/compression'
55
import connect from 'connect'
66
import corsMiddleware from 'cors'
7+
import { disableCache } from '@voidzero-dev/vite-task-client'
78
import type { Connect } from '#dep-types/connect'
89
import type {
910
HttpServer,
@@ -126,6 +127,10 @@ export type PreviewServerHook = (
126127
export async function preview(
127128
inlineConfig: InlineConfig = {},
128129
): Promise<PreviewServer> {
130+
// The preview server is a long-running, interactive process whose
131+
// responses cannot be replayed from a cache.
132+
disableCache()
133+
129134
const config = await resolveConfig(
130135
inlineConfig,
131136
'serve',

‎packages/vite/src/node/server/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import colors from 'picocolors'
1212
import chokidar from 'chokidar'
1313
import launchEditorMiddleware from 'launch-editor-middleware'
1414
import { determineAgent } from '@vercel/detect-agent'
15+
import { disableCache } from '@voidzero-dev/vite-task-client'
1516
import type { SourceMap } from 'rolldown'
1617
import type { ModuleRunner } from 'vite/module-runner'
1718
import type { FSWatcher, WatchOptions } from '#dep-types/chokidar'
@@ -484,6 +485,10 @@ export async function _createServer(
484485
previousForceOptimizeOnRestart?: boolean
485486
},
486487
): Promise<ViteDevServer> {
488+
// The dev server is a long-running, interactive process whose outputs
489+
// (network responses, HMR updates) cannot be replayed from a cache.
490+
disableCache()
491+
487492
const config = isResolvedConfig(inlineConfig)
488493
? inlineConfig
489494
: await resolveConfig(inlineConfig, 'serve')

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)