@dokebi/template-inertia 0.4.9 → 0.4.11
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 +1 -1
- package/dist/index.js +1 -1
- package/dist/module-pages-worker.d.ts +12 -0
- package/dist/module-pages-worker.js +25 -0
- package/dist/module-pages.js +83 -17
- package/package.json +2 -2
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 reads the app again too, and so does a change of **any** module's definition (`module.ts`, `.mts` or `.js`) under the app's `src/` — a module that gains `views:` while `dokebi dev` runs has its pages in the table at once, and one that drops them loses them (0.4.11; before, only modules that already had views were watched, and `Unknown page` stayed until a restart).
|
|
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
|
@@ -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));
|
package/dist/module-pages.js
CHANGED
|
@@ -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,60 @@ 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
|
+
/** A module's definition file (D-0227: `module.ts` at the module's root). */
|
|
90
|
+
const DEFINITION = /^module\.(?:ts|mts|js)$/;
|
|
91
|
+
/** How long the dev server waits for a worker to read an app's module page sources. */
|
|
92
|
+
const WORKER_TIMEOUT_MS = 30_000;
|
|
93
|
+
const WORKER = new URL('./module-pages-worker.js', import.meta.url);
|
|
94
|
+
/**
|
|
95
|
+
* The dev server: the app file read in a worker thread, so it and every module it imports (the
|
|
96
|
+
* CLI's module list, the modules' definitions) are read as they are now, not from this long-lived
|
|
97
|
+
* process's module cache (a module added while `dokebi dev` ran stayed out, 403 on its folder).
|
|
98
|
+
*/
|
|
99
|
+
function readInWorker(app) {
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
const worker = new Worker(WORKER, { workerData: { url: pathToFileURL(app).href } });
|
|
102
|
+
let settled = false;
|
|
103
|
+
const finish = (read) => {
|
|
104
|
+
if (settled)
|
|
105
|
+
return;
|
|
106
|
+
settled = true;
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
void worker.terminate().then(() => resolve(read),
|
|
109
|
+
// The answer is in hand; a worker that does not end is not the page table's failure.
|
|
110
|
+
() => resolve(read));
|
|
111
|
+
};
|
|
112
|
+
const timer = setTimeout(() => finish({
|
|
113
|
+
kind: 'error',
|
|
114
|
+
cause: new Error(`Reading ${app} took longer than ${WORKER_TIMEOUT_MS} ms`),
|
|
115
|
+
}), WORKER_TIMEOUT_MS);
|
|
116
|
+
timer.unref();
|
|
117
|
+
worker.once('message', (answer) => finish(answer.kind === 'error'
|
|
118
|
+
? { kind: 'error', cause: new Error(answer.error) }
|
|
119
|
+
: answer));
|
|
120
|
+
worker.once('error', (cause) => finish({ kind: 'error', cause }));
|
|
121
|
+
worker.once('exit', (code) => finish({
|
|
122
|
+
kind: 'error',
|
|
123
|
+
cause: new Error(`The worker reading ${app} ended with code ${code} before it answered`),
|
|
124
|
+
}));
|
|
125
|
+
});
|
|
126
|
+
}
|
|
72
127
|
/** The virtual module of every app of one Vite config (one dokebiInertia() instance). */
|
|
73
128
|
export class ModulePagesSource {
|
|
74
129
|
#logger;
|
|
@@ -118,24 +173,16 @@ export class ModulePagesSource {
|
|
|
118
173
|
return null;
|
|
119
174
|
const app = id.slice(PREFIX.length);
|
|
120
175
|
const begin = performance.now();
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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')
|
|
176
|
+
const read = this.#serve ? await readInWorker(app) : await readHere(app);
|
|
177
|
+
if (read.kind === 'error')
|
|
178
|
+
throw new InertiaError('MODULE_PAGES_FAILED', `The Inertia build could not import ${app} to find its module pages`, { cause: read.cause });
|
|
179
|
+
if (read.kind === 'no-definition')
|
|
132
180
|
throw new InertiaError('MODULE_PAGES_FAILED', `${app} has no default export made by defineApp of @dokebi/app`);
|
|
133
|
-
const list = definition[PAGES_KEY];
|
|
134
181
|
let sources = [];
|
|
135
|
-
if (
|
|
182
|
+
if (read.kind === 'unsupported')
|
|
136
183
|
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
184
|
else {
|
|
138
|
-
const value =
|
|
185
|
+
const value = read.sources;
|
|
139
186
|
if (!Array.isArray(value) || !value.every(isSource))
|
|
140
187
|
throw new InertiaError('MODULE_PAGES_FAILED', `The module page sources of ${app} are not { module, folder, skip } entries`);
|
|
141
188
|
sources = value;
|
|
@@ -193,7 +240,11 @@ export class ModulePagesSource {
|
|
|
193
240
|
const page = event !== 'change' &&
|
|
194
241
|
path.endsWith('.vue') &&
|
|
195
242
|
watched.folders.some((folder) => within(path, folder));
|
|
196
|
-
|
|
243
|
+
// Any module definition under the app's sources (src/): a module the app mounts that
|
|
244
|
+
// gains, changes or drops `views` changes the table, and a module without views is not
|
|
245
|
+
// in the sources to watch by name (shop: gameplus/member/Favorites unknown until a restart).
|
|
246
|
+
const definition = DEFINITION.test(basename(path)) && within(path, watched.sources);
|
|
247
|
+
if (!page && !definition && path !== watched.app && !watched.files.includes(path))
|
|
197
248
|
continue;
|
|
198
249
|
this.#invalidate(id, event, path);
|
|
199
250
|
}
|
|
@@ -205,12 +256,27 @@ export class ModulePagesSource {
|
|
|
205
256
|
#watch(id, app, sources) {
|
|
206
257
|
// The app's own views too: a page added or removed there changes which module pages it hides.
|
|
207
258
|
const folders = [join(dirname(app), 'views'), ...sources.map((source) => source.folder)];
|
|
208
|
-
|
|
259
|
+
// What decides which modules the app mounts besides the app file: the module list the CLI
|
|
260
|
+
// writes (D-0227: an app may mount every registered module from it) and each mounted
|
|
261
|
+
// module's definition (its sets). A change of one reads the app again in a worker.
|
|
262
|
+
const files = [
|
|
263
|
+
...['ts', 'js'].map((ext) => join(dirname(app), '..', '..', `dokebi.modules.generated.${ext}`)),
|
|
264
|
+
...sources.flatMap((source) => ['module.ts', 'module.mts', 'module.js'].map((name) => join(dirname(source.folder), name))),
|
|
265
|
+
];
|
|
266
|
+
const root = join(dirname(app), '..', '..');
|
|
267
|
+
this.#watched.set(id, { app, folders, files, sources: root });
|
|
209
268
|
if (!this.#serve)
|
|
210
269
|
return;
|
|
211
270
|
for (const source of sources)
|
|
212
271
|
this.#allow(source.folder);
|
|
213
|
-
|
|
272
|
+
// The source tree too, so a definition file is seen wherever its module lives (Vite watches
|
|
273
|
+
// its root; this covers a host whose root is elsewhere).
|
|
274
|
+
this.#server?.watcher?.add([
|
|
275
|
+
app,
|
|
276
|
+
root,
|
|
277
|
+
...folders,
|
|
278
|
+
...files.filter((file) => existsSync(file)),
|
|
279
|
+
]);
|
|
214
280
|
}
|
|
215
281
|
#invalidate(id, event, path) {
|
|
216
282
|
const server = this.#server;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dokebi/template-inertia",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
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.
|
|
149
|
+
"@dokebi/websocket": "0.4.8",
|
|
150
150
|
"@dokebi/websocket-hub": "0.4.3",
|
|
151
151
|
"@inertiajs/vue3": "3.9.1",
|
|
152
152
|
"vue": "3.5.44"
|