@dokebi/template-inertia 0.4.21 → 0.4.23

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,166 @@
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 { readdir, readFile } from 'node:fs/promises';
17
+ import { createRequire } from 'node:module';
18
+ import { dirname, join, resolve } from 'node:path';
19
+ import { diagnostics } from '@dokebi/core/libraries';
20
+ const { describeError } = diagnostics;
21
+ /** TypeScript sources a type can come from. */
22
+ const TYPE_FILE = /\.(?:[cm]?tsx?)$/i;
23
+ /** `export * from '…'` and `export type * from '…'` (not `export * as name`, a named export). */
24
+ const EXPORT_ALL = /\bexport\s+(?:type\s+)?\*\s+from\s*(['"])([^'"\n]+)\1/g;
25
+ /** Extensions a re-export specifier may name (TypeScript's `.js` for `.ts` included). */
26
+ const SPECIFIER_EXTENSION = /\.(?:d\.[cm]?ts|[cm]?[jt]sx?)$/i;
27
+ const stem = (file) => file.replace(SPECIFIER_EXTENSION, '');
28
+ const posix = (path) => path.split('\\').join('/');
29
+ /** The re-exporting files of changed TypeScript sources, cleared in Vue's type cache. */
30
+ export class TypeBarrels {
31
+ #logger;
32
+ #root;
33
+ #invalidate;
34
+ constructor(logger, root, invalidate) {
35
+ this.#logger = logger;
36
+ this.#root = resolve(root);
37
+ this.#invalidate = invalidate;
38
+ }
39
+ /** vue/compiler-sfc's invalidateTypeCache, as @vitejs/plugin-vue resolves the compiler. */
40
+ #compiler() {
41
+ if (this.#invalidate !== undefined)
42
+ return this.#invalidate;
43
+ try {
44
+ const require = createRequire(join(this.#root, 'package.json'));
45
+ const version = require(require.resolve('vue/package.json', { paths: [this.#root] })).version;
46
+ const compiler = typeof version === 'string' && Number(version.split('.')[0]) >= 3
47
+ ? require(require.resolve('vue/compiler-sfc', { paths: [this.#root] }))
48
+ : undefined;
49
+ this.#invalidate =
50
+ typeof compiler?.invalidateTypeCache === 'function' ? compiler.invalidateTypeCache : null;
51
+ }
52
+ catch (error) {
53
+ this.#invalidate = null;
54
+ this.#logger.log('debug', 'Vue type barrels: no vue/compiler-sfc in the project', {
55
+ root: this.#root,
56
+ error: describeError(error),
57
+ });
58
+ }
59
+ return this.#invalidate;
60
+ }
61
+ /** A TypeScript file changed or went away: clears the type caches of the files re-exporting it. */
62
+ async changed(file) {
63
+ if (!TYPE_FILE.test(file))
64
+ return [];
65
+ const invalidate = this.#compiler();
66
+ if (!invalidate)
67
+ return [];
68
+ const changed = posix(resolve(file));
69
+ const cleared = [];
70
+ const seen = new Set([changed]);
71
+ const queue = [changed];
72
+ // `seen` ends a cycle of re-exports.
73
+ while (queue.length) {
74
+ const target = queue.shift();
75
+ for (const barrel of await this.#reexporters(target)) {
76
+ if (seen.has(barrel))
77
+ continue;
78
+ seen.add(barrel);
79
+ invalidate(barrel);
80
+ cleared.push(barrel);
81
+ queue.push(barrel);
82
+ }
83
+ }
84
+ if (cleared.length && this.#logger.isEnabled('debug'))
85
+ this.#logger.log('debug', 'Vue type caches of re-exporting files cleared', {
86
+ file: changed,
87
+ barrels: cleared,
88
+ });
89
+ return cleared;
90
+ }
91
+ /** Files in `target`'s folder and the folders above it (to its package root) that `export *` it. */
92
+ async #reexporters(target) {
93
+ const wanted = stem(target);
94
+ const found = [];
95
+ for (let dir = dirname(target);; dir = dirname(dir)) {
96
+ let names;
97
+ try {
98
+ names = await readdir(dir);
99
+ }
100
+ catch (error) {
101
+ this.#logger.log('debug', 'Vue type barrels: a folder could not be read', {
102
+ dir,
103
+ error: describeError(error),
104
+ });
105
+ break;
106
+ }
107
+ for (const name of names) {
108
+ if (!TYPE_FILE.test(name))
109
+ continue;
110
+ const candidate = posix(join(dir, name));
111
+ if (candidate === target)
112
+ continue;
113
+ let text;
114
+ try {
115
+ text = await readFile(candidate, 'utf8');
116
+ }
117
+ catch (error) {
118
+ this.#logger.log('debug', 'Vue type barrels: a file could not be read', {
119
+ file: candidate,
120
+ error: describeError(error),
121
+ });
122
+ continue;
123
+ }
124
+ if (!text.includes('*'))
125
+ continue;
126
+ for (const match of text.matchAll(EXPORT_ALL)) {
127
+ const specifier = match[2];
128
+ if (!specifier.startsWith('.'))
129
+ continue;
130
+ const named = stem(posix(resolve(dir, specifier)));
131
+ if (named === wanted || `${named}/index` === wanted) {
132
+ found.push(candidate);
133
+ break;
134
+ }
135
+ }
136
+ }
137
+ // The package root (or the project root) is the last folder a barrel of this file sits in.
138
+ if (names.includes('package.json') || posix(dir) === posix(this.#root))
139
+ break;
140
+ if (dirname(dir) === dir)
141
+ break;
142
+ }
143
+ return found;
144
+ }
145
+ }
146
+ /** The Vite plugin: clears the barrels before plugin-vue recompiles the components (`enforce: 'pre'`). */
147
+ export function vueTypeBarrels(logger, root, invalidate) {
148
+ const barrels = new TypeBarrels(logger, root, invalidate);
149
+ const clear = (file) => barrels.changed(file).then(() => undefined, (error) => {
150
+ logger.log('error', 'Vue type barrels could not be cleared', {
151
+ code: 'INERTIA_VUE_TYPE_BARRELS_FAILED',
152
+ file,
153
+ error: describeError(error),
154
+ });
155
+ });
156
+ return {
157
+ name: 'dokebi-inertia:vue-type-barrels',
158
+ enforce: 'pre',
159
+ apply: 'serve',
160
+ configureServer(server) {
161
+ server.watcher?.on('unlink', (file) => void clear(file));
162
+ },
163
+ // Awaited by Vite before the next plugin's handleHotUpdate (plugin-vue's).
164
+ handleHotUpdate: (context) => clear(context.file),
165
+ };
166
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.21",
3
+ "version": "0.4.23",
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",
@@ -87,6 +87,11 @@
87
87
  "name": "dokebi-session",
88
88
  "version": "^0.4.0",
89
89
  "optional": true
90
+ },
91
+ {
92
+ "name": "dokebi-i18n",
93
+ "version": "^0.2.0",
94
+ "optional": true
90
95
  }
91
96
  ],
92
97
  "contributes": [
@@ -147,7 +152,7 @@
147
152
  ]
148
153
  },
149
154
  "devDependencies": {
150
- "@dokebi/i18n": "0.2.9",
155
+ "@dokebi/i18n": "0.2.10",
151
156
  "@dokebi/session": "0.4.11",
152
157
  "@dokebi/web": "0.4.27",
153
158
  "@dokebi/websocket": "0.4.10",