@dokebi/template-inertia 0.4.22 → 0.4.24

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.
@@ -0,0 +1,224 @@
1
+ // D-0292: Vue's type cache of a file that re-exports a changed one (`export * from './event'`) is
2
+ // cleared with it. `defineProps<X>()` resolves X through @vue/compiler-sfc's type scopes, cached
3
+ // per file. For `export *` the compiler copies the source file's exported types into the
4
+ // re-exporting file's scope; @vitejs/plugin-vue clears only the changed file's cache. So a new type
5
+ // in an existing file that a barrel re-exports stayed unknown to the dev compiler
6
+ // (`Unresolvable type reference`) until the barrel itself changed or dev restarted, while vue-tsc,
7
+ // the build and the tests passed (shop dev, 2026-11-29, @vue/compiler-sfc 3.5.44, plugin-vue
8
+ // 6.0.10).
9
+ //
10
+ // On a change or removal of a TypeScript file this plugin finds the files that `export *` (or
11
+ // `export type *`) from it — in the file's own folder and each folder above it up to its package
12
+ // root (the nearest package.json), where barrels sit — and from those, the same way, the files that
13
+ // re-export them; it clears their type caches through the compiler plugin-vue uses (vue/compiler-sfc
14
+ // resolved from the project root, as plugin-vue resolves it). plugin-vue then recompiles the
15
+ // components that depend on the changed file, as before, and they see the new type.
16
+ import { readFileSync, realpathSync, statSync } from 'node:fs';
17
+ import { readdir, readFile } from 'node:fs/promises';
18
+ import { createRequire } from 'node:module';
19
+ import { dirname, join, resolve } from 'node:path';
20
+ import { diagnostics } from '@dokebi/core/libraries';
21
+ const { describeError } = diagnostics;
22
+ /** TypeScript sources a type can come from. */
23
+ const TYPE_FILE = /\.(?:[cm]?tsx?)$/i;
24
+ /** `export * from '…'` and `export type * from '…'` (not `export * as name`, a named export). */
25
+ const EXPORT_ALL = /\bexport\s+(?:type\s+)?\*\s+from\s*(['"])([^'"\n]+)\1/g;
26
+ /** Extensions a re-export specifier may name (TypeScript's `.js` for `.ts` included). */
27
+ const SPECIFIER_EXTENSION = /\.(?:d\.[cm]?ts|[cm]?[jt]sx?)$/i;
28
+ const stem = (file) => file.replace(SPECIFIER_EXTENSION, '');
29
+ const posix = (path) => path.split('\\').join('/');
30
+ /** The re-exporting files of changed TypeScript sources, cleared in Vue's type cache. */
31
+ export class TypeBarrels {
32
+ #logger;
33
+ #root;
34
+ #invalidate;
35
+ constructor(logger, root, invalidate) {
36
+ this.#logger = logger;
37
+ this.#root = resolve(root);
38
+ this.#invalidate = invalidate;
39
+ }
40
+ /** vue/compiler-sfc's invalidateTypeCache, as @vitejs/plugin-vue resolves the compiler. */
41
+ #compiler() {
42
+ if (this.#invalidate !== undefined)
43
+ return this.#invalidate;
44
+ try {
45
+ const require = createRequire(join(this.#root, 'package.json'));
46
+ const version = require(require.resolve('vue/package.json', { paths: [this.#root] })).version;
47
+ const compiler = typeof version === 'string' && Number(version.split('.')[0]) >= 3
48
+ ? require(require.resolve('vue/compiler-sfc', { paths: [this.#root] }))
49
+ : undefined;
50
+ this.#invalidate =
51
+ typeof compiler?.invalidateTypeCache === 'function' ? compiler.invalidateTypeCache : null;
52
+ }
53
+ catch (error) {
54
+ this.#invalidate = null;
55
+ this.#logger.log('debug', 'Vue type barrels: no vue/compiler-sfc in the project', {
56
+ root: this.#root,
57
+ error: describeError(error),
58
+ });
59
+ }
60
+ return this.#invalidate;
61
+ }
62
+ /** A TypeScript file changed or went away: clears the type caches of the files re-exporting it. */
63
+ async changed(file) {
64
+ if (!TYPE_FILE.test(file))
65
+ return [];
66
+ const invalidate = this.#compiler();
67
+ if (!invalidate)
68
+ return [];
69
+ const changed = posix(resolve(file));
70
+ const cleared = [];
71
+ const seen = new Set([changed]);
72
+ const queue = [changed];
73
+ // `seen` ends a cycle of re-exports.
74
+ while (queue.length) {
75
+ const target = queue.shift();
76
+ for (const barrel of await this.#reexporters(target)) {
77
+ if (seen.has(barrel))
78
+ continue;
79
+ seen.add(barrel);
80
+ invalidate(barrel);
81
+ cleared.push(barrel);
82
+ queue.push(barrel);
83
+ }
84
+ }
85
+ if (cleared.length && this.#logger.isEnabled('debug'))
86
+ this.#logger.log('debug', 'Vue type caches of re-exporting files cleared', {
87
+ file: changed,
88
+ barrels: cleared,
89
+ });
90
+ return cleared;
91
+ }
92
+ /** Files in `target`'s folder and the folders above it (to its package root) that `export *` it. */
93
+ async #reexporters(target) {
94
+ const wanted = stem(target);
95
+ const found = [];
96
+ for (let dir = dirname(target);; dir = dirname(dir)) {
97
+ let names;
98
+ try {
99
+ names = await readdir(dir);
100
+ }
101
+ catch (error) {
102
+ this.#logger.log('debug', 'Vue type barrels: a folder could not be read', {
103
+ dir,
104
+ error: describeError(error),
105
+ });
106
+ break;
107
+ }
108
+ for (const name of names) {
109
+ if (!TYPE_FILE.test(name))
110
+ continue;
111
+ const candidate = posix(join(dir, name));
112
+ if (candidate === target)
113
+ continue;
114
+ let text;
115
+ try {
116
+ text = await readFile(candidate, 'utf8');
117
+ }
118
+ catch (error) {
119
+ this.#logger.log('debug', 'Vue type barrels: a file could not be read', {
120
+ file: candidate,
121
+ error: describeError(error),
122
+ });
123
+ continue;
124
+ }
125
+ if (!text.includes('*'))
126
+ continue;
127
+ for (const match of text.matchAll(EXPORT_ALL)) {
128
+ const specifier = match[2];
129
+ if (!specifier.startsWith('.'))
130
+ continue;
131
+ const named = stem(posix(resolve(dir, specifier)));
132
+ if (named === wanted || `${named}/index` === wanted) {
133
+ found.push(candidate);
134
+ break;
135
+ }
136
+ }
137
+ }
138
+ // The package root (or the project root) is the last folder a barrel of this file sits in.
139
+ if (names.includes('package.json') || posix(dir) === posix(this.#root))
140
+ break;
141
+ if (dirname(dir) === dir)
142
+ break;
143
+ }
144
+ return found;
145
+ }
146
+ }
147
+ /** What TypeScript's `ts.sys` gives the compiler, on node:fs (no TypeScript needed). */
148
+ const NODE_FS = {
149
+ fileExists: (file) => statSync(file, { throwIfNoEntry: false })?.isFile() === true,
150
+ readFile: (file) => {
151
+ try {
152
+ return readFileSync(file, 'utf8');
153
+ }
154
+ catch (error) {
155
+ // As ts.sys.readFile: a file that cannot be read is undefined, and the compiler says which.
156
+ if (error.code === 'ENOENT')
157
+ return undefined;
158
+ throw error;
159
+ }
160
+ },
161
+ realpath: (file) => realpathSync.native(file),
162
+ };
163
+ /**
164
+ * D-0292: the type files the compiler reads, told to `onTypeFile` (the Vite host records them for
165
+ * its comparison with the disk, D-0283 / D-0289, which before saw only the modules Vite loads). The
166
+ * compiler reads them through `script.fs` (its own, or TypeScript's `ts.sys`); the wrapper passes
167
+ * every call on to that, or to node:fs with the same answers when there is none.
168
+ */
169
+ export function recordingFileSystem(onTypeFile, base = NODE_FS) {
170
+ const realpath = base.realpath;
171
+ return {
172
+ fileExists: (file) => base.fileExists(file),
173
+ readFile: (file) => {
174
+ const text = base.readFile(file);
175
+ if (text !== undefined && !posix(file).includes('/node_modules/'))
176
+ onTypeFile(file);
177
+ return text;
178
+ },
179
+ // Only when the base has one: TypeScript's module resolution treats links by it.
180
+ ...(realpath ? { realpath: (file) => realpath.call(base, file) } : {}),
181
+ };
182
+ }
183
+ /** The Vite plugin: clears the barrels before plugin-vue recompiles the components (`enforce: 'pre'`). */
184
+ export function vueTypeBarrels(logger, root, options = {}) {
185
+ const barrels = new TypeBarrels(logger, root, options.invalidate);
186
+ const clear = (file) => barrels.changed(file).then(() => undefined, (error) => {
187
+ logger.log('error', 'Vue type barrels could not be cleared', {
188
+ code: 'INERTIA_VUE_TYPE_BARRELS_FAILED',
189
+ file,
190
+ error: describeError(error),
191
+ });
192
+ });
193
+ return {
194
+ name: 'dokebi-inertia:vue-type-barrels',
195
+ enforce: 'pre',
196
+ apply: 'serve',
197
+ configureServer(server) {
198
+ server.watcher?.on('unlink', (file) => void clear(file));
199
+ const onTypeFile = options.onTypeFile;
200
+ if (!onTypeFile)
201
+ return;
202
+ // After every configResolved (plugin-vue sets its options there): its public api.options.
203
+ const vue = server.config?.plugins?.find((plugin) => plugin?.name === 'vite:vue' &&
204
+ plugin.api !== undefined);
205
+ if (!vue?.api) {
206
+ logger.log('debug', 'Vue type files are not recorded: no @vitejs/plugin-vue with an api');
207
+ return;
208
+ }
209
+ const current = vue.api.options;
210
+ vue.api.options = {
211
+ ...current,
212
+ script: {
213
+ ...current.script,
214
+ fs: recordingFileSystem(onTypeFile, current.script?.fs ?? NODE_FS),
215
+ },
216
+ };
217
+ logger.log('debug', 'Vue type files the compiler reads are recorded for the comparison', {
218
+ own: current.script?.fs !== undefined,
219
+ });
220
+ },
221
+ // Awaited by Vite before the next plugin's handleHotUpdate (plugin-vue's).
222
+ handleHotUpdate: (context) => clear(context.file),
223
+ };
224
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.22",
3
+ "version": "0.4.24",
4
4
  "description": "Inertia v3 server adapter for Dokebi: page JSON, Vite manifest assets and optional server-side rendering.",
5
5
  "keywords": [
6
6
  "dokebi",