@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.
- package/AI.md +15 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -0
- package/dist/index.js +10 -2
- package/dist/template-build.js +27 -3
- package/dist/template-bundle.js +11 -7
- package/dist/template-preview.d.ts +38 -0
- package/dist/template-preview.js +339 -0
- package/dist/template-runtime.d.ts +12 -0
- package/dist/template-runtime.js +48 -0
- package/dist/template.d.ts +1 -0
- package/dist/template.js +1 -0
- package/dist/vite-dev-errors.d.ts +41 -0
- package/dist/vite-dev-errors.js +64 -0
- package/dist/vite-dev.js +63 -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 +26 -0
- package/dist/vite-type-barrels.js +166 -0
- package/package.json +7 -2
package/dist/template-runtime.js
CHANGED
|
@@ -181,6 +181,7 @@ export class TemplateBundles {
|
|
|
181
181
|
#verify;
|
|
182
182
|
#repeat;
|
|
183
183
|
#production;
|
|
184
|
+
#words;
|
|
184
185
|
#settings;
|
|
185
186
|
#apps = new Map();
|
|
186
187
|
#slots = new Map();
|
|
@@ -194,6 +195,7 @@ export class TemplateBundles {
|
|
|
194
195
|
this.#verify = options.verify;
|
|
195
196
|
this.#repeat = options.repeat;
|
|
196
197
|
this.#production = options.production ?? process.env.NODE_ENV === 'production';
|
|
198
|
+
this.#words = options.words;
|
|
197
199
|
this.#cacheRoot = resolve(options.settings.cacheDir);
|
|
198
200
|
const store = this.#store;
|
|
199
201
|
globalThis[TEMPLATE_REGISTRY] = Object.freeze({
|
|
@@ -272,6 +274,7 @@ export class TemplateBundles {
|
|
|
272
274
|
kept: [],
|
|
273
275
|
seen: undefined,
|
|
274
276
|
loading: undefined,
|
|
277
|
+
words: false,
|
|
275
278
|
};
|
|
276
279
|
this.#slots.set(key, slot);
|
|
277
280
|
}
|
|
@@ -284,6 +287,8 @@ export class TemplateBundles {
|
|
|
284
287
|
for (const [key, slot] of this.#slots)
|
|
285
288
|
if (!wanted.has(key)) {
|
|
286
289
|
this.#slots.delete(key);
|
|
290
|
+
if (slot.words)
|
|
291
|
+
work.push(this.#setWords(slot, undefined, undefined));
|
|
287
292
|
this.#logger.log('info', 'Template bundle no longer configured; the app pages answer', {
|
|
288
293
|
code: 'INERTIA_TEMPLATE_REMOVED',
|
|
289
294
|
app: slot.app,
|
|
@@ -570,6 +575,11 @@ export class TemplateBundles {
|
|
|
570
575
|
if (target !== undefined)
|
|
571
576
|
importMap[module] = target;
|
|
572
577
|
}
|
|
578
|
+
// D-0291 stage 3b: the bundle's screen words, just before it serves (a refused set leaves the
|
|
579
|
+
// bundle in use with the words of before; a bundle without words takes them out).
|
|
580
|
+
const hasWords = Object.keys(manifest.files).some((file) => file.startsWith('locales/'));
|
|
581
|
+
if (hasWords || slot.words)
|
|
582
|
+
await this.#setWords(slot, hasWords ? join(dir, 'locales') : undefined, buildId);
|
|
573
583
|
const generation = new TemplateGeneration(slot.app, slot.template, buildId, manifest, dir, pages, server, importMap, [...refused]);
|
|
574
584
|
const previous = slot.current;
|
|
575
585
|
slot.current = generation;
|
|
@@ -596,6 +606,44 @@ export class TemplateBundles {
|
|
|
596
606
|
this.#fail(slot, error);
|
|
597
607
|
}
|
|
598
608
|
}
|
|
609
|
+
/** D-0291 stage 3b: hands a generation's words (or none) to @dokebi/i18n. Never throws. */
|
|
610
|
+
async #setWords(slot, directory, buildId) {
|
|
611
|
+
const words = this.#words;
|
|
612
|
+
if (!words) {
|
|
613
|
+
if (directory !== undefined) {
|
|
614
|
+
const repeated = this.#repeat(`words unused ${slot.app} ${slot.template}`);
|
|
615
|
+
if (repeated !== undefined)
|
|
616
|
+
this.#logger.log('info', 'Template bundle words are not used: @dokebi/i18n is not registered', {
|
|
617
|
+
code: 'INERTIA_TEMPLATE_LOCALES_UNUSED',
|
|
618
|
+
app: slot.app,
|
|
619
|
+
template: slot.template,
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
let answer;
|
|
625
|
+
try {
|
|
626
|
+
answer = await words(slot.app, slot.template, directory);
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
answer = { ok: false, reason: error.message };
|
|
630
|
+
}
|
|
631
|
+
if (answer.ok) {
|
|
632
|
+
slot.words = directory !== undefined;
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const repeated = this.#repeat(`words ${slot.app} ${slot.template} ${answer.reason}`);
|
|
636
|
+
if (repeated !== undefined)
|
|
637
|
+
this.#logger.log('warn', 'Template bundle words refused; its pages keep the words of before', {
|
|
638
|
+
code: 'INERTIA_TEMPLATE_LOCALES_REJECTED',
|
|
639
|
+
app: slot.app,
|
|
640
|
+
template: slot.template,
|
|
641
|
+
buildId: buildId ?? null,
|
|
642
|
+
reason: answer.reason,
|
|
643
|
+
...(repeated ? { repeated } : {}),
|
|
644
|
+
fix: 'Replace only keys the app’s catalogs have, with the same placeholders and in the app’s locales (D-0282 rules), then build the bundle again.',
|
|
645
|
+
});
|
|
646
|
+
}
|
|
599
647
|
/** A package.json at the cache root: the bundle's .js modules are ES modules. */
|
|
600
648
|
async #ensureModuleScope() {
|
|
601
649
|
const file = join(this.#cacheRoot, 'package.json');
|
package/dist/template.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { buildTemplateBundle, packageEntry, type TemplateBuildOptions, type TemplateBuildResult, templatePages, } from './template-build.js';
|
|
2
2
|
export { checkTemplateBundle, compatibleLine, type ProvidedShared, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, type TemplateCheck, type TemplateCheckLimits, type TemplateManifest, type TemplatePage, type TemplateProblem, } from './template-bundle.js';
|
|
3
|
+
export { PREVIEW_PREFIX, previewCases, previewWords, startTemplatePreview, type TemplatePreview, type TemplatePreviewOptions, type TemplatePreviewPage, } from './template-preview.js';
|
|
3
4
|
export { type DokebiInertiaTemplateOptions, packageVersion, SHARED_FILE, type SharedFile, sharedEntryName, } from './template-shared.js';
|
package/dist/template.js
CHANGED
|
@@ -2,4 +2,5 @@
|
|
|
2
2
|
// the build that `dokebi template build` runs in a template repository.
|
|
3
3
|
export { buildTemplateBundle, packageEntry, templatePages, } from './template-build.js';
|
|
4
4
|
export { checkTemplateBundle, compatibleLine, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, } from './template-bundle.js';
|
|
5
|
+
export { PREVIEW_PREFIX, previewCases, previewWords, startTemplatePreview, } from './template-preview.js';
|
|
5
6
|
export { packageVersion, SHARED_FILE, sharedEntryName, } from './template-shared.js';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
type Next = (error?: unknown) => void;
|
|
3
|
+
/** The part of Vite's dev server the plugin uses. */
|
|
4
|
+
interface MiddlewaresShape {
|
|
5
|
+
readonly middlewares: {
|
|
6
|
+
use(handler: (error: unknown, request: object, response: unknown, next: Next) => void): unknown;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** The part of a request the answer reads. */
|
|
10
|
+
export interface FailedRequest {
|
|
11
|
+
readonly headers: Readonly<Record<string, string | string[] | undefined>>;
|
|
12
|
+
readonly socket?: {
|
|
13
|
+
readonly remoteAddress?: string | undefined;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** The part of a response the answer writes. */
|
|
17
|
+
export interface FailedResponse {
|
|
18
|
+
statusCode: number;
|
|
19
|
+
readonly headersSent: boolean;
|
|
20
|
+
setHeader(name: string, value: string): unknown;
|
|
21
|
+
end(body?: string): unknown;
|
|
22
|
+
}
|
|
23
|
+
/** The errors Vite's middlewares met, per request, until the host answers. */
|
|
24
|
+
export declare class ViteRequestErrors {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(logger: Logger);
|
|
27
|
+
/** The Vite plugin that keeps each request's error before Vite's error middleware drops it. */
|
|
28
|
+
plugin(): {
|
|
29
|
+
name: string;
|
|
30
|
+
configureServer(server: MiddlewaresShape): () => void;
|
|
31
|
+
};
|
|
32
|
+
/** The error Vite met for `request` (and forgets it), or undefined. */
|
|
33
|
+
take(request: object): unknown;
|
|
34
|
+
/**
|
|
35
|
+
* Answers 500 for a request Vite failed on, logged at error with `INERTIA_VITE_MODULE_FAILED`.
|
|
36
|
+
* This machine gets the message (the overlay shows it too); a remote host (a `devHosts` tunnel)
|
|
37
|
+
* gets no paths.
|
|
38
|
+
*/
|
|
39
|
+
answer(request: FailedRequest, response: FailedResponse, error: unknown, url: string): void;
|
|
40
|
+
}
|
|
41
|
+
export {};
|
|
@@ -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,74 @@ 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
|
+
vueTypeBarrels(logger, root),
|
|
387
|
+
],
|
|
328
388
|
server: {
|
|
329
389
|
middlewareMode: true,
|
|
330
390
|
// Hosts are checked by the dev routes of the plugin (see index.ts) and HTTP needs the key,
|
|
@@ -362,6 +422,7 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
|
|
|
362
422
|
throw error;
|
|
363
423
|
}
|
|
364
424
|
deps = cache;
|
|
425
|
+
notLoaded = new NotLoadedReporter(logger, server, vite.isFileLoadingAllowed);
|
|
365
426
|
// D-0289: the same comparison every DOKEBI_DEV_RESCAN_MS (2 s), so an edit the watcher reports
|
|
366
427
|
// late (FSEvents: seconds, a minute when fseventsd is busy) reaches HMR and the next request
|
|
367
428
|
// 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,26 @@
|
|
|
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 part of Vite's dev server the plugin uses. */
|
|
11
|
+
interface WatcherServer {
|
|
12
|
+
readonly watcher?: {
|
|
13
|
+
on(event: 'unlink', listener: (file: string) => void): unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** The Vite plugin: clears the barrels before plugin-vue recompiles the components (`enforce: 'pre'`). */
|
|
17
|
+
export declare function vueTypeBarrels(logger: Logger, root: string, invalidate?: Invalidate): {
|
|
18
|
+
name: 'dokebi-inertia:vue-type-barrels';
|
|
19
|
+
enforce: 'pre';
|
|
20
|
+
apply: 'serve';
|
|
21
|
+
configureServer(server: WatcherServer): void;
|
|
22
|
+
handleHotUpdate(context: {
|
|
23
|
+
file: string;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
export {};
|