@pentacore/vite-plugin-laravel-typefinder 8.5.0 → 8.7.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 CHANGED
@@ -57,11 +57,13 @@ All options are optional. Defaults work for a standard Laravel project with `php
57
57
  ```ts
58
58
  typefinder({
59
59
  // Shell command for the long-lived watcher process.
60
- // Override for Sail/Herd/Docker.
60
+ // Override for Sail/Herd/Docker. Set to `false` to run no watcher
61
+ // and no regeneration in `vite dev`.
61
62
  command: 'php artisan typefinder:watch',
62
63
 
63
64
  // Shell command used in `vite build` (one-shot, no watcher).
64
- // Set to `false` to skip generation entirely in build mode.
65
+ // Set to `false` to skip generation entirely in build mode
66
+ // (the TYPEFINDER_SKIP_BUILD environment variable does the same).
65
67
  buildCommand: 'php artisan typefinder:generate --json',
66
68
 
67
69
  // Override the paths the plugin watches. When unset (the default),
@@ -133,9 +135,23 @@ The watched paths are read from the watcher's startup handshake, which mirrors y
133
135
 
134
136
  Creating, editing or deleting a migration triggers a full regeneration. The schema itself changes only when the migration runs, and the watcher notices that on the next regeneration: after `php artisan migrate`, save any watched file (or send the `typefinder:full-regen` event) to pick up the new columns.
135
137
 
138
+ **Vue:** Vue's SFC compiler caches the types it resolves for `defineProps<PageProps['Users/Show']>()`, and the generated files are not in Vite's module graph, so nothing would tell it they changed: new props would compile to no props until the dev server restarts. When the project has `vue` installed, the plugin clears that cache for every file a regeneration changed, invalidates the `.vue` modules already compiled and reloads the page. Without Vue this does nothing.
139
+
136
140
  Warnings and per-file failures of every regeneration, the initial one included, are written to stderr. A regeneration that gets no answer within `regenTimeoutMs` is reported as failed and the next one is sent.
137
141
 
138
- **Production (`vite build`):** No watcher. The plugin runs `typefinder:generate --json` once in `buildStart`, prints its warnings, and fails the build on non-zero exit with the generator's error. Set `buildCommand: false` to skip generation entirely (e.g. for pre-built containers without a PHP runtime).
142
+ **Production (`vite build`):** No watcher. The plugin runs `typefinder:generate --json` once in `buildStart`, prints its warnings, and fails the build on non-zero exit with the generator's error. Set `buildCommand: false`, or the `TYPEFINDER_SKIP_BUILD` environment variable, to skip generation entirely.
143
+
144
+ **Vitest:** Vitest starts Vite in serve mode, but the plugin does nothing while `VITEST` is set: it spawns no watcher and regenerates nothing, so a test run never rewrites the generated types and needs neither PHP nor a database. The plugin can stay in a `vite.config.ts` that Vitest shares.
145
+
146
+ ### Builds without a database (Docker, CI)
147
+
148
+ Generating reads the database schema, so a build without a database (a Docker image build, a frontend-only CI job) cannot run it. Commit the generated types and skip build-time generation there: set `TYPEFINDER_SKIP_BUILD` to any value except empty, `0` or `false`, and the plugin uses the committed files as they are.
149
+
150
+ ```dockerfile
151
+ RUN npm ci && TYPEFINDER_SKIP_BUILD=1 npm run build
152
+ ```
153
+
154
+ Keep a check that the committed types are current where the database is available, e.g. `php artisan typefinder:generate --check` in the test job.
139
155
 
140
156
  **Shutdown:** The watcher is killed on `closeBundle` and on process exit. A PHP fatal error or out-of-memory while regenerating ends only the worker of that request: the plugin logs the error and the watcher keeps running. If the watcher itself dies mid-session, the plugin logs the error and stops regenerating — restart the dev server to recover.
141
157
 
package/dist/index.js CHANGED
@@ -1,52 +1,52 @@
1
1
  import { exec as e, spawn as t } from "node:child_process";
2
- import { promisify as n } from "node:util";
3
- import { realpathSync as r } from "node:fs";
4
- import { basename as i, dirname as a } from "node:path";
5
- import { EventEmitter as o } from "node:events";
6
- // src/matcher.ts
7
- var s = (e) => {
2
+ import { basename as n, dirname as r, isAbsolute as i, join as a, resolve as o } from "node:path";
3
+ import { promisify as s } from "node:util";
4
+ import { realpathSync as c } from "node:fs";
5
+ import { EventEmitter as l } from "node:events";
6
+ // \0rolldown/runtime.js
7
+ var u = (e, t) => () => (t || (e((t = { exports: {} }).exports, t, t.exports), e = null), t.exports), d = (e) => {
8
8
  let t = e.replace(/\\/g, "/"), n = t.length;
9
9
  for (; n > 0 && t[n - 1] === "/";) n--;
10
10
  return t.slice(0, n);
11
- }, c = (e) => {
12
- let t = e.map(s).filter((e) => e !== "").map((e) => `${e}/`);
11
+ }, f = (e) => {
12
+ let t = e.map(d).filter((e) => e !== "").map((e) => `${e}/`);
13
13
  return t.length === 0 ? () => !1 : (e) => {
14
14
  let n = e.replace(/\\/g, "/");
15
15
  return n.endsWith(".php") ? t.some((e) => n.startsWith(e)) : !1;
16
16
  };
17
- }, l = (e) => {
18
- let t = [], n = e;
17
+ }, p = (e) => {
18
+ let t = [], i = e;
19
19
  for (;;) try {
20
- let e = s(r.native(n));
20
+ let e = d(c.native(i));
21
21
  return t.length === 0 ? e : `${e}/${t.reverse().join("/")}`;
22
22
  } catch {
23
- let r = a(n);
24
- if (r === n) return s(e);
25
- t.push(i(n)), n = r;
23
+ let a = r(i);
24
+ if (a === i) return d(e);
25
+ t.push(n(i)), i = a;
26
26
  }
27
- }, u = (e, t, n) => e === t ? n : e.startsWith(`${t}/`) ? n + e.slice(t.length) : null, d = class {
27
+ }, m = (e, t, n) => e === t ? n : e.startsWith(`${t}/`) ? n + e.slice(t.length) : null, h = class {
28
28
  entries;
29
29
  constructor(e = {}) {
30
30
  this.entries = Object.entries(e).map(([e, t]) => ({
31
- remote: s(e),
32
- local: l(t)
31
+ remote: d(e),
32
+ local: p(t)
33
33
  }));
34
34
  }
35
35
  toHost(e) {
36
- return this.translate(s(e), "remote", "local");
36
+ return this.translate(d(e), "remote", "local");
37
37
  }
38
38
  toWatcher(e) {
39
- return this.translate(s(e), "local", "remote");
39
+ return this.translate(d(e), "local", "remote");
40
40
  }
41
41
  translate(e, t, n) {
42
42
  let r = [...this.entries].sort((e, n) => n[t].length - e[t].length);
43
43
  for (let i of r) {
44
- let r = u(e, i[t], i[n]);
44
+ let r = m(e, i[t], i[n]);
45
45
  if (r !== null) return r;
46
46
  }
47
47
  return e;
48
48
  }
49
- }, f = class e extends o {
49
+ }, g = class e extends l {
50
50
  handshake;
51
51
  child;
52
52
  killTimeoutMs;
@@ -173,13 +173,28 @@ var s = (e) => {
173
173
  }), this.child.kill("SIGTERM");
174
174
  });
175
175
  }
176
- }, p = n(e), m = (e) => {
176
+ }, _ = (/* @__PURE__ */ u(((e, t) => {
177
+ t.exports = {};
178
+ })))(), v = (e) => {
179
+ try {
180
+ let t = (0, _.createRequire)(a(e, "package.json"))("vue/compiler-sfc").invalidateTypeCache;
181
+ return typeof t == "function" ? (e) => void t(e) : null;
182
+ } catch {
183
+ return null;
184
+ }
185
+ }, y = s(e), b = () => {
186
+ let e = process.env.TYPEFINDER_SKIP_BUILD?.trim().toLowerCase();
187
+ return e !== void 0 && e !== "" && e !== "0" && e !== "false";
188
+ }, x = () => {
189
+ let e = process.env.VITEST?.trim().toLowerCase();
190
+ return e !== void 0 && e !== "" && e !== "0" && e !== "false";
191
+ }, S = (e) => {
177
192
  process.stderr.write(`[typefinder] ${e}\n`);
178
- }, h = (e, t) => {
179
- e.changed.length > 0 && m(`${t} ${e.changed.length} file(s) in ${e.duration_ms}ms`);
180
- for (let t of e.warnings) m(t);
181
- for (let t of e.failed) m(`${t.path}: ${t.message}`);
182
- }, g = (e) => {
193
+ }, C = (e, t) => {
194
+ e.changed.length > 0 && S(`${t} ${e.changed.length} file(s) in ${e.duration_ms}ms`);
195
+ for (let t of e.warnings) S(t);
196
+ for (let t of e.failed) S(`${t.path}: ${t.message}`);
197
+ }, w = (e) => {
183
198
  if (typeof e != "string") return null;
184
199
  let t = e.split("\n").map((e) => e.trim()).filter((e) => e !== "");
185
200
  for (let e = t.length - 1; e >= 0; e--) try {
@@ -187,64 +202,95 @@ var s = (e) => {
187
202
  if (typeof n == "object" && n && "success" in n) return n;
188
203
  } catch {}
189
204
  return null;
190
- }, _ = ({ command: e = "php artisan typefinder:watch", buildCommand: t = "php artisan typefinder:generate --json", watch: n, debounceMs: r = 100, startupTimeoutMs: i = 1e4, killTimeoutMs: a = 2e3, regenTimeoutMs: o = 12e4, pathMap: s, migrations: u } = {}) => {
191
- let _ = process.cwd(), y = null, b = () => !1, x = () => !1, S = !1, C = new d(), w = /* @__PURE__ */ new Set(), T, E = !1, D = () => {
192
- clearTimeout(T), T = setTimeout(() => {
193
- if (!y || y.dead) return;
194
- let e = S ? [] : Array.from(w), t = S ? "full regen" : "regen";
195
- w.clear(), S = !1, y.regen(e).then((e) => h(e, t)).catch((e) => {
196
- m(`regen failed: ${e.message}`);
205
+ }, T = ({ command: e = "php artisan typefinder:watch", buildCommand: t = "php artisan typefinder:generate --json", watch: n, debounceMs: r = 100, startupTimeoutMs: a = 1e4, killTimeoutMs: s = 2e3, regenTimeoutMs: c = 12e4, pathMap: l, migrations: u } = {}) => {
206
+ let d = process.cwd(), m = null, _ = () => !1, T = () => !1, O = !1, k = new h(), A = /* @__PURE__ */ new Set(), j, M = !1, N = () => {}, P = () => {
207
+ clearTimeout(j), j = setTimeout(() => {
208
+ if (!m || m.dead) return;
209
+ let e = O ? [] : Array.from(A), t = O ? "full regen" : "regen";
210
+ A.clear(), O = !1, m.regen(e).then((e) => {
211
+ C(e, t), N(e);
212
+ }).catch((e) => {
213
+ S(`regen failed: ${e.message}`);
197
214
  });
198
215
  }, r);
199
- }, O = (e) => {
200
- if (E || !y || y.dead) return;
201
- let t = l(e);
202
- x(t) ? (S = !0, D()) : b(t) && (w.add(C.toWatcher(t)), D());
216
+ }, F = (e) => {
217
+ if (M || !m || m.dead) return;
218
+ let t = p(e);
219
+ T(t) ? (O = !0, P()) : _(t) && (A.add(k.toWatcher(t)), P());
203
220
  };
204
221
  return {
205
222
  name: "@pentacore/vite-plugin-laravel-typefinder",
206
223
  enforce: "pre",
207
224
  configResolved(e) {
208
- _ = e.root, E = e.command === "build";
225
+ d = e.root, M = e.command === "build";
209
226
  },
210
227
  async configureServer(t) {
211
- E || (C = new d(s), y = await f.spawn({
228
+ if (M || x()) return;
229
+ if (e === !1) {
230
+ S("dev watcher disabled");
231
+ return;
232
+ }
233
+ k = new h(l), m = await g.spawn({
212
234
  command: e,
213
- cwd: _,
214
- startupTimeoutMs: i,
215
- killTimeoutMs: a,
216
- regenTimeoutMs: o
217
- }), b = c((n ?? v(y.handshake.categories).map((e) => C.toHost(e))).map(l)), x = c((u === !1 ? [] : u ?? [`${_}/database/migrations`]).map(l)), y.on("died", (e) => {
218
- m(`watcher exited: ${e} — restart the dev server`);
219
- }), t.watcher.on("add", O), t.watcher.on("change", O), t.watcher.on("unlink", O), t.ws.on("typefinder:full-regen", () => {
220
- y && !y.dead && y.regen([]).then((e) => h(e, "full regen")).catch((e) => {
221
- m(`full regen failed: ${e.message}`);
235
+ cwd: d,
236
+ startupTimeoutMs: a,
237
+ killTimeoutMs: s,
238
+ regenTimeoutMs: c
239
+ }), _ = f((n ?? D(m.handshake.categories).map((e) => k.toHost(e))).map(p)), T = f((u === !1 ? [] : u ?? [`${d}/database/migrations`]).map(p));
240
+ let r = k.toHost(m.handshake.output_path);
241
+ N = E(t, v(d), i(r) ? r : o(d, r)), m.on("died", (e) => {
242
+ S(`watcher exited: ${e} — restart the dev server`);
243
+ }), t.watcher.on("add", F), t.watcher.on("change", F), t.watcher.on("unlink", F), t.ws.on("typefinder:full-regen", () => {
244
+ m && !m.dead && m.regen([]).then((e) => {
245
+ C(e, "full regen"), N(e);
246
+ }).catch((e) => {
247
+ S(`full regen failed: ${e.message}`);
222
248
  });
223
- }), t.httpServer?.once("close", () => void y?.kill()), process.once("exit", () => void y?.kill()));
249
+ }), t.httpServer?.once("close", () => void m?.kill()), process.once("exit", () => void m?.kill());
224
250
  },
225
251
  async buildStart() {
226
- if (E) {
252
+ if (M) {
227
253
  if (t === !1) {
228
- m("build-time generation disabled");
254
+ S("build-time generation disabled");
255
+ return;
256
+ }
257
+ if (b()) {
258
+ S("build-time generation skipped: TYPEFINDER_SKIP_BUILD is set");
229
259
  return;
230
260
  }
231
261
  let e;
232
262
  try {
233
- ({stdout: e} = await p(t, { cwd: _ }));
263
+ ({stdout: e} = await y(t, { cwd: d }));
234
264
  } catch (e) {
235
- let t = g(e.stdout);
236
- for (let e of t?.warnings ?? []) m(e);
265
+ let t = w(e.stdout);
266
+ for (let e of t?.warnings ?? []) S(e);
237
267
  let n = t?.errors ?? [], r = n.length > 0 ? n.join("\n") : e instanceof Error ? e.message : String(e);
238
268
  throw Error(`[typefinder] build-time generation failed: ${r}`, { cause: e });
239
269
  }
240
- for (let t of g(e)?.warnings ?? []) m(t);
241
- } else y && !y.dead && h(await y.regen([]), "initial regen:");
270
+ for (let t of w(e)?.warnings ?? []) S(t);
271
+ } else if (m && !m.dead) {
272
+ let e = await m.regen([]);
273
+ C(e, "initial regen:"), N(e);
274
+ }
242
275
  },
243
276
  async closeBundle() {
244
- y && !y.dead && await y.kill();
277
+ m && !m.dead && await m.kill();
245
278
  }
246
279
  };
247
- }, v = (e) => {
280
+ }, E = (e, t, n) => t === null ? () => {} : (r) => {
281
+ if (r.changed.length === 0) return;
282
+ for (let e of r.changed) {
283
+ let r = a(n, e);
284
+ t(r);
285
+ let i = p(r);
286
+ i !== r && t(i);
287
+ }
288
+ let i = [...e.moduleGraph.idToModuleMap.values()].filter((e) => e.file?.endsWith(".vue"));
289
+ if (i.length !== 0) {
290
+ for (let t of i) e.moduleGraph.invalidateModule(t);
291
+ e.ws.send({ type: "full-reload" });
292
+ }
293
+ }, D = (e) => {
248
294
  let t = [];
249
295
  for (let n of Object.keys(e)) {
250
296
  let r = e[n];
@@ -252,4 +298,4 @@ var s = (e) => {
252
298
  }
253
299
  return t;
254
300
  };
255
- export { _ as default, _ as typefinder };
301
+ export { T as default, T as typefinder };
@@ -1,6 +1,10 @@
1
1
  import { Plugin } from 'vite';
2
2
  export interface TypefinderOptions {
3
- command?: string;
3
+ /**
4
+ * Shell command for the long-lived watcher process in `vite dev`;
5
+ * `false` disables the watcher and the initial regen there.
6
+ */
7
+ command?: string | false;
4
8
  buildCommand?: string | false;
5
9
  watch?: string[];
6
10
  debounceMs?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"vite-plugin-typefinder.d.ts","sourceRoot":"","sources":["../src/vite-plugin-typefinder.ts"],"names":[],"mappings":"AAE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,EAAE,GAAG,KAAK,CAAC;CACjC;AAgED,eAAO,MAAM,UAAU,GAAI,sHAUxB,iBAAsB,KAAG,MAiK3B,CAAC;AAkDF,eAAe,UAAU,CAAC"}
package/dist/vue.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Vue's `invalidateTypeCache()` from the project's own `vue/compiler-sfc`,
3
+ * or null when Vue is not installed. Loaded the way `@vitejs/plugin-vue`
4
+ * loads the compiler (a `require` resolved from the Vite root), so it clears
5
+ * the cache that plugin compiles with.
6
+ */
7
+ export declare const loadVueTypeCacheInvalidator: (root: string) => ((file: string) => void) | null;
8
+ //# sourceMappingURL=vue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vue.d.ts","sourceRoot":"","sources":["../src/vue.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,eAAO,MAAM,2BAA2B,GAAI,MAAM,MAAM,KAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,IAUrF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pentacore/vite-plugin-laravel-typefinder",
3
- "version": "8.5.0",
3
+ "version": "8.7.0",
4
4
  "description": "Vite plugin for Laravel Typefinder — auto-generate TypeScript types from Models, Enums, Casts, and Requests.",
5
5
  "keywords": [
6
6
  "vite",