Open History to see the last commit
1const end = "\t\targs: rustCliArgs\n\t});";
2// The hook is installed once per machine, so it must patch every Vite+ entrypoint still in use.
3// Vite+ 1.8 returns `{ exitCode, devCommand }` from `run` instead of the exit code.
4const entrypoints = [
5 { start: "\tconst exitCode = await run({", names: "exitCode", assign: "exitCode = await run({" },
6 {
7 start: "\tconst { exitCode, devCommand } = await run({",
8 names: "exitCode, devCommand",
9 assign: "({ exitCode, devCommand } = await run({",
10 },
11];
12
13/** @param {string} source */
14export function patchNativeCli(source) {
15 if (source.includes('import { startRunHook } from "./run-hook.js";')) return source;
16 const entrypoint = entrypoints.find(({ start }) => source.split(start).length === 2);
17 if (!entrypoint || source.split(end).length !== 2)
18 throw new Error("Unsupported Vite+ entrypoint; no files were changed.");
19 const close = entrypoint.assign.startsWith("(") ? "\t}));" : "\t});";
20 return (
21 'import { startRunHook } from "./run-hook.js";\n' +
22 source
23 .replace(
24 entrypoint.start,
25 `\tconst finishRunHook = await startRunHook(command, process.cwd(), async () => (await (await import("../binding/index.js")).detectWorkspace(process.cwd())).root);\n\tif (finishRunHook?.remoteCache && !rustCliArgs.includes("--last-details") && !rustCliArgs.some((arg) => arg === "--remote-cache" || arg.startsWith("--remote-cache="))) { rustCliArgs.splice(1, 0, "--remote-cache", finishRunHook.remoteCache); process.env.VP_T3_TASK_CACHE_MODE = finishRunHook.remoteCache; }\n\tlet ${entrypoint.names};\n\ttry {\n\t${entrypoint.assign}`,
26 )
27 .replace(
28 end,
29 `\t\targs: rustCliArgs\n${close}\n\t} finally {\n\t\tawait finishRunHook?.();\n\t}`,
30 )
31 );
32}