|
| 1 | +// The JSDoc in this file is the source of truth for the package's public |
| 2 | +// types. `index.d.ts` is generated from it via `pnpm run build:types` |
| 3 | +// (using `tsc` with `@tsconfig/strictest`) — edit JSDoc here, not the |
| 4 | +// `.d.ts`. CI fails if the committed `.d.ts` drifts from a fresh regen. |
| 5 | + |
| 6 | +import { createRequire } from 'node:module'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Methods exposed by the napi addon. Keep this shape in sync with the |
| 10 | + * `RunnerClient` returned by `load()` in |
| 11 | + * `crates/vite_task_client_napi/src/lib.rs` — any new method added there |
| 12 | + * needs a matching entry here, and vice versa. |
| 13 | + * |
| 14 | + * @type {{ |
| 15 | + * ignoreInput: (path: string) => void, |
| 16 | + * ignoreOutput: (path: string) => void, |
| 17 | + * disableCache: () => void, |
| 18 | + * getEnv: (name: string, options?: { tracked?: boolean }) => string | undefined, |
| 19 | + * getEnvs: (pattern: string, options?: { tracked?: boolean }) => Record<string, string>, |
| 20 | + * } | null | undefined} |
| 21 | + */ |
| 22 | +let addon; |
| 23 | + |
| 24 | +function load() { |
| 25 | + if (addon !== undefined) return addon; |
| 26 | + try { |
| 27 | + const path = process.env['VP_RUN_NODE_CLIENT_PATH']; |
| 28 | + if (path) { |
| 29 | + // The addon exports a `load(options?)` factory rather than the |
| 30 | + // methods directly, so the addon shape can evolve in lockstep with |
| 31 | + // this wrapper: a future wrapper can pass `{ version: N }` to opt |
| 32 | + // into a new shape without breaking older addons that only know v1. |
| 33 | + // Today's wrapper passes nothing and accepts whatever the addon's |
| 34 | + // current default version returns. |
| 35 | + addon = createRequire(import.meta.url)(path).load(); |
| 36 | + return addon; |
| 37 | + } |
| 38 | + } catch { |
| 39 | + // Fall through — the runner's IPC env is absent or the addon refused to |
| 40 | + // load. Memoize the unavailable decision so subsequent calls don't retry. |
| 41 | + } |
| 42 | + addon = null; |
| 43 | + return addon; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Tell the runner to ignore reads under `path` when inferring cache inputs. |
| 48 | + * |
| 49 | + * No-op when not running inside a runner. |
| 50 | + * |
| 51 | + * @param {string} path |
| 52 | + * @returns {void} |
| 53 | + */ |
| 54 | +export function ignoreInput(path) { |
| 55 | + load()?.ignoreInput(path); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Tell the runner to ignore writes under `path` when inferring cache outputs. |
| 60 | + * |
| 61 | + * No-op when not running inside a runner. |
| 62 | + * |
| 63 | + * @param {string} path |
| 64 | + * @returns {void} |
| 65 | + */ |
| 66 | +export function ignoreOutput(path) { |
| 67 | + load()?.ignoreOutput(path); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Tell the runner not to cache this run. |
| 72 | + * |
| 73 | + * No-op when not running inside a runner. |
| 74 | + * |
| 75 | + * @returns {void} |
| 76 | + */ |
| 77 | +export function disableCache() { |
| 78 | + load()?.disableCache(); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Ask the runner for the value of the env var `name` and return it, or |
| 83 | + * `undefined` when the runner has no such env. |
| 84 | + * |
| 85 | + * With `tracked: true` (the default) the runner records `name` as a |
| 86 | + * dependency, so a change to its value invalidates this run's cache entry. |
| 87 | + * |
| 88 | + * Has no effect on `process.env`; the caller decides what to do with the |
| 89 | + * returned value. Returns `undefined` when not running inside a runner. |
| 90 | + * |
| 91 | + * @param {string} name |
| 92 | + * @param {{ tracked?: boolean }} [options] |
| 93 | + * @returns {string | undefined} |
| 94 | + */ |
| 95 | +export function getEnv(name, options) { |
| 96 | + const a = load(); |
| 97 | + if (!a) return undefined; |
| 98 | + return a.getEnv(name, options); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Ask the runner for every env whose name matches `pattern` (a glob, e.g. |
| 103 | + * `VITE_*`) and return the match-set as a plain object. |
| 104 | + * |
| 105 | + * With `tracked: true` (the default) the runner records the pattern as a |
| 106 | + * dependency, so adding, removing, or changing a matching env invalidates |
| 107 | + * this run's cache entry. |
| 108 | + * |
| 109 | + * Has no effect on `process.env`; the caller decides what to do with the |
| 110 | + * returned values. Returns an empty object when not running inside a runner. |
| 111 | + * |
| 112 | + * @param {string} pattern |
| 113 | + * @param {{ tracked?: boolean }} [options] |
| 114 | + * @returns {Record<string, string>} |
| 115 | + */ |
| 116 | +export function getEnvs(pattern, options) { |
| 117 | + const a = load(); |
| 118 | + if (!a) return {}; |
| 119 | + return a.getEnvs(pattern, options); |
| 120 | +} |
0 commit comments