@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.
- package/AI.md +12 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -0
- package/dist/index.d.ts +17 -2
- package/dist/index.js +27 -1
- package/dist/template-build.js +1 -1
- package/dist/template-preview.js +3 -0
- package/dist/template-runtime.d.ts +66 -2
- package/dist/template-runtime.js +148 -5
- package/dist/vite-dev-errors.d.ts +41 -0
- package/dist/vite-dev-errors.js +64 -0
- package/dist/vite-dev.js +65 -2
- package/dist/vite-not-loaded.d.ts +91 -0
- package/dist/vite-not-loaded.js +242 -0
- package/dist/vite-type-barrels.d.ts +48 -0
- package/dist/vite-type-barrels.js +224 -0
- package/package.json +1 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { diagnostics } from '@dokebi/core/libraries';
|
|
2
|
+
import { isRemoteRequest } from './vite-files.js';
|
|
3
|
+
const { describeError } = diagnostics;
|
|
4
|
+
/** The longest URL text a record keeps. */
|
|
5
|
+
const URL_TEXT = 500;
|
|
6
|
+
/** The errors Vite's middlewares met, per request, until the host answers. */
|
|
7
|
+
export class ViteRequestErrors {
|
|
8
|
+
#logger;
|
|
9
|
+
#errors = new WeakMap();
|
|
10
|
+
constructor(logger) {
|
|
11
|
+
this.#logger = logger;
|
|
12
|
+
}
|
|
13
|
+
/** The Vite plugin that keeps each request's error before Vite's error middleware drops it. */
|
|
14
|
+
plugin() {
|
|
15
|
+
const errors = this.#errors;
|
|
16
|
+
return {
|
|
17
|
+
name: 'dokebi-inertia:dev-errors',
|
|
18
|
+
configureServer(server) {
|
|
19
|
+
// Returned: runs after Vite's internal middlewares, before its error middleware.
|
|
20
|
+
return () => {
|
|
21
|
+
server.middlewares.use(function keepViteError(error, request, _response, next) {
|
|
22
|
+
errors.set(request, error);
|
|
23
|
+
next(error);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** The error Vite met for `request` (and forgets it), or undefined. */
|
|
30
|
+
take(request) {
|
|
31
|
+
const error = this.#errors.get(request);
|
|
32
|
+
this.#errors.delete(request);
|
|
33
|
+
return error;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Answers 500 for a request Vite failed on, logged at error with `INERTIA_VITE_MODULE_FAILED`.
|
|
37
|
+
* This machine gets the message (the overlay shows it too); a remote host (a `devHosts` tunnel)
|
|
38
|
+
* gets no paths.
|
|
39
|
+
*/
|
|
40
|
+
answer(request, response, error, url) {
|
|
41
|
+
url = url.slice(0, URL_TEXT);
|
|
42
|
+
this.#logger.log('error', 'Vite dev host could not serve a module', {
|
|
43
|
+
code: 'INERTIA_VITE_MODULE_FAILED',
|
|
44
|
+
url,
|
|
45
|
+
error: describeError(error),
|
|
46
|
+
});
|
|
47
|
+
if (response.headersSent) {
|
|
48
|
+
response.end();
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const remote = isRemoteRequest(request.headers, request.socket?.remoteAddress);
|
|
52
|
+
response.statusCode = 500;
|
|
53
|
+
response.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
54
|
+
response.setHeader('cache-control', 'no-store');
|
|
55
|
+
if (remote) {
|
|
56
|
+
response.end('500 Internal Server Error\n');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// Vite and its plugins throw Rollup-style error objects as well as Errors.
|
|
60
|
+
const text = error?.message;
|
|
61
|
+
const message = typeof text === 'string' ? text : String(error);
|
|
62
|
+
response.end(`500 Internal Server Error (Vite): ${url}\n${message}\n`);
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/vite-dev.js
CHANGED
|
@@ -20,8 +20,12 @@ import { diagnostics } from '@dokebi/core/libraries';
|
|
|
20
20
|
import { RESCAN_VARIABLE, rescanInterval } from './dev-rescan.js';
|
|
21
21
|
import { InertiaError } from './errors.js';
|
|
22
22
|
import { DependencyCache } from './vite-deps.js';
|
|
23
|
+
import { ViteRequestErrors } from './vite-dev-errors.js';
|
|
23
24
|
import { DEV_CLIENT_HEADER, dokebiDevFiles } from './vite-files.js';
|
|
25
|
+
import { NotLoadedReporter } from './vite-not-loaded.js';
|
|
26
|
+
import { vueTypeBarrels } from './vite-type-barrels.js';
|
|
24
27
|
import { ServedFiles, staleEventFilter } from './vite-watch.js';
|
|
28
|
+
import { registerVueTypeScript } from './vue-typescript.js';
|
|
25
29
|
const { describeError } = diagnostics;
|
|
26
30
|
export const viteDevRequested = () => process.env.DOKEBI_VITE_DEV === '1';
|
|
27
31
|
/**
|
|
@@ -233,6 +237,7 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
|
|
|
233
237
|
const expected = Buffer.from(key);
|
|
234
238
|
let started;
|
|
235
239
|
let deps;
|
|
240
|
+
let notLoaded;
|
|
236
241
|
const report = (operation) => (error) => logger.log('error', 'Vite dev host request failed', {
|
|
237
242
|
code: 'INERTIA_VITE_HOST_FAILED',
|
|
238
243
|
operation,
|
|
@@ -312,19 +317,76 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
|
|
|
312
317
|
if (response.statusCode === 504)
|
|
313
318
|
void cache.ensure('outdated').catch(report('deps'));
|
|
314
319
|
});
|
|
315
|
-
|
|
320
|
+
const url = String(request.url ?? '/');
|
|
321
|
+
const reporter = notLoaded;
|
|
322
|
+
if (reporter)
|
|
323
|
+
response.once('finish', () => reporter.served(url, request.method, response.statusCode));
|
|
324
|
+
server.middlewares(request, response, (error) => {
|
|
325
|
+
// D-0292: an error Vite's error middleware dropped (a module that does not compile)
|
|
326
|
+
// answers 500 with it, not an empty 404.
|
|
327
|
+
const failed = failures.take(request);
|
|
328
|
+
if ((error === undefined || error === null) && failed !== undefined && failed !== null) {
|
|
329
|
+
failures.answer(request, response, failed, url);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
// D-0292: a module URL Vite answers nothing for (it drops ERR_LOAD_URL) is explained, and
|
|
333
|
+
// run through Vite once more when the file is there and allowed.
|
|
334
|
+
const subject = reporter && (error === undefined || error === null) && !response.headersSent
|
|
335
|
+
? reporter.subject(url, request.method, request.headers)
|
|
336
|
+
: undefined;
|
|
337
|
+
if (!reporter || !subject) {
|
|
338
|
+
viteFallthrough(response, error, report('vite'));
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
void (async () => {
|
|
342
|
+
const diagnosis = await reporter.diagnose(subject);
|
|
343
|
+
if (!diagnosis.retry || response.headersSent) {
|
|
344
|
+
viteFallthrough(response, undefined, report('vite'));
|
|
345
|
+
reporter.report(diagnosis, response.statusCode);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
await reporter.refresh(subject, diagnosis);
|
|
349
|
+
request.url = url;
|
|
350
|
+
response.once('finish', () => reporter.report(diagnosis, response.statusCode));
|
|
351
|
+
server.middlewares(request, response, (again) => {
|
|
352
|
+
const failedAgain = failures.take(request);
|
|
353
|
+
if ((again === undefined || again === null) &&
|
|
354
|
+
failedAgain !== undefined &&
|
|
355
|
+
failedAgain !== null)
|
|
356
|
+
failures.answer(request, response, failedAgain, url);
|
|
357
|
+
else
|
|
358
|
+
viteFallthrough(response, again, report('vite'));
|
|
359
|
+
});
|
|
360
|
+
})().catch((failure) => {
|
|
361
|
+
report('not-loaded')(failure);
|
|
362
|
+
viteFallthrough(response, undefined, report('vite'));
|
|
363
|
+
});
|
|
364
|
+
});
|
|
316
365
|
}
|
|
317
366
|
});
|
|
318
367
|
// Events FSEvents still holds from before the start are no edits (D-0045 addendum 4); the same
|
|
319
368
|
// records find the edits the watcher has not reported (D-0283).
|
|
320
369
|
const served = new ServedFiles(logger);
|
|
321
370
|
served.recordConfig(root);
|
|
371
|
+
const failures = new ViteRequestErrors(logger);
|
|
372
|
+
// D-0292: the Vue compiler resolves imported props types through TypeScript's JS API (with
|
|
373
|
+
// TypeScript 7 only @typescript/typescript6 has one). dokebiInertia() registers it; a vite.config
|
|
374
|
+
// without it got none, and every `defineProps<Imported>()` failed in dev.
|
|
375
|
+
registerVueTypeScript(root, logger);
|
|
322
376
|
try {
|
|
323
377
|
started = await vite.createServer({
|
|
324
378
|
root,
|
|
325
379
|
appType: 'custom',
|
|
326
380
|
// D-0129: the dev file policy for a vite.config without dokebiInertia() (which carries it).
|
|
327
|
-
|
|
381
|
+
// D-0292: errors Vite's error middleware would drop; Vue type caches of re-exporting files.
|
|
382
|
+
plugins: [
|
|
383
|
+
staleEventFilter(served, logger),
|
|
384
|
+
dokebiDevFiles(logger),
|
|
385
|
+
failures.plugin(),
|
|
386
|
+
// D-0292: the type files the Vue compiler reads join the comparison with the disk, so an
|
|
387
|
+
// edit the watcher misses reaches the pages like one of a module Vite loads.
|
|
388
|
+
vueTypeBarrels(logger, root, { onTypeFile: (file) => served.record(file, 'types') }),
|
|
389
|
+
],
|
|
328
390
|
server: {
|
|
329
391
|
middlewareMode: true,
|
|
330
392
|
// Hosts are checked by the dev routes of the plugin (see index.ts) and HTTP needs the key,
|
|
@@ -362,6 +424,7 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
|
|
|
362
424
|
throw error;
|
|
363
425
|
}
|
|
364
426
|
deps = cache;
|
|
427
|
+
notLoaded = new NotLoadedReporter(logger, server, vite.isFileLoadingAllowed);
|
|
365
428
|
// D-0289: the same comparison every DOKEBI_DEV_RESCAN_MS (2 s), so an edit the watcher reports
|
|
366
429
|
// late (FSEvents: seconds, a minute when fseventsd is busy) reaches HMR and the next request
|
|
367
430
|
// within that time, without a page load. A sweep stats the files Vite read (a few ms for a
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
/** A module of Vite's graph, as far as the diagnosis reads it. */
|
|
3
|
+
interface ModuleShape {
|
|
4
|
+
readonly id?: string | null;
|
|
5
|
+
readonly file?: string | null;
|
|
6
|
+
readonly url?: string;
|
|
7
|
+
readonly transformResult?: unknown;
|
|
8
|
+
readonly lastInvalidationTimestamp?: number;
|
|
9
|
+
}
|
|
10
|
+
interface GraphShape {
|
|
11
|
+
getModuleByUrl(url: string): Promise<ModuleShape | undefined> | ModuleShape | undefined;
|
|
12
|
+
invalidateModule(module: never): void;
|
|
13
|
+
readonly urlToModuleMap?: Map<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
/** The part of Vite's dev server the diagnosis uses (structural: no Vite types here). */
|
|
16
|
+
export interface NotLoadedServer {
|
|
17
|
+
readonly config: {
|
|
18
|
+
readonly root: string;
|
|
19
|
+
readonly base: string;
|
|
20
|
+
};
|
|
21
|
+
readonly environments?: {
|
|
22
|
+
readonly client?: {
|
|
23
|
+
readonly moduleGraph?: GraphShape;
|
|
24
|
+
transformRequest?(url: string): Promise<unknown>;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Vite's own check (`import { isFileLoadingAllowed } from 'vite'`), when this Vite exports it. */
|
|
29
|
+
export type LoadingCheck = (config: never, file: string) => boolean;
|
|
30
|
+
/** A request that may be a module Vite failed to load. */
|
|
31
|
+
export interface NotLoadedSubject {
|
|
32
|
+
/** The request URL as the browser sent it (with the base, without the host). */
|
|
33
|
+
readonly url: string;
|
|
34
|
+
/** The module URL Vite looks up: the base removed, decoded, without `t` and `import`. */
|
|
35
|
+
readonly module: string;
|
|
36
|
+
/** The file it names (absolute), when it names one (`/@fs/…` or a path under the root). */
|
|
37
|
+
readonly file: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** What this process sees for a module URL that Vite answered 404. */
|
|
40
|
+
export interface NotLoadedDiagnosis {
|
|
41
|
+
readonly url: string;
|
|
42
|
+
readonly module: string;
|
|
43
|
+
readonly root: string;
|
|
44
|
+
readonly file?: string;
|
|
45
|
+
/** `file` is a regular file here. */
|
|
46
|
+
readonly exists: boolean;
|
|
47
|
+
/** Why the stat failed (ENOENT, EACCES, …), or `directory` / `other` for a non-file. */
|
|
48
|
+
readonly missing?: string;
|
|
49
|
+
readonly readable?: boolean;
|
|
50
|
+
/** The real path, when it differs from `file` (a link). */
|
|
51
|
+
readonly real?: string;
|
|
52
|
+
/** Vite's isFileLoadingAllowed for `file`; absent when this Vite does not export it. */
|
|
53
|
+
readonly allowed?: boolean;
|
|
54
|
+
/** The module graph's entry for the URL, if any. */
|
|
55
|
+
readonly graph?: {
|
|
56
|
+
readonly id: string | null;
|
|
57
|
+
readonly file: string | null;
|
|
58
|
+
/** The entry's id names another file than the URL. */
|
|
59
|
+
readonly stale: boolean;
|
|
60
|
+
readonly transformed: boolean;
|
|
61
|
+
readonly invalidatedAt?: number;
|
|
62
|
+
};
|
|
63
|
+
/** The error of one more transformRequest: the message Vite dropped (`ERR_LOAD_URL` …). */
|
|
64
|
+
readonly cause?: {
|
|
65
|
+
readonly code?: string;
|
|
66
|
+
readonly message: string;
|
|
67
|
+
};
|
|
68
|
+
/** One more transformRequest produced the module (Vite's state changed since its answer). */
|
|
69
|
+
readonly loadsNow?: boolean;
|
|
70
|
+
/** The file is there, readable and allowed: the request goes through Vite once more. */
|
|
71
|
+
readonly retry: boolean;
|
|
72
|
+
}
|
|
73
|
+
/** Why a module URL answered 404, logged once per URL until it is served again. */
|
|
74
|
+
export declare class NotLoadedReporter {
|
|
75
|
+
#private;
|
|
76
|
+
constructor(logger: Logger, server: NotLoadedServer, allowed?: LoadingCheck);
|
|
77
|
+
/** The module request behind `url`, or undefined for anything else (a page, a folder, a virtual id). */
|
|
78
|
+
subject(url: string, method: string | undefined, headers: Readonly<Record<string, string | string[] | undefined>>): NotLoadedSubject | undefined;
|
|
79
|
+
/** What this process sees for the subject (never throws: a step that fails is recorded). */
|
|
80
|
+
diagnose(subject: NotLoadedSubject): Promise<NotLoadedDiagnosis>;
|
|
81
|
+
/**
|
|
82
|
+
* Before the one more run through Vite: the module is invalidated, and a URL entry whose id
|
|
83
|
+
* names another file is dropped, so Vite resolves the URL again.
|
|
84
|
+
*/
|
|
85
|
+
refresh(subject: NotLoadedSubject, diagnosis: NotLoadedDiagnosis): Promise<void>;
|
|
86
|
+
/** Logs the 404 (warn the first time for a URL, then debug with the count) with its outcome. */
|
|
87
|
+
report(diagnosis: NotLoadedDiagnosis, status: number): void;
|
|
88
|
+
/** A response for `url` that was served: a later 404 for it warns again. */
|
|
89
|
+
served(url: string, method: string | undefined, status: number): void;
|
|
90
|
+
}
|
|
91
|
+
export {};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// D-0292: why the Vite dev host answered 404 for a module URL. Vite's transform middleware turns
|
|
2
|
+
// `ERR_LOAD_URL` ("Failed to load url … (resolved id: …) in <importer>. Does the file exist?")
|
|
3
|
+
// into `next()` without the error, and when its static middleware does not serve the file either
|
|
4
|
+
// the host answers an empty 404: nothing in the log says which id Vite tried or why it read
|
|
5
|
+
// nothing (shop dev, 2026-11-28: three rewritten pages answered 404 for ten minutes while the
|
|
6
|
+
// files were on disk; a restart cleared it, and no trace was left).
|
|
7
|
+
//
|
|
8
|
+
// For a module request (a script, `?import`, a module or style extension) that ends that way, the
|
|
9
|
+
// host asks Vite again in the same process: the module graph's entry for the URL (its id and file),
|
|
10
|
+
// the file as this process sees it (stat, read access, real path), whether Vite's
|
|
11
|
+
// `isFileLoadingAllowed` admits it, and the error of one more `transformRequest` (the dropped
|
|
12
|
+
// message). It logs that as `INERTIA_VITE_NOT_LOADED` (warn, once per URL until the URL is served
|
|
13
|
+
// again; repeats at debug with their count).
|
|
14
|
+
//
|
|
15
|
+
// When the file is there, readable and allowed, the 404 is Vite's state, not the file's (a stale
|
|
16
|
+
// graph entry is the likeliest, which is what a restart clears): the host invalidates the module,
|
|
17
|
+
// drops a URL entry whose id names another file, and runs the request through Vite once more.
|
|
18
|
+
import { constants } from 'node:fs';
|
|
19
|
+
import { access, realpath, stat } from 'node:fs/promises';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { diagnostics } from '@dokebi/core/libraries';
|
|
22
|
+
const { describeError } = diagnostics;
|
|
23
|
+
/** Extensions of the modules and styles a page loads through Vite (`.html` is never a module). */
|
|
24
|
+
const MODULE_FILE = /\.(?:vue|[cm]?[jt]sx?|css|scss|sass|less|styl|stylus|pcss|postcss|sss|json|svg|wasm)$/i;
|
|
25
|
+
/** The longest URL text a record keeps. */
|
|
26
|
+
const URL_TEXT = 500;
|
|
27
|
+
const errorCode = (error) => {
|
|
28
|
+
const code = error?.code;
|
|
29
|
+
return typeof code === 'string' ? code : undefined;
|
|
30
|
+
};
|
|
31
|
+
/** Why a module URL answered 404, logged once per URL until it is served again. */
|
|
32
|
+
export class NotLoadedReporter {
|
|
33
|
+
#logger;
|
|
34
|
+
#server;
|
|
35
|
+
#allowed;
|
|
36
|
+
/** URL path → requests answered 404 since the warning (a later 2xx/3xx clears it). */
|
|
37
|
+
#reported = new Map();
|
|
38
|
+
constructor(logger, server, allowed) {
|
|
39
|
+
this.#logger = logger;
|
|
40
|
+
this.#server = server;
|
|
41
|
+
this.#allowed = allowed;
|
|
42
|
+
}
|
|
43
|
+
/** The module request behind `url`, or undefined for anything else (a page, a folder, a virtual id). */
|
|
44
|
+
subject(url, method, headers) {
|
|
45
|
+
if (method !== 'GET' && method !== 'HEAD')
|
|
46
|
+
return undefined;
|
|
47
|
+
const base = this.#server.config.base || '/';
|
|
48
|
+
const [rawPath = '/', query = ''] = url.split('#', 1)[0]?.split('?', 2) ?? [];
|
|
49
|
+
if (!rawPath.startsWith(base))
|
|
50
|
+
return undefined;
|
|
51
|
+
let path;
|
|
52
|
+
try {
|
|
53
|
+
path = decodeURI(`/${rawPath.slice(base.length)}`);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (!(error instanceof URIError))
|
|
57
|
+
throw error;
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (path.endsWith('/') || path.includes('\0'))
|
|
61
|
+
return undefined;
|
|
62
|
+
const params = new URLSearchParams(query);
|
|
63
|
+
const script = headers['sec-fetch-dest'] === 'script';
|
|
64
|
+
if (!script && !params.has('import') && !MODULE_FILE.test(path))
|
|
65
|
+
return undefined;
|
|
66
|
+
params.delete('t');
|
|
67
|
+
params.delete('import');
|
|
68
|
+
const rest = params.toString().replace(/=(?=&|$)/g, '');
|
|
69
|
+
const module = rest ? `${path}?${rest}` : path;
|
|
70
|
+
let file;
|
|
71
|
+
if (path.startsWith('/@fs/'))
|
|
72
|
+
file = path.slice(4);
|
|
73
|
+
else if (!path.startsWith('/@'))
|
|
74
|
+
file = join(this.#server.config.root, path);
|
|
75
|
+
return { url, module, file };
|
|
76
|
+
}
|
|
77
|
+
/** What this process sees for the subject (never throws: a step that fails is recorded). */
|
|
78
|
+
async diagnose(subject) {
|
|
79
|
+
const root = this.#server.config.root;
|
|
80
|
+
const client = this.#server.environments?.client;
|
|
81
|
+
let exists = false;
|
|
82
|
+
let missing;
|
|
83
|
+
let readable;
|
|
84
|
+
let real;
|
|
85
|
+
let allowed;
|
|
86
|
+
const file = subject.file;
|
|
87
|
+
if (file !== undefined) {
|
|
88
|
+
try {
|
|
89
|
+
const info = await stat(file);
|
|
90
|
+
exists = info.isFile();
|
|
91
|
+
if (!exists)
|
|
92
|
+
missing = info.isDirectory() ? 'directory' : 'other';
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
missing = errorCode(error) ?? 'failed';
|
|
96
|
+
}
|
|
97
|
+
if (exists) {
|
|
98
|
+
readable = await access(file, constants.R_OK).then(() => true, () => false);
|
|
99
|
+
const resolved = await realpath(file).then((path) => path, (error) => {
|
|
100
|
+
if (this.#logger.isEnabled('debug'))
|
|
101
|
+
this.#logger.log('debug', 'Vite dev host cannot resolve the real path of a module', {
|
|
102
|
+
file,
|
|
103
|
+
error: describeError(error),
|
|
104
|
+
});
|
|
105
|
+
return undefined;
|
|
106
|
+
});
|
|
107
|
+
if (resolved !== undefined && resolved !== file)
|
|
108
|
+
real = resolved;
|
|
109
|
+
}
|
|
110
|
+
if (this.#allowed) {
|
|
111
|
+
try {
|
|
112
|
+
allowed = this.#allowed(this.#server.config, file.split('\\').join('/'));
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
this.#logger.log('error', 'Vite isFileLoadingAllowed failed', {
|
|
116
|
+
code: 'INERTIA_VITE_NOT_LOADED',
|
|
117
|
+
file,
|
|
118
|
+
error: describeError(error),
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let graph;
|
|
124
|
+
const entry = await this.#module(subject.module);
|
|
125
|
+
if (entry) {
|
|
126
|
+
const id = typeof entry.id === 'string' ? entry.id : null;
|
|
127
|
+
const entryFile = typeof entry.file === 'string' ? entry.file : null;
|
|
128
|
+
const named = entryFile ?? (id === null ? null : (id.split('?', 1)[0] ?? null));
|
|
129
|
+
graph = {
|
|
130
|
+
id,
|
|
131
|
+
file: entryFile,
|
|
132
|
+
stale: file !== undefined && named !== null && named !== file && named !== real,
|
|
133
|
+
transformed: entry.transformResult != null,
|
|
134
|
+
...(typeof entry.lastInvalidationTimestamp === 'number' &&
|
|
135
|
+
entry.lastInvalidationTimestamp > 0
|
|
136
|
+
? { invalidatedAt: entry.lastInvalidationTimestamp }
|
|
137
|
+
: {}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
let cause;
|
|
141
|
+
let loadsNow;
|
|
142
|
+
if (client?.transformRequest) {
|
|
143
|
+
try {
|
|
144
|
+
loadsNow = (await client.transformRequest(subject.module)) != null;
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
const code = errorCode(error);
|
|
148
|
+
cause = {
|
|
149
|
+
...(code === undefined ? {} : { code }),
|
|
150
|
+
message: error instanceof Error ? error.message : String(error),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
url: subject.url.slice(0, URL_TEXT),
|
|
156
|
+
module: subject.module.slice(0, URL_TEXT),
|
|
157
|
+
root,
|
|
158
|
+
...(file === undefined ? {} : { file }),
|
|
159
|
+
exists,
|
|
160
|
+
...(missing === undefined ? {} : { missing }),
|
|
161
|
+
...(readable === undefined ? {} : { readable }),
|
|
162
|
+
...(real === undefined ? {} : { real }),
|
|
163
|
+
...(allowed === undefined ? {} : { allowed }),
|
|
164
|
+
...(graph === undefined ? {} : { graph }),
|
|
165
|
+
...(cause === undefined ? {} : { cause }),
|
|
166
|
+
...(loadsNow === undefined ? {} : { loadsNow }),
|
|
167
|
+
retry: exists && readable === true && allowed !== false,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Before the one more run through Vite: the module is invalidated, and a URL entry whose id
|
|
172
|
+
* names another file is dropped, so Vite resolves the URL again.
|
|
173
|
+
*/
|
|
174
|
+
async refresh(subject, diagnosis) {
|
|
175
|
+
const graph = this.#server.environments?.client?.moduleGraph;
|
|
176
|
+
if (!graph)
|
|
177
|
+
return;
|
|
178
|
+
const entry = await this.#module(subject.module);
|
|
179
|
+
if (!entry)
|
|
180
|
+
return;
|
|
181
|
+
try {
|
|
182
|
+
graph.invalidateModule(entry);
|
|
183
|
+
if (diagnosis.graph?.stale && typeof entry.url === 'string')
|
|
184
|
+
graph.urlToModuleMap?.delete(entry.url);
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
this.#logger.log('error', 'Vite dev host could not invalidate a module it answered 404', {
|
|
188
|
+
code: 'INERTIA_VITE_NOT_LOADED',
|
|
189
|
+
module: subject.module.slice(0, URL_TEXT),
|
|
190
|
+
error: describeError(error),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** Logs the 404 (warn the first time for a URL, then debug with the count) with its outcome. */
|
|
195
|
+
report(diagnosis, status) {
|
|
196
|
+
const key = diagnosis.module;
|
|
197
|
+
const served = status < 400;
|
|
198
|
+
const repeats = this.#reported.get(key);
|
|
199
|
+
if (served)
|
|
200
|
+
this.#reported.delete(key);
|
|
201
|
+
else
|
|
202
|
+
this.#reported.set(key, (repeats ?? -1) + 1);
|
|
203
|
+
const fields = {
|
|
204
|
+
code: 'INERTIA_VITE_NOT_LOADED',
|
|
205
|
+
...diagnosis,
|
|
206
|
+
...(diagnosis.retry ? { retried: status } : {}),
|
|
207
|
+
};
|
|
208
|
+
if (repeats === undefined)
|
|
209
|
+
this.#logger.log('warn', served
|
|
210
|
+
? 'Vite dev host answered 404 for a module, then served it after invalidating it'
|
|
211
|
+
: 'Vite dev host answered 404 for a module', fields);
|
|
212
|
+
else if (this.#logger.isEnabled('debug'))
|
|
213
|
+
this.#logger.log('debug', 'Vite dev host answered 404 for a module again', {
|
|
214
|
+
...fields,
|
|
215
|
+
repeats: repeats + 1,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/** A response for `url` that was served: a later 404 for it warns again. */
|
|
219
|
+
served(url, method, status) {
|
|
220
|
+
if (this.#reported.size === 0 || status >= 400)
|
|
221
|
+
return;
|
|
222
|
+
const subject = this.subject(url, method, {});
|
|
223
|
+
if (subject)
|
|
224
|
+
this.#reported.delete(subject.module);
|
|
225
|
+
}
|
|
226
|
+
async #module(url) {
|
|
227
|
+
const graph = this.#server.environments?.client?.moduleGraph;
|
|
228
|
+
if (!graph)
|
|
229
|
+
return undefined;
|
|
230
|
+
try {
|
|
231
|
+
return (await graph.getModuleByUrl(url)) ?? undefined;
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
if (this.#logger.isEnabled('debug'))
|
|
235
|
+
this.#logger.log('debug', 'Vite dev host cannot look a module up by its URL', {
|
|
236
|
+
url: url.slice(0, URL_TEXT),
|
|
237
|
+
error: describeError(error),
|
|
238
|
+
});
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
type Invalidate = (file: string) => void;
|
|
3
|
+
/** The re-exporting files of changed TypeScript sources, cleared in Vue's type cache. */
|
|
4
|
+
export declare class TypeBarrels {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(logger: Logger, root: string, invalidate?: Invalidate);
|
|
7
|
+
/** A TypeScript file changed or went away: clears the type caches of the files re-exporting it. */
|
|
8
|
+
changed(file: string): Promise<string[]>;
|
|
9
|
+
}
|
|
10
|
+
/** The file access @vue/compiler-sfc takes as `script.fs` (what it reads type files through). */
|
|
11
|
+
export interface TypeFileSystem {
|
|
12
|
+
fileExists(file: string): boolean;
|
|
13
|
+
readFile(file: string): string | undefined;
|
|
14
|
+
realpath?(file: string): string;
|
|
15
|
+
}
|
|
16
|
+
/** The part of Vite's dev server the plugin uses. */
|
|
17
|
+
interface WatcherServer {
|
|
18
|
+
readonly watcher?: {
|
|
19
|
+
on(event: 'unlink', listener: (file: string) => void): unknown;
|
|
20
|
+
};
|
|
21
|
+
readonly config?: {
|
|
22
|
+
readonly plugins?: readonly unknown[];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* D-0292: the type files the compiler reads, told to `onTypeFile` (the Vite host records them for
|
|
27
|
+
* its comparison with the disk, D-0283 / D-0289, which before saw only the modules Vite loads). The
|
|
28
|
+
* compiler reads them through `script.fs` (its own, or TypeScript's `ts.sys`); the wrapper passes
|
|
29
|
+
* every call on to that, or to node:fs with the same answers when there is none.
|
|
30
|
+
*/
|
|
31
|
+
export declare function recordingFileSystem(onTypeFile: (file: string) => void, base?: TypeFileSystem): TypeFileSystem;
|
|
32
|
+
export interface VueTypeBarrelsOptions {
|
|
33
|
+
/** The compiler's invalidateTypeCache (tests); by default vue/compiler-sfc of the project. */
|
|
34
|
+
readonly invalidate?: Invalidate;
|
|
35
|
+
/** Each type file (outside node_modules) the compiler reads (the host's comparison records it). */
|
|
36
|
+
readonly onTypeFile?: (file: string) => void;
|
|
37
|
+
}
|
|
38
|
+
/** The Vite plugin: clears the barrels before plugin-vue recompiles the components (`enforce: 'pre'`). */
|
|
39
|
+
export declare function vueTypeBarrels(logger: Logger, root: string, options?: VueTypeBarrelsOptions): {
|
|
40
|
+
name: 'dokebi-inertia:vue-type-barrels';
|
|
41
|
+
enforce: 'pre';
|
|
42
|
+
apply: 'serve';
|
|
43
|
+
configureServer(server: WatcherServer): void;
|
|
44
|
+
handleHotUpdate(context: {
|
|
45
|
+
file: string;
|
|
46
|
+
}): Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
export {};
|