@dokebi/template-inertia 0.4.9 → 0.4.10

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/AI.md CHANGED
@@ -242,7 +242,7 @@ A module keeps its Vue pages in its own views folder (@dokebi/module AI.md §4a)
242
242
  - The module text is one static import per page file and `export default modulePages({ <module>: { '<template>/<path>': page } })`. Left out: pages of sets the app did not choose (`Pages/<set>/**`), and a page the app overrides in `views/default/Pages/<module>/` (the app's file wins for every template). At most 2000 page files per app (warn `INERTIA_MODULE_PAGES_LIMIT`).
243
243
  - `modulePages(table)` of `@dokebi/template-inertia/pages` (no imports, browser and SSR bundles) gives `{ modules, page(template, name) }`: the module's page of `template`, else of `default`, else undefined. The entries look up the app's own page first: `` pick(`Pages/${name}`) ?? modulePages.page(template, name) ``.
244
244
  - Types: `/// <reference types="@dokebi/template-inertia/module-pages" />` in `src/infra/inertia/page.d.ts` (the generated file has it) declares the virtual module for vue-tsc and `dokebi check`, whose browser program now reads the project's `.d.ts` files too.
245
- - The dev server serves each module's views folder like an app's (§5.2), and reloads the browser when a `.vue` file is added to or removed from a module folder or the app's views, or the app file changes.
245
+ - The dev server serves each module's views folder like an app's (§5.2), and reloads the browser when a `.vue` file is added to or removed from a module folder or the app's views, or the app file changes. The dev server reads an app's module pages in a worker thread of its own (0.4.10, D-0230), so the app file and everything it imports — the module list the CLI writes (`registeredModules`, D-0227), the modules' definitions — are read as they are now; a module added with `npx dokebi module add` while `dokebi dev` runs is served (its pages in the table, its folder allowed) once the list or the app file changes, without a restart. A change of the module list file or of a mounted module's `module.ts` reads the app again too.
246
246
  - `vite.ssr.config.ts` needs `dokebiInertia()` too (the SSR build resolves the same import; server rendering still leaves the seal imports to Node). `npx dokebi doctor` names an entry without the import while a module under `src/` has Vue pages (`TEMPLATE_FIX_MISSING`, `module-pages`) and a Vite config without `dokebiInertia()` while an entry imports it (`module-pages-plugin`).
247
247
  - An app package without the sources (before @dokebi/app 0.4.9): no module pages, warn `INERTIA_MODULE_PAGES_UNSUPPORTED`. Failures are coded errors of the build: `MODULE_PAGES_APP_MISSING` (imported outside an app's views folder), `MODULE_PAGES_FAILED` (the app file cannot be imported, exports no definition, or a module folder cannot be read; the cause is kept).
248
248
  - Debug logs: `Inertia module pages gathered` (app, page count per module, skipped sets, ms), `Inertia module pages changed; the browser reloads` (event, path), `Inertia dev file policy allows a module views folder`.
package/dist/index.js CHANGED
@@ -1036,7 +1036,7 @@ class InertiaRuntime {
1036
1036
  }
1037
1037
  export const createPlugin = definePlugin({
1038
1038
  name: 'dokebi-template-inertia',
1039
- version: '0.4.9',
1039
+ version: '0.4.10',
1040
1040
  core: '^0.4.2',
1041
1041
  options,
1042
1042
  entry: parseEntry,
@@ -0,0 +1,12 @@
1
+ /** What the worker answers: the sources as @dokebi/app lists them, or why it could not. */
2
+ export type ModulePagesAnswer = {
3
+ readonly kind: 'sources';
4
+ readonly sources: unknown;
5
+ } | {
6
+ readonly kind: 'unsupported';
7
+ } | {
8
+ readonly kind: 'no-definition';
9
+ } | {
10
+ readonly kind: 'error';
11
+ readonly error: string;
12
+ };
@@ -0,0 +1,25 @@
1
+ // D-0218, dev server: reads one app's module page sources in a worker thread of its own. A worker
2
+ // has its own module cache, so the app file and everything it imports (the module list the CLI
3
+ // writes, the modules' own definitions) are loaded as they are now; the Vite host lives for the
4
+ // whole `dokebi dev` session, and its own cache would keep the modules of its first read (a module
5
+ // added while it ran stayed out of the page table and its folder answered 403).
6
+ import { parentPort, workerData } from 'node:worker_threads';
7
+ import { diagnostics } from '@dokebi/core/libraries';
8
+ const PAGES_KEY = Symbol.for('dokebi.app.modulePages');
9
+ async function read(url) {
10
+ try {
11
+ const definition = (await import(url)).default;
12
+ if (definition === null || typeof definition !== 'object')
13
+ return { kind: 'no-definition' };
14
+ const list = definition[PAGES_KEY];
15
+ if (typeof list !== 'function')
16
+ return { kind: 'unsupported' };
17
+ // Plain data (module names, folders, set names): it crosses to the main thread as it is.
18
+ return { kind: 'sources', sources: list() };
19
+ }
20
+ catch (error) {
21
+ return { kind: 'error', error: diagnostics.describeError(error) };
22
+ }
23
+ }
24
+ const { url } = workerData;
25
+ parentPort?.postMessage(await read(url));
@@ -10,6 +10,7 @@ import { existsSync } from 'node:fs';
10
10
  import { readdir } from 'node:fs/promises';
11
11
  import { basename, dirname, join, relative, sep } from 'node:path';
12
12
  import { pathToFileURL } from 'node:url';
13
+ import { Worker } from 'node:worker_threads';
13
14
  import { InertiaError } from './errors.js';
14
15
  /** The specifier views/app.js and views/ssr.js import. */
15
16
  export const MODULE_PAGES_ID = 'virtual:dokebi/module-pages';
@@ -69,6 +70,58 @@ async function pageFiles(source, appViews) {
69
70
  }
70
71
  return found;
71
72
  }
73
+ /** The build: the app file in this process (one read per build). */
74
+ async function readHere(app) {
75
+ let definition;
76
+ try {
77
+ definition = (await import(pathToFileURL(app).href)).default;
78
+ }
79
+ catch (cause) {
80
+ return { kind: 'error', cause };
81
+ }
82
+ if (definition === null || typeof definition !== 'object')
83
+ return { kind: 'no-definition' };
84
+ const list = definition[PAGES_KEY];
85
+ if (typeof list !== 'function')
86
+ return { kind: 'unsupported' };
87
+ return { kind: 'sources', sources: list() };
88
+ }
89
+ /** How long the dev server waits for a worker to read an app's module page sources. */
90
+ const WORKER_TIMEOUT_MS = 30_000;
91
+ const WORKER = new URL('./module-pages-worker.js', import.meta.url);
92
+ /**
93
+ * The dev server: the app file read in a worker thread, so it and every module it imports (the
94
+ * CLI's module list, the modules' definitions) are read as they are now, not from this long-lived
95
+ * process's module cache (a module added while `dokebi dev` ran stayed out, 403 on its folder).
96
+ */
97
+ function readInWorker(app) {
98
+ return new Promise((resolve) => {
99
+ const worker = new Worker(WORKER, { workerData: { url: pathToFileURL(app).href } });
100
+ let settled = false;
101
+ const finish = (read) => {
102
+ if (settled)
103
+ return;
104
+ settled = true;
105
+ clearTimeout(timer);
106
+ void worker.terminate().then(() => resolve(read),
107
+ // The answer is in hand; a worker that does not end is not the page table's failure.
108
+ () => resolve(read));
109
+ };
110
+ const timer = setTimeout(() => finish({
111
+ kind: 'error',
112
+ cause: new Error(`Reading ${app} took longer than ${WORKER_TIMEOUT_MS} ms`),
113
+ }), WORKER_TIMEOUT_MS);
114
+ timer.unref();
115
+ worker.once('message', (answer) => finish(answer.kind === 'error'
116
+ ? { kind: 'error', cause: new Error(answer.error) }
117
+ : answer));
118
+ worker.once('error', (cause) => finish({ kind: 'error', cause }));
119
+ worker.once('exit', (code) => finish({
120
+ kind: 'error',
121
+ cause: new Error(`The worker reading ${app} ended with code ${code} before it answered`),
122
+ }));
123
+ });
124
+ }
72
125
  /** The virtual module of every app of one Vite config (one dokebiInertia() instance). */
73
126
  export class ModulePagesSource {
74
127
  #logger;
@@ -118,24 +171,16 @@ export class ModulePagesSource {
118
171
  return null;
119
172
  const app = id.slice(PREFIX.length);
120
173
  const begin = performance.now();
121
- let definition;
122
- try {
123
- // The dev server reads the app file again after it changed (a module mounted or removed);
124
- // the modules it imports stay as Node cached them.
125
- const url = pathToFileURL(app).href + (this.#serve ? `?dokebi-pages=${Date.now()}` : '');
126
- definition = (await import(url)).default;
127
- }
128
- catch (cause) {
129
- throw new InertiaError('MODULE_PAGES_FAILED', `The Inertia build could not import ${app} to find its module pages`, { cause });
130
- }
131
- if (definition === null || typeof definition !== 'object')
174
+ const read = this.#serve ? await readInWorker(app) : await readHere(app);
175
+ if (read.kind === 'error')
176
+ throw new InertiaError('MODULE_PAGES_FAILED', `The Inertia build could not import ${app} to find its module pages`, { cause: read.cause });
177
+ if (read.kind === 'no-definition')
132
178
  throw new InertiaError('MODULE_PAGES_FAILED', `${app} has no default export made by defineApp of @dokebi/app`);
133
- const list = definition[PAGES_KEY];
134
179
  let sources = [];
135
- if (typeof list !== 'function')
180
+ if (read.kind === 'unsupported')
136
181
  this.#logger.log('warn', 'The app package does not list module pages; module pages are left out of this app (update @dokebi/app to 0.4.9 or later)', { code: 'INERTIA_MODULE_PAGES_UNSUPPORTED', app });
137
182
  else {
138
- const value = list();
183
+ const value = read.sources;
139
184
  if (!Array.isArray(value) || !value.every(isSource))
140
185
  throw new InertiaError('MODULE_PAGES_FAILED', `The module page sources of ${app} are not { module, folder, skip } entries`);
141
186
  sources = value;
@@ -193,7 +238,7 @@ export class ModulePagesSource {
193
238
  const page = event !== 'change' &&
194
239
  path.endsWith('.vue') &&
195
240
  watched.folders.some((folder) => within(path, folder));
196
- if (!page && path !== watched.app)
241
+ if (!page && path !== watched.app && !watched.files.includes(path))
197
242
  continue;
198
243
  this.#invalidate(id, event, path);
199
244
  }
@@ -205,12 +250,19 @@ export class ModulePagesSource {
205
250
  #watch(id, app, sources) {
206
251
  // The app's own views too: a page added or removed there changes which module pages it hides.
207
252
  const folders = [join(dirname(app), 'views'), ...sources.map((source) => source.folder)];
208
- this.#watched.set(id, { app, folders });
253
+ // What decides which modules the app mounts besides the app file: the module list the CLI
254
+ // writes (D-0227: an app may mount every registered module from it) and each mounted
255
+ // module's definition (its sets). A change of one reads the app again in a worker.
256
+ const files = [
257
+ ...['ts', 'js'].map((ext) => join(dirname(app), '..', '..', `dokebi.modules.generated.${ext}`)),
258
+ ...sources.flatMap((source) => ['module.ts', 'module.mts', 'module.js'].map((name) => join(dirname(source.folder), name))),
259
+ ];
260
+ this.#watched.set(id, { app, folders, files });
209
261
  if (!this.#serve)
210
262
  return;
211
263
  for (const source of sources)
212
264
  this.#allow(source.folder);
213
- this.#server?.watcher?.add([app, ...folders]);
265
+ this.#server?.watcher?.add([app, ...folders, ...files.filter((file) => existsSync(file))]);
214
266
  }
215
267
  #invalidate(id, event, path) {
216
268
  const server = this.#server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
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",
@@ -146,7 +146,7 @@
146
146
  "@dokebi/i18n": "0.2.5",
147
147
  "@dokebi/session": "0.4.8",
148
148
  "@dokebi/web": "0.4.12",
149
- "@dokebi/websocket": "0.4.4",
149
+ "@dokebi/websocket": "0.4.5",
150
150
  "@dokebi/websocket-hub": "0.4.3",
151
151
  "@inertiajs/vue3": "3.9.1",
152
152
  "vue": "3.5.44"