You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/runner-task-ipc/vite-proposal.md
+32-41Lines changed: 32 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,11 +43,7 @@ Vite Task needs the Vite process to communicate two things:
43
43
44
44
## Cases && Proposed Changes
45
45
46
-
The proposal has three pieces:
47
-
48
-
1. Add `@voidzero-dev/vite-task-client` as a dependency.
49
-
2. Ship an internal plugin (`vite-task-plugin`) inside Vite that calls the client at the right points. The plugin is gated by the same env var Vite Task sets, so it's fully inert when Vite runs outside Vite Task.
50
-
3. Add non-public plugin hooks where existing ones aren't enough. The hooks are designed in a generic shape (any plugin could use them) with no stability guarantee for now, ready to be promoted to public hooks later if they prove stable.
46
+
We want to add a small dependency `@voidzero-dev/vite-task-client` to Vite that lets it talk to Vite Task at runtime. Vite calls into the client at the relevant code paths to declare "ignore this path as an input/output" or "fetch these envs from Vite Task". The calls **are no-ops when Vite runs outside Vite Task**.
51
47
52
48
### 1. Reading envs according to `envPrefix`
53
49
@@ -64,7 +60,10 @@ This task config duplicates `envPrefix`. The two configs currently have to be ma
64
60
65
61
**Proposed Changes**:
66
62
67
-
`configResolved` fires after `loadEnv` has already read envs by prefix, so this case needs a new internal hook. We add `loadEnvPrefixed`, which fires inside `loadEnv` before the prefix-read loop:
63
+
Add a call to `vite-task-client`'s `fetchEnvs`**before reading the envs matching `envPrefix`**. If Vite is running outside Vite Task, this calls is a no-op. If Vite is running inside Vite Task, it does two things:
64
+
65
+
- It tells Vite Task which envs are relevant to the build, so they **become part of the cache fingerprint**.
66
+
- It **populates `process.env`** with those envs, so Vite can see them.
68
67
69
68
```diff
70
69
--- a/packages/vite/src/node/env.ts
@@ -73,17 +72,11 @@ This task config duplicates `envPrefix`. The two configs currently have to be ma
73
72
const parsed = Object.fromEntries(...);
74
73
for (const [key, value] of Object.entries(parsed))
75
74
if (prefixes.some(p => key.startsWith(p))) env[key] = value;
76
-
+ // New non-public hook. Fires synchronously so plugins can inject
77
-
+ // prefix-matched envs into process.env before the loop below reads it.
+ for (const prefix of prefixes) fetchEnvs(`${prefix}*`, { tracked: true });
79
76
for (const key in process.env)
80
77
if (prefixes.some(p => key.startsWith(p))) env[key] = process.env[key];
81
78
```
82
79
83
-
`vite-task-plugin` handles `loadEnvPrefixed` by calling `fetchEnvs(`${prefix}\*`, { tracked: true })` for each prefix. When Vite runs inside Vite Task this populates `process.env` with the matching values (so the loop below reads them) and tells Vite Task which envs to fingerprint. When Vite runs outside Vite Task the call is a no-op.
84
-
85
-
The hook is generic in shape: any plugin could use it to inject envs by prefix during `loadEnv`. It stays non-public until the shape settles, then can be promoted to a public hook.
86
-
87
80
### 2. Pre-bundling deps in `cacheDir`
88
81
89
82
Vite's dep optimizer reads metadata from and writes pre-bundled deps into [`cacheDir`](https://vite.dev/config/shared-options#cachedir) (default `node_modules/.vite/`).
@@ -102,7 +95,19 @@ And `cacheDir` is user-configurable, so the path in this config has to match wha
102
95
103
96
**Proposed Changes**:
104
97
105
-
`vite-task-plugin` handles this in the existing `configResolved` hook (no new Vite hook needed). By the time the dep optimizer reads or writes inside `config.cacheDir`, Vite Task already knows to ignore the directory.
98
+
Add two calls **right after resolving `depsCacheDir`**. If Vite is running outside Vite Task, these calls are no-ops. If Vite is running inside Vite Task, they do two things:
99
+
100
+
- They tell Vite Task to **not treat reads in this directory as inputs**.
101
+
- They tell Vite Task to **not treat writes in this directory as outputs**.
### 3. Cleaning `outDir` before writing the bundle
108
113
@@ -112,7 +117,16 @@ Before writing the bundle, Vite calls [`emptyDir(outDir)`](https://github.com/vi
112
117
113
118
**Proposed Changes**:
114
119
115
-
Same as case 2: `vite-task-plugin` calls `ignoreInput(config.build.outDir)` in `configResolved`. The bundle write is still tracked as an output, which is correct. No new Vite hook needed.
120
+
Add a call **before `emptyDir`**. If Vite is running outside Vite Task, this call is a no-op. If Vite is running inside Vite Task, it tells Vite Task to **not treat reads in this directory as inputs**.
for (const prefix ofprefixes) fetchEnvs(`${prefix}*`, { tracked: true });
161
-
},
162
-
};
163
-
```
164
-
165
-
The plugin is added to Vite's built-in plugin list and only activates when Vite Task spawns Vite (gated by the same env var that the wrapper uses to find `node_client`).
166
156
167
157
## Details of `@voidzero-dev/vite-task-client` package
168
158
@@ -230,5 +220,6 @@ Two examples of what could come next:
230
220
-**Logic locality.** Each call belongs next to the Vite code that owns its path; patching from outside puts them somewhere else.
231
221
- Users with a self-installed Vite (not the Vite+ bundled one) would get no benefit.
232
222
233
-
-**Ship the plugin and any new hooks as public Vite API from day one.**
234
-
- Vite would commit to long-term API stability for hooks (like `loadEnvPrefixed`) before their shape has settled. Internal-with-no-stability lets us iterate first; promotion to public can happen later if a hook proves stable.
223
+
-**Implement this in a Vite plugin**
224
+
- The plugin doesn't have the enough hooks to cover all cases (fetch envs for `envPrefix`).
225
+
- Enhancing the plugin API to cover all cases would be a much bigger impact on Vite.
0 commit comments