next-yak 9.2.0 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/dist/context/baseContext.cjs.map +1 -1
- package/dist/context/baseContext.d.cts +1 -2
- package/dist/context/baseContext.d.ts +1 -2
- package/dist/context/baseContext.js.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/isolated-source-eval/index.d.ts +28 -0
- package/dist/isolated-source-eval/index.js +329 -0
- package/dist/isolated-source-eval/index.js.map +1 -0
- package/dist/isolated-source-eval/worker.js +63 -0
- package/dist/isolated-source-eval/worker.js.map +1 -0
- package/dist/loaders/vite-plugin.d.ts +8 -0
- package/dist/loaders/vite-plugin.js +52 -71
- package/dist/loaders/vite-plugin.js.map +1 -1
- package/loaders/vite-plugin.ts +60 -78
- package/package.json +3 -3
- package/runtime/context/baseContext.tsx +1 -1
- package/runtime/index.ts +1 -1
- package/runtime/mocks/cssLiteral.ts +18 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// isolated-source-eval/evaluator.ts
|
|
2
|
+
import { Worker } from "worker_threads";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
import { isAbsolute } from "path";
|
|
5
|
+
import { existsSync } from "fs";
|
|
6
|
+
|
|
7
|
+
// isolated-source-eval/loader-hooks.ts
|
|
8
|
+
var LOADER_HOOK_SOURCE = `
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
|
|
11
|
+
let port;
|
|
12
|
+
const deps = new Set();
|
|
13
|
+
|
|
14
|
+
export function initialize(data) {
|
|
15
|
+
port = data.port;
|
|
16
|
+
port.on("message", (msg) => {
|
|
17
|
+
if (msg === "getDeps") {
|
|
18
|
+
port.postMessage([...deps]);
|
|
19
|
+
deps.clear();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
25
|
+
const result = await nextResolve(specifier);
|
|
26
|
+
if (result.url.startsWith("file://") && !result.url.includes("/node_modules/")) {
|
|
27
|
+
deps.add(fileURLToPath(result.url));
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
var LOADER_DATA_URL = `data:text/javascript;base64,${Buffer.from(LOADER_HOOK_SOURCE).toString("base64")}`;
|
|
33
|
+
function createLoaderDataUrl() {
|
|
34
|
+
return LOADER_DATA_URL;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// isolated-source-eval/evaluator.ts
|
|
38
|
+
function resolveWorkerPath() {
|
|
39
|
+
const jsPath = fileURLToPath(new URL("./worker.js", import.meta.url));
|
|
40
|
+
if (existsSync(jsPath)) {
|
|
41
|
+
return jsPath;
|
|
42
|
+
}
|
|
43
|
+
return fileURLToPath(new URL("./worker.ts", import.meta.url));
|
|
44
|
+
}
|
|
45
|
+
function bootWorker() {
|
|
46
|
+
const workerPath = resolveWorkerPath();
|
|
47
|
+
const loaderDataUrl = createLoaderDataUrl();
|
|
48
|
+
const worker = new Worker(workerPath, {
|
|
49
|
+
workerData: { loaderDataUrl }
|
|
50
|
+
});
|
|
51
|
+
const ready = new Promise((resolve) => {
|
|
52
|
+
const onMessage = (msg) => {
|
|
53
|
+
if (msg.type === "ready") {
|
|
54
|
+
worker.off("message", onMessage);
|
|
55
|
+
resolve();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
worker.on("message", onMessage);
|
|
59
|
+
});
|
|
60
|
+
return { worker, ready };
|
|
61
|
+
}
|
|
62
|
+
var DEFAULT_EVAL_TIMEOUT_MS = 3e4;
|
|
63
|
+
var MAX_STALENESS_RETRIES = 3;
|
|
64
|
+
async function createEvaluator() {
|
|
65
|
+
const resultCache = /* @__PURE__ */ new Map();
|
|
66
|
+
const inflight = /* @__PURE__ */ new Map();
|
|
67
|
+
const reverseDeps = /* @__PURE__ */ new Map();
|
|
68
|
+
const forwardDeps = /* @__PURE__ */ new Map();
|
|
69
|
+
let nextId = 0;
|
|
70
|
+
let currentEval = null;
|
|
71
|
+
let currentTimeout = null;
|
|
72
|
+
const queue = [];
|
|
73
|
+
const invalidatedDuringEval = /* @__PURE__ */ new Set();
|
|
74
|
+
let disposed = false;
|
|
75
|
+
let primary = bootWorker();
|
|
76
|
+
let shadow = bootWorker();
|
|
77
|
+
await Promise.all([primary.ready, shadow.ready]);
|
|
78
|
+
function setupMessageHandler(workerObj) {
|
|
79
|
+
workerObj.worker.on("message", (msg) => {
|
|
80
|
+
if (msg.type !== "result") return;
|
|
81
|
+
handleResult(msg);
|
|
82
|
+
});
|
|
83
|
+
workerObj.worker.on("error", (err) => {
|
|
84
|
+
if (currentEval) {
|
|
85
|
+
clearEvalTimeout();
|
|
86
|
+
const pending = currentEval;
|
|
87
|
+
currentEval = null;
|
|
88
|
+
const result = {
|
|
89
|
+
ok: false,
|
|
90
|
+
error: { message: err.message, stack: err.stack ?? "" }
|
|
91
|
+
};
|
|
92
|
+
updateDeps(pending.absolutePath, [pending.absolutePath]);
|
|
93
|
+
resultCache.set(pending.absolutePath, result);
|
|
94
|
+
inflight.delete(pending.absolutePath);
|
|
95
|
+
pending.resolve(result);
|
|
96
|
+
processQueue();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
setupMessageHandler(primary);
|
|
101
|
+
function clearEvalTimeout() {
|
|
102
|
+
if (currentTimeout !== null) {
|
|
103
|
+
clearTimeout(currentTimeout);
|
|
104
|
+
currentTimeout = null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function handleResult(msg) {
|
|
108
|
+
if (!currentEval) return;
|
|
109
|
+
if (msg.id !== currentEval.id) return;
|
|
110
|
+
clearEvalTimeout();
|
|
111
|
+
const pending = currentEval;
|
|
112
|
+
currentEval = null;
|
|
113
|
+
if (msg.ok && invalidatedDuringEval.size > 0) {
|
|
114
|
+
const isStale = msg.dependencies.some(
|
|
115
|
+
(dep) => invalidatedDuringEval.has(dep)
|
|
116
|
+
);
|
|
117
|
+
if (isStale) {
|
|
118
|
+
if (pending.retryCount >= MAX_STALENESS_RETRIES) {
|
|
119
|
+
pending.resolve({
|
|
120
|
+
ok: false,
|
|
121
|
+
error: {
|
|
122
|
+
message: `Evaluation of ${pending.absolutePath} exceeded maximum staleness retries (${MAX_STALENESS_RETRIES})`,
|
|
123
|
+
stack: ""
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
processQueue();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
pending.retryCount++;
|
|
130
|
+
swapWorkers();
|
|
131
|
+
queue.unshift(pending);
|
|
132
|
+
processQueue();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
let result;
|
|
137
|
+
if (msg.ok) {
|
|
138
|
+
result = { ok: true, value: msg.value, dependencies: msg.dependencies };
|
|
139
|
+
updateDeps(pending.absolutePath, msg.dependencies);
|
|
140
|
+
} else {
|
|
141
|
+
result = { ok: false, error: msg.error };
|
|
142
|
+
updateDeps(pending.absolutePath, msg.dependencies);
|
|
143
|
+
}
|
|
144
|
+
resultCache.set(pending.absolutePath, result);
|
|
145
|
+
inflight.delete(pending.absolutePath);
|
|
146
|
+
pending.resolve(result);
|
|
147
|
+
processQueue();
|
|
148
|
+
}
|
|
149
|
+
function updateDeps(entryPoint, deps) {
|
|
150
|
+
const oldDeps = forwardDeps.get(entryPoint);
|
|
151
|
+
if (oldDeps) {
|
|
152
|
+
for (const dep of oldDeps) {
|
|
153
|
+
const entries = reverseDeps.get(dep);
|
|
154
|
+
if (entries) {
|
|
155
|
+
entries.delete(entryPoint);
|
|
156
|
+
if (entries.size === 0) {
|
|
157
|
+
reverseDeps.delete(dep);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const depSet = new Set(deps);
|
|
163
|
+
forwardDeps.set(entryPoint, depSet);
|
|
164
|
+
for (const dep of depSet) {
|
|
165
|
+
let entries = reverseDeps.get(dep);
|
|
166
|
+
if (!entries) {
|
|
167
|
+
entries = /* @__PURE__ */ new Set();
|
|
168
|
+
reverseDeps.set(dep, entries);
|
|
169
|
+
}
|
|
170
|
+
entries.add(entryPoint);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function processQueue() {
|
|
174
|
+
if (currentEval || queue.length === 0 || disposed) return;
|
|
175
|
+
const next = queue.shift();
|
|
176
|
+
const alreadyCached = resultCache.get(next.absolutePath);
|
|
177
|
+
if (alreadyCached) {
|
|
178
|
+
inflight.delete(next.absolutePath);
|
|
179
|
+
next.resolve(alreadyCached);
|
|
180
|
+
processQueue();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
currentEval = next;
|
|
184
|
+
primary.ready.then(() => {
|
|
185
|
+
if (disposed || currentEval !== next) return;
|
|
186
|
+
primary.worker.postMessage({
|
|
187
|
+
type: "evaluate",
|
|
188
|
+
id: next.id,
|
|
189
|
+
absolutePath: next.absolutePath
|
|
190
|
+
});
|
|
191
|
+
currentTimeout = setTimeout(() => {
|
|
192
|
+
if (currentEval !== next) return;
|
|
193
|
+
currentEval = null;
|
|
194
|
+
currentTimeout = null;
|
|
195
|
+
const result = {
|
|
196
|
+
ok: false,
|
|
197
|
+
error: {
|
|
198
|
+
message: `Evaluation of ${next.absolutePath} timed out after ${DEFAULT_EVAL_TIMEOUT_MS}ms`,
|
|
199
|
+
stack: ""
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
updateDeps(next.absolutePath, [next.absolutePath]);
|
|
203
|
+
resultCache.set(next.absolutePath, result);
|
|
204
|
+
inflight.delete(next.absolutePath);
|
|
205
|
+
next.resolve(result);
|
|
206
|
+
swapWorkers();
|
|
207
|
+
processQueue();
|
|
208
|
+
}, DEFAULT_EVAL_TIMEOUT_MS);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function swapWorkers() {
|
|
212
|
+
primary.worker.removeAllListeners();
|
|
213
|
+
primary.worker.terminate();
|
|
214
|
+
primary = shadow;
|
|
215
|
+
setupMessageHandler(primary);
|
|
216
|
+
shadow = bootWorker();
|
|
217
|
+
invalidatedDuringEval.clear();
|
|
218
|
+
}
|
|
219
|
+
function evaluate(absolutePath) {
|
|
220
|
+
if (disposed) {
|
|
221
|
+
return Promise.reject(new Error("Evaluator has been disposed"));
|
|
222
|
+
}
|
|
223
|
+
if (!isAbsolute(absolutePath)) {
|
|
224
|
+
return Promise.reject(
|
|
225
|
+
new Error(`evaluate() requires an absolute path, got: ${absolutePath}`)
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const cached = resultCache.get(absolutePath);
|
|
229
|
+
if (cached) {
|
|
230
|
+
return Promise.resolve(cached);
|
|
231
|
+
}
|
|
232
|
+
const pending = inflight.get(absolutePath);
|
|
233
|
+
if (pending) return pending;
|
|
234
|
+
const promise = new Promise((resolve, reject) => {
|
|
235
|
+
const id = nextId++;
|
|
236
|
+
queue.push({ id, absolutePath, resolve, reject, retryCount: 0 });
|
|
237
|
+
processQueue();
|
|
238
|
+
});
|
|
239
|
+
inflight.set(absolutePath, promise);
|
|
240
|
+
return promise;
|
|
241
|
+
}
|
|
242
|
+
function invalidate(...absolutePaths) {
|
|
243
|
+
if (absolutePaths.length === 0) return;
|
|
244
|
+
for (const path of absolutePaths) {
|
|
245
|
+
invalidatedDuringEval.add(path);
|
|
246
|
+
}
|
|
247
|
+
const allEntryPoints = /* @__PURE__ */ new Set();
|
|
248
|
+
for (const path of absolutePaths) {
|
|
249
|
+
const entryPoints = reverseDeps.get(path);
|
|
250
|
+
if (entryPoints) {
|
|
251
|
+
for (const entry of entryPoints) {
|
|
252
|
+
allEntryPoints.add(entry);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (allEntryPoints.size === 0) return;
|
|
257
|
+
for (const entry of allEntryPoints) {
|
|
258
|
+
resultCache.delete(entry);
|
|
259
|
+
inflight.delete(entry);
|
|
260
|
+
const deps = forwardDeps.get(entry);
|
|
261
|
+
if (deps) {
|
|
262
|
+
for (const dep of deps) {
|
|
263
|
+
const entries = reverseDeps.get(dep);
|
|
264
|
+
if (entries) {
|
|
265
|
+
entries.delete(entry);
|
|
266
|
+
if (entries.size === 0) {
|
|
267
|
+
reverseDeps.delete(dep);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
forwardDeps.delete(entry);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
clearEvalTimeout();
|
|
275
|
+
const inflightEval = currentEval;
|
|
276
|
+
currentEval = null;
|
|
277
|
+
swapWorkers();
|
|
278
|
+
if (inflightEval) {
|
|
279
|
+
queue.unshift(inflightEval);
|
|
280
|
+
}
|
|
281
|
+
processQueue();
|
|
282
|
+
}
|
|
283
|
+
function invalidateAll() {
|
|
284
|
+
resultCache.clear();
|
|
285
|
+
inflight.clear();
|
|
286
|
+
forwardDeps.clear();
|
|
287
|
+
reverseDeps.clear();
|
|
288
|
+
clearEvalTimeout();
|
|
289
|
+
const inflightEval = currentEval;
|
|
290
|
+
currentEval = null;
|
|
291
|
+
swapWorkers();
|
|
292
|
+
if (inflightEval) {
|
|
293
|
+
queue.unshift(inflightEval);
|
|
294
|
+
}
|
|
295
|
+
processQueue();
|
|
296
|
+
}
|
|
297
|
+
function getDependentsOf(absolutePath) {
|
|
298
|
+
const entryPoints = reverseDeps.get(absolutePath);
|
|
299
|
+
if (!entryPoints) return [];
|
|
300
|
+
return [...entryPoints];
|
|
301
|
+
}
|
|
302
|
+
async function dispose() {
|
|
303
|
+
if (disposed) return;
|
|
304
|
+
disposed = true;
|
|
305
|
+
clearEvalTimeout();
|
|
306
|
+
if (currentEval) {
|
|
307
|
+
currentEval.reject(new Error("Evaluator has been disposed"));
|
|
308
|
+
currentEval = null;
|
|
309
|
+
}
|
|
310
|
+
for (const pending of queue) {
|
|
311
|
+
pending.reject(new Error("Evaluator has been disposed"));
|
|
312
|
+
}
|
|
313
|
+
queue.length = 0;
|
|
314
|
+
inflight.clear();
|
|
315
|
+
await Promise.all([primary.worker.terminate(), shadow.worker.terminate()]);
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
evaluate,
|
|
319
|
+
invalidate,
|
|
320
|
+
invalidateAll,
|
|
321
|
+
getDependentsOf,
|
|
322
|
+
dispose,
|
|
323
|
+
[Symbol.asyncDispose]: dispose
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
export {
|
|
327
|
+
createEvaluator
|
|
328
|
+
};
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../isolated-source-eval/evaluator.ts","../../isolated-source-eval/loader-hooks.ts"],"sourcesContent":["import { Worker } from \"node:worker_threads\";\nimport { fileURLToPath } from \"node:url\";\nimport { isAbsolute } from \"node:path\";\nimport { existsSync } from \"node:fs\";\nimport { createLoaderDataUrl } from \"./loader-hooks.ts\";\n\nexport type EvaluateResult =\n | { ok: true; value: Record<string, unknown>; dependencies: string[] }\n | { ok: false; error: { message: string; stack: string } };\n\nexport interface Evaluator {\n /** Evaluate a module and return its exports plus the full transitive dependency list. */\n evaluate(absolutePath: string): Promise<EvaluateResult>;\n /** Clear cached results for every entry point that transitively depends on the given file(s). */\n invalidate(...absolutePaths: string[]): void;\n /** Clear the entire result cache and swap workers. */\n invalidateAll(): void;\n /** Return entry point paths that would be affected by invalidate(absolutePath). */\n getDependentsOf(absolutePath: string): string[];\n /** Terminate both workers and reject any pending evaluations. */\n dispose(): Promise<void>;\n [Symbol.asyncDispose](): Promise<void>;\n}\n\n/** Wire format for messages from the worker back to the main thread. */\ntype WorkerResult =\n | {\n type: \"result\";\n id: number;\n ok: true;\n value: Record<string, unknown>;\n dependencies: string[];\n }\n | {\n type: \"result\";\n id: number;\n ok: false;\n error: { message: string; stack: string };\n dependencies: string[];\n };\n\ninterface PendingEvaluation {\n id: number;\n absolutePath: string;\n resolve: (result: EvaluateResult) => void;\n reject: (error: Error) => void;\n /** Number of times this evaluation has been retried due to staleness detection. */\n retryCount: number;\n}\n\n/**\n * Resolves the worker entry point path.\n *\n * In production the compiled `.js` file exists in `dist/` — we prefer that.\n * During development (vitest runs against source), only the `.ts` file exists.\n * Node 22.18.0+ handles `.ts` natively via strip-types, so the fallback works\n * without any flags or loaders.\n */\nfunction resolveWorkerPath(): string {\n const jsPath = fileURLToPath(new URL(\"./worker.js\", import.meta.url));\n if (existsSync(jsPath)) {\n return jsPath;\n }\n return fileURLToPath(new URL(\"./worker.ts\", import.meta.url));\n}\n\n/**\n * Boots a worker and returns a handle with a ready promise.\n *\n * The loader hook data URL is passed via `workerData` rather than having\n * the worker import it from a sibling module. This avoids a module\n * resolution mismatch: source files use `.ts` extensions (required by\n * Node strip-types) while compiled output uses `.js` (rewritten by tsc\n * via `rewriteRelativeImportExtensions`). Since the worker file would need\n * to import loader-hooks with the right extension for its context, passing\n * the data URL through workerData eliminates the problem entirely.\n */\nfunction bootWorker(): { worker: Worker; ready: Promise<void> } {\n const workerPath = resolveWorkerPath();\n const loaderDataUrl = createLoaderDataUrl();\n const worker = new Worker(workerPath, {\n workerData: { loaderDataUrl },\n });\n const ready = new Promise<void>((resolve) => {\n const onMessage = (msg: { type: string }) => {\n if (msg.type === \"ready\") {\n worker.off(\"message\", onMessage);\n resolve();\n }\n };\n worker.on(\"message\", onMessage);\n });\n return { worker, ready };\n}\n\n/** Default timeout for a single evaluation in milliseconds. */\nconst DEFAULT_EVAL_TIMEOUT_MS = 30_000;\n\n/** Maximum number of staleness retries before giving up with an error. */\nconst MAX_STALENESS_RETRIES = 3;\n\n/** Creates an evaluator that runs modules in isolated worker threads with dependency tracking. */\nexport async function createEvaluator(): Promise<Evaluator> {\n const resultCache = new Map<string, EvaluateResult>();\n /**\n * Tracks in-flight evaluation promises by path. When `evaluate()` is called\n * for a path that's already queued or being evaluated, the same promise is\n * returned instead of queuing a duplicate. This prevents a second worker\n * evaluation from producing incomplete dependencies (the ESM resolve hook\n * only fires on cache misses — a second import on the same worker would\n * miss transitive deps that are already in the ESM cache).\n */\n const inflight = new Map<string, Promise<EvaluateResult>>();\n\n /**\n * Two dependency maps provide O(1) lookups in both directions:\n *\n * - `reverseDeps`: file path → set of entry points that depend on it.\n * Consulted during `invalidate()` to find which cached results to clear.\n *\n * - `forwardDeps`: entry point → set of all its transitive dependencies.\n * Needed to clean up stale reverse entries when an entry point is\n * re-evaluated and its dependency tree has changed (e.g. an import\n * was removed). Without this, removed dependencies would accumulate\n * phantom reverse entries that never get cleaned up.\n */\n const reverseDeps = new Map<string, Set<string>>();\n const forwardDeps = new Map<string, Set<string>>();\n\n let nextId = 0;\n let currentEval: PendingEvaluation | null = null;\n let currentTimeout: ReturnType<typeof setTimeout> | null = null;\n const queue: PendingEvaluation[] = [];\n /**\n * Tracks files passed to `invalidate()` while an evaluation is in-flight.\n * When an evaluation completes, `handleResult()` checks whether any reported\n * dependency intersects this set. If so, the result is stale — it's discarded,\n * workers are swapped, and the evaluation is retried on a clean worker.\n *\n * This closes the \"cold start\" gap: if `evaluate(\"A\")` is dispatched before\n * `invalidate(\"B\")` is called, and A depends on B, the dependency graph\n * doesn't know about A→B yet. Without this set, the invalidation would be\n * silently missed.\n *\n * Cleared in `swapWorkers()` because the new primary has a clean ESM cache.\n */\n const invalidatedDuringEval = new Set<string>();\n let disposed = false;\n\n /**\n * Two workers are maintained at all times:\n *\n * - **primary**: handles all evaluate() calls. Accumulates Node's ESM\n * module cache over time, making repeated evaluations fast (~1-3ms).\n *\n * - **shadow**: pre-booted but completely idle. Never evaluates anything,\n * so its module cache stays empty. On invalidation, the primary is\n * terminated (stale cache) and the shadow is promoted to primary\n * instantly (no boot latency). A new shadow is booted in the background.\n *\n * This pattern trades ~30ms of background boot time for instant\n * invalidation. The alternative — reusing a single worker and clearing\n * its ESM cache — isn't possible because Node doesn't expose an API\n * to clear the module cache.\n */\n let primary = bootWorker();\n let shadow = bootWorker();\n\n await Promise.all([primary.ready, shadow.ready]);\n\n function setupMessageHandler(workerObj: { worker: Worker }) {\n workerObj.worker.on(\"message\", (msg: WorkerResult) => {\n if (msg.type !== \"result\") return;\n handleResult(msg);\n });\n\n workerObj.worker.on(\"error\", (err: Error) => {\n if (currentEval) {\n clearEvalTimeout();\n const pending = currentEval;\n currentEval = null;\n const result: EvaluateResult = {\n ok: false,\n error: { message: err.message, stack: err.stack ?? \"\" },\n };\n updateDeps(pending.absolutePath, [pending.absolutePath]);\n resultCache.set(pending.absolutePath, result);\n inflight.delete(pending.absolutePath);\n pending.resolve(result);\n processQueue();\n }\n });\n }\n\n setupMessageHandler(primary);\n\n function clearEvalTimeout() {\n if (currentTimeout !== null) {\n clearTimeout(currentTimeout);\n currentTimeout = null;\n }\n }\n\n function handleResult(msg: WorkerResult) {\n if (!currentEval) return;\n if (msg.id !== currentEval.id) return;\n\n clearEvalTimeout();\n\n const pending = currentEval;\n currentEval = null;\n\n // Staleness check: if a file was invalidated while this evaluation was\n // in-flight and the result depends on it, discard the result and retry\n // on a clean worker. Only checked for successes — errors don't report\n // deps, and the file watcher will trigger another invalidation when the\n // source is fixed.\n if (msg.ok && invalidatedDuringEval.size > 0) {\n const isStale = msg.dependencies.some((dep) =>\n invalidatedDuringEval.has(dep),\n );\n if (isStale) {\n if (pending.retryCount >= MAX_STALENESS_RETRIES) {\n pending.resolve({\n ok: false,\n error: {\n message: `Evaluation of ${pending.absolutePath} exceeded maximum staleness retries (${MAX_STALENESS_RETRIES})`,\n stack: \"\",\n },\n });\n processQueue();\n return;\n }\n pending.retryCount++;\n swapWorkers();\n queue.unshift(pending);\n processQueue();\n return;\n }\n }\n\n let result: EvaluateResult;\n if (msg.ok) {\n result = { ok: true, value: msg.value, dependencies: msg.dependencies };\n updateDeps(pending.absolutePath, msg.dependencies);\n } else {\n result = { ok: false, error: msg.error };\n // Use the partial dependencies reported by the worker (files resolved\n // before the error). This ensures transitive deps are registered in\n // reverseDeps so that fixing a broken file triggers re-evaluation.\n updateDeps(pending.absolutePath, msg.dependencies);\n }\n\n resultCache.set(pending.absolutePath, result);\n inflight.delete(pending.absolutePath);\n pending.resolve(result);\n processQueue();\n }\n\n /**\n * Reconciles the dependency graph after an evaluation.\n *\n * First removes all old reverse entries for this entry point (using\n * forwardDeps as the source of truth), then writes the new forward\n * and reverse mappings. This ensures that if a dependency was removed\n * between evaluations, it no longer points back to this entry point.\n */\n function updateDeps(entryPoint: string, deps: string[]) {\n const oldDeps = forwardDeps.get(entryPoint);\n if (oldDeps) {\n for (const dep of oldDeps) {\n const entries = reverseDeps.get(dep);\n if (entries) {\n entries.delete(entryPoint);\n if (entries.size === 0) {\n reverseDeps.delete(dep);\n }\n }\n }\n }\n\n const depSet = new Set(deps);\n forwardDeps.set(entryPoint, depSet);\n\n for (const dep of depSet) {\n let entries = reverseDeps.get(dep);\n if (!entries) {\n entries = new Set();\n reverseDeps.set(dep, entries);\n }\n entries.add(entryPoint);\n }\n }\n\n /**\n * Evaluations are serialized — one at a time per worker. This prevents\n * interleaved dependency tracking: the loader hook accumulates deps in\n * a single Set and flushes it after each evaluation. Concurrent imports\n * would mix dependencies from different entry points.\n *\n * Waits for the primary worker to be ready before sending work. This\n * matters after a swap: the promoted shadow may still be booting.\n */\n function processQueue() {\n if (currentEval || queue.length === 0 || disposed) return;\n\n const next = queue.shift()!;\n\n // A prior evaluation for the same path may have completed and cached\n // while this item was queued (e.g. after invalidation + re-queue).\n // Resolving from cache avoids dispatching to a worker where the module\n // is already in the ESM cache (which would produce incomplete deps).\n const alreadyCached = resultCache.get(next.absolutePath);\n if (alreadyCached) {\n inflight.delete(next.absolutePath);\n next.resolve(alreadyCached);\n processQueue();\n return;\n }\n\n currentEval = next;\n\n primary.ready.then(() => {\n if (disposed || currentEval !== next) return;\n primary.worker.postMessage({\n type: \"evaluate\",\n id: next.id,\n absolutePath: next.absolutePath,\n });\n\n currentTimeout = setTimeout(() => {\n if (currentEval !== next) return;\n currentEval = null;\n currentTimeout = null;\n\n const result: EvaluateResult = {\n ok: false,\n error: {\n message: `Evaluation of ${next.absolutePath} timed out after ${DEFAULT_EVAL_TIMEOUT_MS}ms`,\n stack: \"\",\n },\n };\n updateDeps(next.absolutePath, [next.absolutePath]);\n resultCache.set(next.absolutePath, result);\n inflight.delete(next.absolutePath);\n next.resolve(result);\n\n swapWorkers();\n processQueue();\n }, DEFAULT_EVAL_TIMEOUT_MS);\n });\n }\n\n /**\n * Terminates the primary (stale module cache), promotes the shadow\n * (clean cache, instant), and boots a new shadow in the background.\n *\n * `removeAllListeners` prevents the terminated worker from delivering\n * late messages or error events that would confuse the result handler.\n */\n function swapWorkers() {\n primary.worker.removeAllListeners();\n primary.worker.terminate();\n\n primary = shadow;\n setupMessageHandler(primary);\n\n shadow = bootWorker();\n\n // The new primary has a clean ESM cache, so any previously tracked\n // invalidations are no longer relevant — the fresh worker will\n // re-import everything from disk.\n invalidatedDuringEval.clear();\n }\n\n function evaluate(absolutePath: string): Promise<EvaluateResult> {\n if (disposed) {\n return Promise.reject(new Error(\"Evaluator has been disposed\"));\n }\n\n if (!isAbsolute(absolutePath)) {\n return Promise.reject(\n new Error(`evaluate() requires an absolute path, got: ${absolutePath}`),\n );\n }\n\n const cached = resultCache.get(absolutePath);\n if (cached) {\n return Promise.resolve(cached);\n }\n\n // Deduplicate: if an evaluation is already queued or in-flight for this\n // path, return the same promise. This prevents a second worker evaluation\n // that would produce incomplete dependencies (the ESM resolve hook only\n // fires on cache misses).\n const pending = inflight.get(absolutePath);\n if (pending) return pending;\n\n const promise = new Promise<EvaluateResult>((resolve, reject) => {\n const id = nextId++;\n queue.push({ id, absolutePath, resolve, reject, retryCount: 0 });\n processQueue();\n });\n\n inflight.set(absolutePath, promise);\n return promise;\n }\n\n function invalidate(...absolutePaths: string[]): void {\n if (absolutePaths.length === 0) return;\n\n // Track all invalidations for cold-start detection (in-flight evaluations\n // whose dependency graph isn't populated yet).\n for (const path of absolutePaths) {\n invalidatedDuringEval.add(path);\n }\n\n // Collect all affected entry points across all changed files.\n const allEntryPoints = new Set<string>();\n for (const path of absolutePaths) {\n const entryPoints = reverseDeps.get(path);\n if (entryPoints) {\n for (const entry of entryPoints) {\n allEntryPoints.add(entry);\n }\n }\n }\n\n // If no cached entry points are affected, return early. The cold-start\n // set (invalidatedDuringEval) will catch in-flight evaluations.\n if (allEntryPoints.size === 0) return;\n\n // Clear caches and clean dependency graphs for all affected entry points.\n for (const entry of allEntryPoints) {\n resultCache.delete(entry);\n inflight.delete(entry);\n\n const deps = forwardDeps.get(entry);\n if (deps) {\n for (const dep of deps) {\n const entries = reverseDeps.get(dep);\n if (entries) {\n entries.delete(entry);\n if (entries.size === 0) {\n reverseDeps.delete(dep);\n }\n }\n }\n forwardDeps.delete(entry);\n }\n }\n\n // If an evaluation is in-flight on the worker we're about to terminate,\n // capture it and re-queue at the front. The caller's promise will\n // resolve transparently with the result from the promoted shadow worker.\n clearEvalTimeout();\n const inflightEval = currentEval;\n currentEval = null;\n\n swapWorkers();\n\n if (inflightEval) {\n queue.unshift(inflightEval);\n }\n\n processQueue();\n }\n\n function invalidateAll(): void {\n resultCache.clear();\n inflight.clear();\n forwardDeps.clear();\n reverseDeps.clear();\n\n clearEvalTimeout();\n const inflightEval = currentEval;\n currentEval = null;\n\n swapWorkers();\n\n if (inflightEval) {\n queue.unshift(inflightEval);\n }\n\n processQueue();\n }\n\n function getDependentsOf(absolutePath: string): string[] {\n const entryPoints = reverseDeps.get(absolutePath);\n if (!entryPoints) return [];\n return [...entryPoints];\n }\n\n async function dispose(): Promise<void> {\n if (disposed) return;\n disposed = true;\n\n clearEvalTimeout();\n\n if (currentEval) {\n currentEval.reject(new Error(\"Evaluator has been disposed\"));\n currentEval = null;\n }\n for (const pending of queue) {\n pending.reject(new Error(\"Evaluator has been disposed\"));\n }\n queue.length = 0;\n inflight.clear();\n\n await Promise.all([primary.worker.terminate(), shadow.worker.terminate()]);\n }\n\n return {\n evaluate,\n invalidate,\n invalidateAll,\n getDependentsOf,\n dispose,\n [Symbol.asyncDispose]: dispose,\n };\n}\n","/**\n * ESM loader hook source, embedded as a string constant.\n *\n * This MUST be plain JavaScript — not TypeScript. The loader thread receives\n * this code via a `data:` URL passed to `module.register()`. Node's loader\n * thread does not run data: URLs through strip-types, so any TypeScript\n * syntax here would cause a SyntaxError at registration time.\n *\n * The hook uses a MessagePort (received in `initialize`) to communicate\n * collected dependencies back to the worker thread on demand. Dependencies\n * are accumulated in a Set during `resolve()` calls, then flushed as a\n * batch when the worker requests them after an evaluation completes. This\n * batching avoids per-import message overhead and keeps dependency tracking\n * tightly coupled to a single evaluation.\n *\n * Only `file://` URLs outside of `node_modules` are tracked. Node built-ins\n * (`node:*`) and third-party packages are excluded because consumers only\n * need to invalidate on project source file changes.\n *\n * `fileURLToPath` converts `file://` URLs into platform-correct absolute\n * paths (avoids the leading-slash issue `URL.pathname` has on Windows).\n */\nconst LOADER_HOOK_SOURCE = `\nimport { fileURLToPath } from \"node:url\";\n\nlet port;\nconst deps = new Set();\n\nexport function initialize(data) {\n port = data.port;\n port.on(\"message\", (msg) => {\n if (msg === \"getDeps\") {\n port.postMessage([...deps]);\n deps.clear();\n }\n });\n}\n\nexport async function resolve(specifier, context, nextResolve) {\n const result = await nextResolve(specifier);\n if (result.url.startsWith(\"file://\") && !result.url.includes(\"/node_modules/\")) {\n deps.add(fileURLToPath(result.url));\n }\n return result;\n}\n`;\n\n/**\n * Pre-computed data URL for the loader hook source.\n *\n * The same URL is reused across workers — each `module.register()` call\n * creates an independent loader instance with its own dependency Set\n * regardless of URL identity.\n */\nconst LOADER_DATA_URL = `data:text/javascript;base64,${Buffer.from(LOADER_HOOK_SOURCE).toString(\"base64\")}`;\n\n/** Returns the loader hook as a base64 `data:` URL. */\nexport function createLoaderDataUrl(): string {\n return LOADER_DATA_URL;\n}\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,qBAAqB;AAC9B,SAA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AAClB,aAAS,MAAM;AACf,gBAAY,MAAM;AAClB,gBAAY,MAAM;AAElB,qBAAiB;AACjB,UAAM,eAAe;AACrB,kBAAc;AAEd,gBAAY;AAEZ,QAAI,cAAc;AAChB,YAAM,QAAQ,YAAY;AAAA,IAC5B;AAEA,iBAAa;AAAA,EACf;AAEA,WAAS,gBAAgB,cAAgC;AACvD,UAAM,cAAc,YAAY,IAAI,YAAY;AAChD,QAAI,CAAC,YAAa,QAAO,CAAC;AAC1B,WAAO,CAAC,GAAG,WAAW;AAAA,EACxB;AAEA,iBAAe,UAAyB;AACtC,QAAI,SAAU;AACd,eAAW;AAEX,qBAAiB;AAEjB,QAAI,aAAa;AACf,kBAAY,OAAO,IAAI,MAAM,6BAA6B,CAAC;AAC3D,oBAAc;AAAA,IAChB;AACA,eAAW,WAAW,OAAO;AAC3B,cAAQ,OAAO,IAAI,MAAM,6BAA6B,CAAC;AAAA,IACzD;AACA,UAAM,SAAS;AACf,aAAS,MAAM;AAEf,UAAM,QAAQ,IAAI,CAAC,QAAQ,OAAO,UAAU,GAAG,OAAO,OAAO,UAAU,CAAC,CAAC;AAAA,EAC3E;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,CAAC,OAAO,YAAY,GAAG;AAAA,EACzB;AACF;","names":[]}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// isolated-source-eval/worker.ts
|
|
2
|
+
import { parentPort, workerData } from "worker_threads";
|
|
3
|
+
import { MessageChannel } from "worker_threads";
|
|
4
|
+
import { pathToFileURL } from "url";
|
|
5
|
+
import { register } from "module";
|
|
6
|
+
if (!parentPort) {
|
|
7
|
+
throw new Error("This file must be run as a worker thread");
|
|
8
|
+
}
|
|
9
|
+
var loaderDataUrl = workerData.loaderDataUrl;
|
|
10
|
+
var { port1, port2 } = new MessageChannel();
|
|
11
|
+
register(loaderDataUrl, {
|
|
12
|
+
parentURL: import.meta.url,
|
|
13
|
+
data: { port: port2 },
|
|
14
|
+
transferList: [port2]
|
|
15
|
+
});
|
|
16
|
+
function requestDeps() {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
port1.once("message", (deps) => {
|
|
19
|
+
resolve(deps);
|
|
20
|
+
});
|
|
21
|
+
port1.postMessage("getDeps");
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
parentPort.on(
|
|
25
|
+
"message",
|
|
26
|
+
async (msg) => {
|
|
27
|
+
if (msg.type !== "evaluate") return;
|
|
28
|
+
try {
|
|
29
|
+
const fileUrl = pathToFileURL(msg.absolutePath).href;
|
|
30
|
+
const ns = await import(fileUrl);
|
|
31
|
+
const deps = await requestDeps();
|
|
32
|
+
if (!deps.includes(msg.absolutePath)) {
|
|
33
|
+
deps.unshift(msg.absolutePath);
|
|
34
|
+
}
|
|
35
|
+
const value = {};
|
|
36
|
+
for (const key of Object.keys(ns)) {
|
|
37
|
+
value[key] = ns[key];
|
|
38
|
+
}
|
|
39
|
+
parentPort.postMessage({
|
|
40
|
+
type: "result",
|
|
41
|
+
id: msg.id,
|
|
42
|
+
ok: true,
|
|
43
|
+
value,
|
|
44
|
+
dependencies: deps
|
|
45
|
+
});
|
|
46
|
+
} catch (err) {
|
|
47
|
+
const error = err instanceof Error ? { message: err.message, stack: err.stack ?? "" } : { message: String(err), stack: "" };
|
|
48
|
+
const deps = await requestDeps();
|
|
49
|
+
if (!deps.includes(msg.absolutePath)) {
|
|
50
|
+
deps.unshift(msg.absolutePath);
|
|
51
|
+
}
|
|
52
|
+
parentPort.postMessage({
|
|
53
|
+
type: "result",
|
|
54
|
+
id: msg.id,
|
|
55
|
+
ok: false,
|
|
56
|
+
error,
|
|
57
|
+
dependencies: deps
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
parentPort.postMessage({ type: "ready" });
|
|
63
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../isolated-source-eval/worker.ts"],"sourcesContent":["/**\n * Worker thread entry point for isolated module evaluation.\n *\n * Each worker is a full Node.js environment with its own ESM module cache.\n * Modules are loaded via dynamic `import()` — Node 22.18.0+ strips TypeScript\n * types natively, so no compilation step or flags are needed.\n *\n * The loader hook data URL is received via `workerData` rather than\n * importing it from a sibling module. This avoids a cross-file import\n * resolution problem: Node's strip-types requires `.ts` extensions in\n * source, but compiled output uses `.js`. Passing the URL through\n * `workerData` sidesteps this entirely since the worker file has no\n * local imports beyond node built-ins.\n */\nimport { parentPort, workerData } from \"node:worker_threads\";\nimport { MessageChannel } from \"node:worker_threads\";\nimport { pathToFileURL } from \"node:url\";\n\n/**\n * We import `register` from `node:module` instead of calling `module.register()`\n * directly. Node's strip-types parser sees the bare `module` identifier as a\n * CJS global, and combined with top-level `await` (or other ESM syntax), it\n * throws \"Cannot determine intended module format\". Using the explicit import\n * avoids this ambiguity entirely.\n */\nimport { register } from \"node:module\";\n\nif (!parentPort) {\n throw new Error(\"This file must be run as a worker thread\");\n}\n\nconst loaderDataUrl: string = workerData.loaderDataUrl;\n\n/**\n * MessageChannel for communicating with the loader hook.\n * port1 stays in this worker thread; port2 is transferred to the loader\n * thread via `register()`'s transferList. This is the only way to get\n * data out of loader hooks, since they run in an isolated loader thread\n * that cannot access worker_threads.parentPort.\n */\nconst { port1, port2 } = new MessageChannel();\n\nregister(loaderDataUrl, {\n parentURL: import.meta.url,\n data: { port: port2 },\n transferList: [port2],\n});\n\n/**\n * Requests the accumulated dependency list from the loader hook.\n *\n * Called after each `import()` completes. The loader hook clears its\n * internal Set on each request, so dependencies are scoped to the\n * most recent evaluation. This is safe because evaluations are\n * serialized — only one runs at a time per worker.\n */\nfunction requestDeps(): Promise<string[]> {\n return new Promise((resolve) => {\n port1.once(\"message\", (deps: string[]) => {\n resolve(deps);\n });\n port1.postMessage(\"getDeps\");\n });\n}\n\nparentPort.on(\n \"message\",\n async (msg: { type: string; id: number; absolutePath: string }) => {\n if (msg.type !== \"evaluate\") return;\n\n try {\n const fileUrl = pathToFileURL(msg.absolutePath).href;\n const ns = await import(fileUrl);\n\n const deps = await requestDeps();\n\n // The entry point may not appear in the loader hook's dependency set\n // if it was already in the ESM cache from a previous evaluation.\n // The resolve hook only fires on cache misses.\n if (!deps.includes(msg.absolutePath)) {\n deps.unshift(msg.absolutePath);\n }\n\n // Module namespace objects are exotic objects that fail structured clone\n // (postMessage would throw). Copying to a plain object is required.\n const value: Record<string, unknown> = {};\n for (const key of Object.keys(ns)) {\n value[key] = ns[key];\n }\n\n parentPort!.postMessage({\n type: \"result\",\n id: msg.id,\n ok: true,\n value,\n dependencies: deps,\n });\n } catch (err: unknown) {\n // Catches both evaluation errors (syntax errors, runtime exceptions)\n // and structured clone failures (e.g. when exports contain functions,\n // Symbols, or other non-cloneable values). Both are returned as\n // { ok: false } results — the caller does not need to distinguish.\n const error =\n err instanceof Error\n ? { message: err.message, stack: err.stack ?? \"\" }\n : { message: String(err), stack: \"\" };\n\n // Flush partial dependencies that the loader hook tracked before the\n // error. This allows the evaluator to register them in the reverse\n // dep map so that fixing the broken file triggers re-evaluation.\n const deps = await requestDeps();\n if (!deps.includes(msg.absolutePath)) {\n deps.unshift(msg.absolutePath);\n }\n\n parentPort!.postMessage({\n type: \"result\",\n id: msg.id,\n ok: false,\n error,\n dependencies: deps,\n });\n }\n },\n);\n\nparentPort.postMessage({ type: \"ready\" });\n"],"mappings":";AAcA,SAAS,YAAY,kBAAkB;AACvC,SAAS,sBAAsB;AAC/B,SAAS,qBAAqB;AAS9B,SAAS,gBAAgB;AAEzB,IAAI,CAAC,YAAY;AACf,QAAM,IAAI,MAAM,0CAA0C;AAC5D;AAEA,IAAM,gBAAwB,WAAW;AASzC,IAAM,EAAE,OAAO,MAAM,IAAI,IAAI,eAAe;AAE5C,SAAS,eAAe;AAAA,EACtB,WAAW,YAAY;AAAA,EACvB,MAAM,EAAE,MAAM,MAAM;AAAA,EACpB,cAAc,CAAC,KAA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vite';
|
|
|
3
3
|
import { YakConfigOptions } from '../withYak/index.ts';
|
|
4
4
|
|
|
5
5
|
type ViteYakPluginOptions = YakConfigOptions & {
|
|
6
|
+
/**
|
|
7
|
+
* Base path for resolving CSS virtual module paths.
|
|
8
|
+
* Relative paths are resolved from Vite's project root.
|
|
9
|
+
* In monorepo setups where source files live outside the Vite project root,
|
|
10
|
+
* set this to the monorepo root to avoid broken CSS imports.
|
|
11
|
+
* @defaultValue Vite's resolved `root`
|
|
12
|
+
*/
|
|
13
|
+
basePath?: string;
|
|
6
14
|
swcOptions?: Omit<Options, "filename" | "sourceFileName" | "inputSourceMap" | "sourceMaps" | "sourceRoot">;
|
|
7
15
|
};
|
|
8
16
|
declare function viteYak(userOptions?: ViteYakPluginOptions): Promise<Plugin>;
|