Skip to content

Commit aa6dddb

Browse files
authored
feat(vite): add VITE_* env caching for build and dev commands (#544)
Pass and fingerprint `VITE_*` environment variables to `build`/`dev`
1 parent f48af93 commit aa6dddb

6 files changed

Lines changed: 78 additions & 2 deletions

File tree

packages/cli/binding/src/cli.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,16 @@ impl TaskSynthesizer<CustomTaskSubcommand> for VitePlusTaskSynthesizer {
403403
.chain(iter::once(Str::from("build")))
404404
.chain(args.into_iter().map(Str::from))
405405
.collect(),
406-
task_options: Default::default(),
406+
task_options: UserTaskOptions {
407+
cache_config: UserCacheConfig::Enabled {
408+
cache: None,
409+
enabled_cache_config: EnabledCacheConfig {
410+
envs: Some(Box::new([Str::from("VITE_*")])),
411+
pass_through_envs: None,
412+
},
413+
},
414+
..Default::default()
415+
},
407416
direct_execution_cache_key,
408417
envs: merge_resolved_envs(envs, resolved.envs),
409418
})
@@ -479,7 +488,16 @@ impl TaskSynthesizer<CustomTaskSubcommand> for VitePlusTaskSynthesizer {
479488
.chain(iter::once(Str::from("dev")))
480489
.chain(args.into_iter().map(Str::from))
481490
.collect(),
482-
task_options: Default::default(),
491+
task_options: UserTaskOptions {
492+
cache_config: UserCacheConfig::Enabled {
493+
cache: None,
494+
enabled_cache_config: EnabledCacheConfig {
495+
envs: Some(Box::new([Str::from("VITE_*")])),
496+
pass_through_envs: None,
497+
},
498+
},
499+
..Default::default()
500+
},
483501
direct_execution_cache_key,
484502
envs: merge_resolved_envs(envs, resolved.envs),
485503
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<script type="module">
5+
console.log('hello');
6+
</script>
7+
</body>
8+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "build-vite-env-test",
3+
"private": true
4+
}

packages/cli/snap-tests/build-vite-env/snap.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
> VITE_MY_VAR=1 vite build
2+
vite v<semver>
3+
transforming...✓ <variable> modules transformed.
4+
rendering chunks...
5+
computing gzip size...
6+
dist/index.html <variable> kB │ gzip: <variable> kB
7+
dist/assets/index-BnIqjoTZ.js <variable> kB │ gzip: <variable> kB
8+
9+
✓ built in <variable>ms
10+
11+
12+
> VITE_MY_VAR=1 vite build # should hit cache
13+
vite v<semver>
14+
transforming...✓ <variable> modules transformed.
15+
rendering chunks...
16+
computing gzip size...
17+
dist/index.html <variable> kB │ gzip: <variable> kB
18+
dist/assets/index-BnIqjoTZ.js <variable> kB │ gzip: <variable> kB
19+
20+
✓ built in <variable>ms
21+
✓ cache hit, logs replayed
22+
23+
24+
> VITE_MY_VAR=2 vite build # env changed, should miss cache
25+
vite v<semver>
26+
transforming...✓ <variable> modules transformed.
27+
rendering chunks...
28+
computing gzip size...
29+
dist/index.html <variable> kB │ gzip: <variable> kB
30+
dist/assets/index-BnIqjoTZ.js <variable> kB │ gzip: <variable> kB
31+
32+
✓ built in <variable>ms
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"ignoredPlatforms": ["win32"],
3+
"env": {
4+
"VITE_DISABLE_AUTO_INSTALL": "1"
5+
},
6+
"commands": [
7+
"VITE_MY_VAR=1 vite build",
8+
"VITE_MY_VAR=1 vite build # should hit cache",
9+
"VITE_MY_VAR=2 vite build # env changed, should miss cache"
10+
]
11+
}

packages/tools/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export function replaceUnstableOutput(output: string, cwd?: string) {
8282
'$1$2\n',
8383
)
8484
.replaceAll(/\nfound \d+ vulnerabilities\n/g, '')
85+
// vite modules transformed count
86+
.replaceAll(/ \d+ modules? transformed/g, '✓ <variable> modules transformed')
8587
// replace size for tsdown
8688
.replaceAll(/ \d+(\.\d+)? ([km]?B)/g, ' <variable> $2')
8789
// replace npm notice size:

0 commit comments

Comments
 (0)