@dokebi/template-inertia 0.4.19 → 0.4.21
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 +59 -1
- package/README.md +4 -0
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +5 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.js +107 -11
- package/dist/options.d.ts +9 -0
- package/dist/options.js +53 -0
- package/dist/pages.d.ts +33 -0
- package/dist/pages.js +82 -0
- package/dist/responder.d.ts +6 -0
- package/dist/responder.js +18 -8
- package/dist/template-build.d.ts +43 -0
- package/dist/template-build.js +365 -0
- package/dist/template-bundle.d.ts +102 -0
- package/dist/template-bundle.js +344 -0
- package/dist/template-runtime.d.ts +145 -0
- package/dist/template-runtime.js +723 -0
- package/dist/template-shared.d.ts +99 -0
- package/dist/template-shared.js +231 -0
- package/dist/template.d.ts +3 -0
- package/dist/template.js +5 -0
- package/dist/vite-plugin.d.ts +20 -2
- package/dist/vite-plugin.js +27 -0
- package/dist/vue-typescript.d.ts +9 -0
- package/dist/vue-typescript.js +68 -0
- package/options.schema.json +79 -1
- package/package.json +6 -2
- package/templates/shared.ts +4 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
/** `dokebiInertia({ templates })` (D-0291). */
|
|
3
|
+
export interface DokebiInertiaTemplateOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The app's own modules template bundles share with its pages (its SDK: components,
|
|
6
|
+
* composables, stores), as package names (`@shop/sdk`, `@shop/sdk/components`). vue,
|
|
7
|
+
* vue/server-renderer, @inertiajs/vue3 and @dokebi/template-inertia/{i18n,pages} are always
|
|
8
|
+
* provided.
|
|
9
|
+
*/
|
|
10
|
+
readonly shared?: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
/** Where a build writes what it provides, under its outDir. */
|
|
13
|
+
export declare const SHARED_FILE = ".vite/dokebi-shared.json";
|
|
14
|
+
/** `dokebi-shared.json`. */
|
|
15
|
+
export interface SharedFile {
|
|
16
|
+
readonly format: 1;
|
|
17
|
+
readonly ssr: boolean;
|
|
18
|
+
/** The URL prefix of the browser build (`base`); `/` for the server build. */
|
|
19
|
+
readonly base: string;
|
|
20
|
+
/** Module → the file of the build that provides it (relative to outDir) and its package version. */
|
|
21
|
+
readonly modules: Readonly<Record<string, {
|
|
22
|
+
readonly file: string;
|
|
23
|
+
readonly version: string | null;
|
|
24
|
+
}>>;
|
|
25
|
+
}
|
|
26
|
+
/** The checked shared list (defaults first), or throws INVALID_TEMPLATE_SHARED. */
|
|
27
|
+
export declare function sharedModules(options: DokebiInertiaTemplateOptions | undefined): string[];
|
|
28
|
+
/** The entry name of a shared module: `dokebi-shared/vue`, `dokebi-shared/shop__sdk`. */
|
|
29
|
+
export declare function sharedEntryName(module: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* The version of the package of `module` as installed for `root`: the nearest
|
|
32
|
+
* node_modules/<package>/package.json walking up (a workspace link counts), or null for a module
|
|
33
|
+
* that is no installed package (a local alias).
|
|
34
|
+
*/
|
|
35
|
+
export declare function packageVersion(root: string, module: string): string | null;
|
|
36
|
+
/**
|
|
37
|
+
* Whether `module`, as Node resolves it for `root`, has a default export: a server build leaves
|
|
38
|
+
* installed packages to Node (an external), so the entry cannot see their exports. A probe file
|
|
39
|
+
* in root's node_modules/.cache imports it the way the built server code will.
|
|
40
|
+
*/
|
|
41
|
+
export declare function externalHasDefault(root: string, module: string): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* The version of `module`'s package found from a file it resolved to (an alias such as
|
|
44
|
+
* `resolve.alias: { '@shop/sdk': 'packages/sdk/src/index.ts' }`): the nearest package.json above
|
|
45
|
+
* that file whose `name` is the package's, or null (an alias into the app's own src/ is no package).
|
|
46
|
+
*/
|
|
47
|
+
export declare function versionNear(file: string, module: string): string | null;
|
|
48
|
+
/** The part of Rollup's input options this reads and changes. */
|
|
49
|
+
export interface InputOptionsShape {
|
|
50
|
+
readonly input?: string | readonly string[] | Readonly<Record<string, string>>;
|
|
51
|
+
readonly preserveEntrySignatures?: unknown;
|
|
52
|
+
readonly [key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
/** The part of a plugin context `load` uses. */
|
|
55
|
+
export interface SharedLoadContext {
|
|
56
|
+
resolve(source: string, importer?: string): Promise<{
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly external?: unknown;
|
|
59
|
+
} | null>;
|
|
60
|
+
load(options: {
|
|
61
|
+
id: string;
|
|
62
|
+
}): Promise<{
|
|
63
|
+
readonly exports?: readonly string[] | null;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
/** One output chunk as `writeBundle` sees it. */
|
|
67
|
+
interface OutputChunkShape {
|
|
68
|
+
readonly type: string;
|
|
69
|
+
readonly isEntry?: boolean;
|
|
70
|
+
readonly facadeModuleId?: string | null;
|
|
71
|
+
readonly fileName: string;
|
|
72
|
+
}
|
|
73
|
+
/** The shared entries of one app build (`dokebiInertia({ templates })`). */
|
|
74
|
+
export declare class SharedEntries {
|
|
75
|
+
#private;
|
|
76
|
+
constructor(logger: Logger, options: DokebiInertiaTemplateOptions);
|
|
77
|
+
/** The modules this build provides (the server build alone has vue/server-renderer). */
|
|
78
|
+
get modules(): readonly string[];
|
|
79
|
+
configResolved(config: {
|
|
80
|
+
readonly root: string;
|
|
81
|
+
readonly command: string;
|
|
82
|
+
readonly base?: string;
|
|
83
|
+
readonly build: {
|
|
84
|
+
readonly ssr?: unknown;
|
|
85
|
+
readonly outDir: string;
|
|
86
|
+
};
|
|
87
|
+
}): void;
|
|
88
|
+
/**
|
|
89
|
+
* Rollup's `options`: the shared entries join the app's inputs (a string or list input is
|
|
90
|
+
* named as Rollup names it, by file name), and entry exports are kept.
|
|
91
|
+
*/
|
|
92
|
+
options(input: InputOptionsShape): InputOptionsShape | null;
|
|
93
|
+
resolveId(source: string): string | null;
|
|
94
|
+
/** The entry text: every export of the module, and its default when it has one. */
|
|
95
|
+
load(context: SharedLoadContext, id: string): Promise<string | null>;
|
|
96
|
+
/** `writeBundle`: `dokebi-shared.json` beside the build's manifest. */
|
|
97
|
+
writeBundle(bundle: Readonly<Record<string, OutputChunkShape>>): Promise<void>;
|
|
98
|
+
}
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// D-0291 §2: the app build provides the shared modules of template bundles. With
|
|
2
|
+
// `dokebiInertia({ templates: { shared: ['@shop/sdk'] } })` every `vite build` of the app (the
|
|
3
|
+
// browser build and the server build) gets one more entry per shared module, `export * from
|
|
4
|
+
// '<module>'` (and its default), named `dokebi-shared/<module>`. Entries of one build share module
|
|
5
|
+
// instances, so the vue a bundle page receives through that entry is the vue of the app's own
|
|
6
|
+
// pages. The build writes which file provides which module, with its package version, to
|
|
7
|
+
// `<outDir>/.vite/dokebi-shared.json`; the app (stage 2) points the bundle's imports there: an
|
|
8
|
+
// import map in the browser, a resolve hook on the server.
|
|
9
|
+
import { randomBytes } from 'node:crypto';
|
|
10
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
11
|
+
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
12
|
+
import { dirname, isAbsolute, join, parse, resolve } from 'node:path';
|
|
13
|
+
import { pathToFileURL } from 'node:url';
|
|
14
|
+
import { InertiaUsageError } from './errors.js';
|
|
15
|
+
import { SERVER_ONLY_SHARED, SHARED_DEFAULTS, SHARED_SPECIFIER } from './template-bundle.js';
|
|
16
|
+
/** Where a build writes what it provides, under its outDir. */
|
|
17
|
+
export const SHARED_FILE = '.vite/dokebi-shared.json';
|
|
18
|
+
const PREFIX = '\0dokebi-shared:';
|
|
19
|
+
/** The checked shared list (defaults first), or throws INVALID_TEMPLATE_SHARED. */
|
|
20
|
+
export function sharedModules(options) {
|
|
21
|
+
const own = options?.shared ?? [];
|
|
22
|
+
if (!Array.isArray(own) || own.length > 64)
|
|
23
|
+
throw new InertiaUsageError('INVALID_TEMPLATE_SHARED', 'templates.shared must be a list of at most 64 module names');
|
|
24
|
+
for (const name of own)
|
|
25
|
+
if (typeof name !== 'string' || !SHARED_SPECIFIER.test(name))
|
|
26
|
+
throw new InertiaUsageError('INVALID_TEMPLATE_SHARED', `templates.shared: ${String(name)} is not a package name with an optional subpath`);
|
|
27
|
+
return [...new Set([...SHARED_DEFAULTS, ...own])];
|
|
28
|
+
}
|
|
29
|
+
/** The entry name of a shared module: `dokebi-shared/vue`, `dokebi-shared/shop__sdk`. */
|
|
30
|
+
export function sharedEntryName(module) {
|
|
31
|
+
return `dokebi-shared/${module.replace(/^@/, '').replace(/[^A-Za-z0-9._-]+/g, '__')}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The version of the package of `module` as installed for `root`: the nearest
|
|
35
|
+
* node_modules/<package>/package.json walking up (a workspace link counts), or null for a module
|
|
36
|
+
* that is no installed package (a local alias).
|
|
37
|
+
*/
|
|
38
|
+
export function packageVersion(root, module) {
|
|
39
|
+
const parts = module.split('/');
|
|
40
|
+
const name = module.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
41
|
+
for (let dir = resolve(root);; dir = dirname(dir)) {
|
|
42
|
+
const file = join(dir, 'node_modules', name, 'package.json');
|
|
43
|
+
if (existsSync(file)) {
|
|
44
|
+
try {
|
|
45
|
+
const version = JSON.parse(readFileSync(file, 'utf8')).version;
|
|
46
|
+
return typeof version === 'string' ? version : null;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (dirname(dir) === dir)
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether `module`, as Node resolves it for `root`, has a default export: a server build leaves
|
|
58
|
+
* installed packages to Node (an external), so the entry cannot see their exports. A probe file
|
|
59
|
+
* in root's node_modules/.cache imports it the way the built server code will.
|
|
60
|
+
*/
|
|
61
|
+
export async function externalHasDefault(root, module) {
|
|
62
|
+
const folder = join(root, 'node_modules', '.cache', 'dokebi-shared');
|
|
63
|
+
await mkdir(folder, { recursive: true });
|
|
64
|
+
const probe = join(folder, `probe-${randomBytes(6).toString('hex')}.mjs`);
|
|
65
|
+
await writeFile(probe, `export * as namespace from ${JSON.stringify(module)};\n`);
|
|
66
|
+
try {
|
|
67
|
+
const { namespace } = (await import(pathToFileURL(probe).href));
|
|
68
|
+
return Object.hasOwn(namespace, 'default');
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await rm(probe, { force: true });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The version of `module`'s package found from a file it resolved to (an alias such as
|
|
76
|
+
* `resolve.alias: { '@shop/sdk': 'packages/sdk/src/index.ts' }`): the nearest package.json above
|
|
77
|
+
* that file whose `name` is the package's, or null (an alias into the app's own src/ is no package).
|
|
78
|
+
*/
|
|
79
|
+
export function versionNear(file, module) {
|
|
80
|
+
const parts = module.split('/');
|
|
81
|
+
const name = module.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
82
|
+
for (let dir = dirname(resolve(file));; dir = dirname(dir)) {
|
|
83
|
+
const candidate = join(dir, 'package.json');
|
|
84
|
+
if (existsSync(candidate)) {
|
|
85
|
+
let json;
|
|
86
|
+
try {
|
|
87
|
+
json = JSON.parse(readFileSync(candidate, 'utf8'));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
if (json.name === name)
|
|
93
|
+
return typeof json.version === 'string' ? json.version : null;
|
|
94
|
+
}
|
|
95
|
+
if (dirname(dir) === dir || /[\\/]node_modules$/.test(dir))
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** The shared entries of one app build (`dokebiInertia({ templates })`). */
|
|
100
|
+
export class SharedEntries {
|
|
101
|
+
#logger;
|
|
102
|
+
#modules;
|
|
103
|
+
#root = process.cwd();
|
|
104
|
+
#ssr = false;
|
|
105
|
+
#base = '/';
|
|
106
|
+
#outDir = 'dist';
|
|
107
|
+
#build = false;
|
|
108
|
+
/** module → the file it resolved to (not an external): its version when no package is installed. */
|
|
109
|
+
#resolved = new Map();
|
|
110
|
+
constructor(logger, options) {
|
|
111
|
+
this.#logger = logger;
|
|
112
|
+
this.#modules = sharedModules(options);
|
|
113
|
+
const names = new Map();
|
|
114
|
+
for (const module of this.#modules) {
|
|
115
|
+
const name = sharedEntryName(module);
|
|
116
|
+
const other = names.get(name);
|
|
117
|
+
if (other !== undefined)
|
|
118
|
+
throw new InertiaUsageError('INVALID_TEMPLATE_SHARED', `templates.shared: ${module} and ${other} make the same entry name ${name}`);
|
|
119
|
+
names.set(name, module);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** The modules this build provides (the server build alone has vue/server-renderer). */
|
|
123
|
+
get modules() {
|
|
124
|
+
return this.#ssr
|
|
125
|
+
? this.#modules
|
|
126
|
+
: this.#modules.filter((module) => !SERVER_ONLY_SHARED.includes(module));
|
|
127
|
+
}
|
|
128
|
+
configResolved(config) {
|
|
129
|
+
this.#root = config.root;
|
|
130
|
+
this.#build = config.command === 'build';
|
|
131
|
+
this.#ssr = Boolean(config.build.ssr);
|
|
132
|
+
this.#base = this.#ssr ? '/' : config.base || '/';
|
|
133
|
+
this.#outDir = isAbsolute(config.build.outDir)
|
|
134
|
+
? config.build.outDir
|
|
135
|
+
: join(config.root, config.build.outDir);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Rollup's `options`: the shared entries join the app's inputs (a string or list input is
|
|
139
|
+
* named as Rollup names it, by file name), and entry exports are kept.
|
|
140
|
+
*/
|
|
141
|
+
options(input) {
|
|
142
|
+
if (!this.#build)
|
|
143
|
+
return null;
|
|
144
|
+
const own = input.input;
|
|
145
|
+
const named = own === undefined
|
|
146
|
+
? {}
|
|
147
|
+
: typeof own === 'string'
|
|
148
|
+
? { [parse(own).name]: own }
|
|
149
|
+
: Array.isArray(own)
|
|
150
|
+
? Object.fromEntries(own.map((file) => [parse(file).name, file]))
|
|
151
|
+
: { ...own };
|
|
152
|
+
for (const module of this.modules)
|
|
153
|
+
named[sharedEntryName(module)] = `${PREFIX}${module}`;
|
|
154
|
+
this.#logger.log('debug', 'Inertia build provides the shared modules of template bundles', {
|
|
155
|
+
code: 'INERTIA_TEMPLATE_SHARED_ENTRIES',
|
|
156
|
+
ssr: this.#ssr,
|
|
157
|
+
modules: this.modules,
|
|
158
|
+
});
|
|
159
|
+
return {
|
|
160
|
+
...input,
|
|
161
|
+
input: named,
|
|
162
|
+
preserveEntrySignatures: input.preserveEntrySignatures === undefined || input.preserveEntrySignatures === false
|
|
163
|
+
? 'exports-only'
|
|
164
|
+
: input.preserveEntrySignatures,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
resolveId(source) {
|
|
168
|
+
return source.startsWith(PREFIX) ? source : null;
|
|
169
|
+
}
|
|
170
|
+
/** The entry text: every export of the module, and its default when it has one. */
|
|
171
|
+
async load(context, id) {
|
|
172
|
+
if (!id.startsWith(PREFIX))
|
|
173
|
+
return null;
|
|
174
|
+
const module = id.slice(PREFIX.length);
|
|
175
|
+
const spec = JSON.stringify(module);
|
|
176
|
+
const resolved = await context.resolve(module, join(this.#root, 'package.json'));
|
|
177
|
+
if (resolved === null)
|
|
178
|
+
throw new InertiaUsageError('INVALID_TEMPLATE_SHARED', `templates.shared: ${module} cannot be resolved from ${this.#root} (install it, or correct the name)`);
|
|
179
|
+
// An external (an installed package in the server build) stays an import; Node is asked
|
|
180
|
+
// whether it has a default export, as the built server code will import it.
|
|
181
|
+
let withDefault;
|
|
182
|
+
if (resolved.external) {
|
|
183
|
+
try {
|
|
184
|
+
withDefault = await externalHasDefault(this.#root, module);
|
|
185
|
+
}
|
|
186
|
+
catch (cause) {
|
|
187
|
+
throw new InertiaUsageError('INVALID_TEMPLATE_SHARED', `templates.shared: ${module} cannot be imported from ${this.#root} by Node: ${cause.message}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.#resolved.set(module, resolved.id.split('?', 1)[0]);
|
|
192
|
+
withDefault = (await context.load({ id: resolved.id })).exports?.includes('default') === true;
|
|
193
|
+
}
|
|
194
|
+
return `export * from ${spec};\n${withDefault ? `export { default } from ${spec};\n` : ''}`;
|
|
195
|
+
}
|
|
196
|
+
/** `writeBundle`: `dokebi-shared.json` beside the build's manifest. */
|
|
197
|
+
async writeBundle(bundle) {
|
|
198
|
+
if (!this.#build)
|
|
199
|
+
return;
|
|
200
|
+
const modules = {};
|
|
201
|
+
for (const chunk of Object.values(bundle)) {
|
|
202
|
+
const facade = chunk.facadeModuleId;
|
|
203
|
+
if (chunk.type !== 'chunk' || !chunk.isEntry || !facade?.startsWith(PREFIX))
|
|
204
|
+
continue;
|
|
205
|
+
const module = facade.slice(PREFIX.length);
|
|
206
|
+
const near = this.#resolved.get(module);
|
|
207
|
+
modules[module] = {
|
|
208
|
+
file: chunk.fileName,
|
|
209
|
+
version: packageVersion(this.#root, module) ??
|
|
210
|
+
(near === undefined ? null : versionNear(near, module)),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const missing = this.modules.filter((module) => modules[module] === undefined);
|
|
214
|
+
if (missing.length)
|
|
215
|
+
this.#logger.log('warn', 'Inertia build did not emit some shared module entries', {
|
|
216
|
+
code: 'INERTIA_TEMPLATE_SHARED_MISSING',
|
|
217
|
+
modules: missing,
|
|
218
|
+
fix: 'Keep the inputs dokebiInertia({ templates }) adds (do not replace build.rollupOptions.input in another plugin).',
|
|
219
|
+
});
|
|
220
|
+
const file = { format: 1, ssr: this.#ssr, base: this.#base, modules };
|
|
221
|
+
const path = join(this.#outDir, SHARED_FILE);
|
|
222
|
+
await mkdir(dirname(path), { recursive: true });
|
|
223
|
+
await writeFile(path, `${JSON.stringify(file, null, 2)}\n`);
|
|
224
|
+
this.#logger.log('info', 'Inertia build wrote the shared modules of template bundles', {
|
|
225
|
+
code: 'INERTIA_TEMPLATE_SHARED_WRITTEN',
|
|
226
|
+
file: path,
|
|
227
|
+
ssr: this.#ssr,
|
|
228
|
+
modules: Object.keys(modules),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { buildTemplateBundle, packageEntry, type TemplateBuildOptions, type TemplateBuildResult, templatePages, } from './template-build.js';
|
|
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 { type DokebiInertiaTemplateOptions, packageVersion, SHARED_FILE, type SharedFile, sharedEntryName, } from './template-shared.js';
|
package/dist/template.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// `@dokebi/template-inertia/template` (D-0291): template bundles — the format and its check, and
|
|
2
|
+
// the build that `dokebi template build` runs in a template repository.
|
|
3
|
+
export { buildTemplateBundle, packageEntry, templatePages, } from './template-build.js';
|
|
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 { packageVersion, SHARED_FILE, sharedEntryName, } from './template-shared.js';
|
package/dist/vite-plugin.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type Logger } from '@dokebi/core';
|
|
2
2
|
import { type PagesDevServer } from './module-pages.js';
|
|
3
|
+
import { type DokebiInertiaTemplateOptions, type InputOptionsShape } from './template-shared.js';
|
|
3
4
|
import { type DevFilesOptions, type DevFilesPatch, type DevMiddleware, type ResolvedFilesShape } from './vite-files.js';
|
|
4
5
|
export { MODULE_PAGES_ID } from './module-pages.js';
|
|
6
|
+
export { type DokebiInertiaTemplateOptions, SHARED_FILE, type SharedFile, } from './template-shared.js';
|
|
5
7
|
export { DEV_FILES_DENY, type DevFilesOptions, type DokebiDevFilesPlugin, dokebiDevFiles, } from './vite-files.js';
|
|
6
8
|
/** Specifier → the URL the app serves it at (@dokebi/seal, `GET /dokebi/seal/*`). */
|
|
7
9
|
export declare const SERVED_MODULES: Readonly<Record<string, string>>;
|
|
@@ -13,9 +15,21 @@ export interface DokebiInertiaViteOptions extends DevFilesOptions {
|
|
|
13
15
|
* `DOKEBI_LOG_LEVEL` (`info` when unset), the level `dokebi dev` passes to its processes.
|
|
14
16
|
*/
|
|
15
17
|
readonly logger?: Logger;
|
|
18
|
+
/**
|
|
19
|
+
* D-0291: provide the shared modules of template bundles (vue, @inertiajs/vue3,
|
|
20
|
+
* @dokebi/template-inertia/{i18n,pages} and `shared`, the app's SDK) as entries of this build,
|
|
21
|
+
* listed in `<outDir>/.vite/dokebi-shared.json`. Give the same value in the browser and the
|
|
22
|
+
* server vite config. Without it the build is as before.
|
|
23
|
+
*/
|
|
24
|
+
readonly templates?: DokebiInertiaTemplateOptions;
|
|
16
25
|
}
|
|
17
26
|
/** The part of Vite's resolved config this plugin reads. */
|
|
18
|
-
type ResolvedConfigShape = ResolvedFilesShape
|
|
27
|
+
type ResolvedConfigShape = ResolvedFilesShape & {
|
|
28
|
+
readonly build?: {
|
|
29
|
+
readonly ssr?: unknown;
|
|
30
|
+
readonly outDir?: string;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
19
33
|
/** The part of Vite's dev server this plugin uses (connect middlewares). */
|
|
20
34
|
interface DevServerShape extends PagesDevServer {
|
|
21
35
|
readonly middlewares: {
|
|
@@ -60,7 +74,11 @@ export interface DokebiInertiaVitePlugin {
|
|
|
60
74
|
id: string;
|
|
61
75
|
external: true | 'absolute';
|
|
62
76
|
} | string | null;
|
|
63
|
-
load(id: string): string | null | Promise<string | null>;
|
|
77
|
+
load(this: unknown, id: string): string | null | Promise<string | null>;
|
|
78
|
+
/** Rollup's input options (D-0291: the shared entries join the inputs). */
|
|
79
|
+
options(input: InputOptionsShape): InputOptionsShape | null;
|
|
80
|
+
/** D-0291: writes dokebi-shared.json after a build. */
|
|
81
|
+
writeBundle(output: unknown, bundle: Readonly<Record<string, never>>): Promise<void>;
|
|
64
82
|
configureServer(server: DevServerShape): void;
|
|
65
83
|
/** Vite calls it when the dev server closes (or restarts) and after a build. */
|
|
66
84
|
closeBundle(): void;
|
package/dist/vite-plugin.js
CHANGED
|
@@ -29,8 +29,11 @@
|
|
|
29
29
|
// remote hosts), so an upgrade of this package secures every vite.config that uses it.
|
|
30
30
|
import { ConsoleLogger } from '@dokebi/core';
|
|
31
31
|
import { MODULE_PAGES_ID, ModulePagesSource } from './module-pages.js';
|
|
32
|
+
import { SharedEntries, } from './template-shared.js';
|
|
32
33
|
import { DevFilesPolicy, } from './vite-files.js';
|
|
34
|
+
import { registerVueTypeScript } from './vue-typescript.js';
|
|
33
35
|
export { MODULE_PAGES_ID } from './module-pages.js';
|
|
36
|
+
export { SHARED_FILE, } from './template-shared.js';
|
|
34
37
|
export { DEV_FILES_DENY, dokebiDevFiles, } from './vite-files.js';
|
|
35
38
|
/** Specifier → the URL the app serves it at (@dokebi/seal, `GET /dokebi/seal/*`). */
|
|
36
39
|
export const SERVED_MODULES = Object.freeze({
|
|
@@ -92,6 +95,8 @@ export function dokebiInertia(options = {}) {
|
|
|
92
95
|
const dev = new Map(Object.values(SERVED_MODULES).map((served) => [devId(served), served]));
|
|
93
96
|
/** Request path (with and without `base`) → the module text the dev server answers. */
|
|
94
97
|
const answers = new Map();
|
|
98
|
+
// D-0291: the shared modules of template bundles, when the app opts in.
|
|
99
|
+
const shared = options.templates ? new SharedEntries(logger, options.templates) : undefined;
|
|
95
100
|
return {
|
|
96
101
|
name: 'dokebi-inertia',
|
|
97
102
|
enforce: 'pre',
|
|
@@ -111,6 +116,17 @@ export function dokebiInertia(options = {}) {
|
|
|
111
116
|
},
|
|
112
117
|
configResolved(config) {
|
|
113
118
|
files.configResolved(config);
|
|
119
|
+
// D-0291 follow-up: props types imported from packages need a TypeScript with a JS API.
|
|
120
|
+
registerVueTypeScript(config.root ?? process.cwd(), logger);
|
|
121
|
+
shared?.configResolved({
|
|
122
|
+
root: config.root ?? process.cwd(),
|
|
123
|
+
command: config.command,
|
|
124
|
+
base: config.base,
|
|
125
|
+
build: {
|
|
126
|
+
ssr: config.build?.ssr,
|
|
127
|
+
outDir: config.build?.outDir ?? 'dist',
|
|
128
|
+
},
|
|
129
|
+
});
|
|
114
130
|
command = config.command;
|
|
115
131
|
pages.command = command;
|
|
116
132
|
base = config.base || '/';
|
|
@@ -126,9 +142,18 @@ export function dokebiInertia(options = {}) {
|
|
|
126
142
|
modules: SERVED_MODULES,
|
|
127
143
|
});
|
|
128
144
|
},
|
|
145
|
+
options(input) {
|
|
146
|
+
return shared ? shared.options(input) : null;
|
|
147
|
+
},
|
|
148
|
+
async writeBundle(_output, bundle) {
|
|
149
|
+
await shared?.writeBundle(bundle);
|
|
150
|
+
},
|
|
129
151
|
resolveId(source, importer, resolveOptions) {
|
|
130
152
|
if (source === MODULE_PAGES_ID)
|
|
131
153
|
return pages.resolve(source, importer);
|
|
154
|
+
const entry = shared?.resolveId(source);
|
|
155
|
+
if (entry)
|
|
156
|
+
return entry;
|
|
132
157
|
const served = SERVED_MODULES[source];
|
|
133
158
|
if (served === undefined)
|
|
134
159
|
return null;
|
|
@@ -148,6 +173,8 @@ export function dokebiInertia(options = {}) {
|
|
|
148
173
|
load(id) {
|
|
149
174
|
if (id.startsWith('\0dokebi-module-pages:'))
|
|
150
175
|
return pages.load(id);
|
|
176
|
+
if (shared && id.startsWith('\0dokebi-shared:'))
|
|
177
|
+
return shared.load(this, id);
|
|
151
178
|
// Vite pre-transforms the imports it sees, the dev re-export too. The middleware below
|
|
152
179
|
// answers its URL before Vite does, so this text only fills Vite's module graph; were it
|
|
153
180
|
// ever served, the page would fail loudly instead of running without the served module.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
/** The TypeScript packages tried, in order: the first with a JS API is registered. */
|
|
3
|
+
export declare const VUE_TYPESCRIPT_CANDIDATES: readonly ['typescript', '@typescript/typescript6'];
|
|
4
|
+
/**
|
|
5
|
+
* Registers a TypeScript with a JS API on the project's vue/compiler-sfc (once per root and
|
|
6
|
+
* process). Returns the package registered, `typescript-7` when none has a JS API, or undefined
|
|
7
|
+
* when the project has no vue/compiler-sfc.
|
|
8
|
+
*/
|
|
9
|
+
export declare function registerVueTypeScript(root: string, logger: Logger): string | undefined;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// D-0291 follow-up: the Vue SFC compiler resolves the props types a page imports from a package
|
|
2
|
+
// (`defineProps<Props>()` with `import type { Props } from '@shop/sdk'`) through TypeScript's JS
|
|
3
|
+
// API (`ts.findConfigFile`, module resolution). vue/compiler-sfc registers `require('typescript')`
|
|
4
|
+
// for that the first time it is imported; TypeScript 7 (the native compiler) has no JS API, so the
|
|
5
|
+
// build fails with "ts2.findConfigFile is not a function". This registers a TypeScript that has
|
|
6
|
+
// one instead: the project's `typescript` when it is 6 or older, else `@typescript/typescript6`
|
|
7
|
+
// (TypeScript 6 as a package beside 7). Without either nothing changes (and only builds whose
|
|
8
|
+
// pages import types from packages need it).
|
|
9
|
+
import { createRequire } from 'node:module';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
/** The TypeScript packages tried, in order: the first with a JS API is registered. */
|
|
12
|
+
export const VUE_TYPESCRIPT_CANDIDATES = ['typescript', '@typescript/typescript6'];
|
|
13
|
+
const done = new Set();
|
|
14
|
+
/**
|
|
15
|
+
* Registers a TypeScript with a JS API on the project's vue/compiler-sfc (once per root and
|
|
16
|
+
* process). Returns the package registered, `typescript-7` when none has a JS API, or undefined
|
|
17
|
+
* when the project has no vue/compiler-sfc.
|
|
18
|
+
*/
|
|
19
|
+
export function registerVueTypeScript(root, logger) {
|
|
20
|
+
const require = createRequire(join(root, 'package.json'));
|
|
21
|
+
let sfc;
|
|
22
|
+
try {
|
|
23
|
+
// Importing it first runs its own registration, which this one then replaces.
|
|
24
|
+
sfc = require('vue/compiler-sfc');
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
logger.log('debug', 'Vue SFC compiler not found; no TypeScript registered for its types', {
|
|
28
|
+
root,
|
|
29
|
+
error: error.message,
|
|
30
|
+
});
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
if (typeof sfc.registerTS !== 'function')
|
|
34
|
+
return undefined;
|
|
35
|
+
for (const name of VUE_TYPESCRIPT_CANDIDATES) {
|
|
36
|
+
let ts;
|
|
37
|
+
try {
|
|
38
|
+
ts = require(name);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
logger.log('debug', 'TypeScript candidate for the Vue SFC compiler not installed', {
|
|
42
|
+
package: name,
|
|
43
|
+
error: error.message,
|
|
44
|
+
});
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (typeof ts.findConfigFile !== 'function')
|
|
48
|
+
continue;
|
|
49
|
+
if (!done.has(`${root} ${name}`)) {
|
|
50
|
+
done.add(`${root} ${name}`);
|
|
51
|
+
logger.log('debug', 'TypeScript registered for the Vue SFC compiler', {
|
|
52
|
+
code: 'INERTIA_VUE_TYPESCRIPT',
|
|
53
|
+
package: name,
|
|
54
|
+
version: String(ts.version ?? ''),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
sfc.registerTS(() => ts);
|
|
58
|
+
return name;
|
|
59
|
+
}
|
|
60
|
+
if (!done.has(`${root} none`)) {
|
|
61
|
+
done.add(`${root} none`);
|
|
62
|
+
logger.log('debug', 'No TypeScript with a JS API for the Vue SFC compiler (TypeScript 7)', {
|
|
63
|
+
code: 'INERTIA_VUE_TYPESCRIPT',
|
|
64
|
+
fix: 'Pages that import their props types from a package need it: npm install --save-dev @typescript/typescript6',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return 'typescript-7';
|
|
68
|
+
}
|
package/options.schema.json
CHANGED
|
@@ -228,9 +228,87 @@
|
|
|
228
228
|
"pattern": "^[A-Za-z][A-Za-z0-9._-]{0,63}$",
|
|
229
229
|
"x-dokebi-secret": false,
|
|
230
230
|
"description": "Id of the GET <vite base>* routes of the Vite dev server (dokebi dev, one per base); default template-inertia.viteDev"
|
|
231
|
+
},
|
|
232
|
+
"templateFiles": {
|
|
233
|
+
"type": "string",
|
|
234
|
+
"pattern": "^[A-Za-z][A-Za-z0-9._-]{0,63}$",
|
|
235
|
+
"x-dokebi-secret": false,
|
|
236
|
+
"description": "Id of D-0291: the GET /_dokebi/templates/* route of the browser files of template bundles; default template-inertia.templateFiles"
|
|
231
237
|
}
|
|
232
238
|
},
|
|
233
|
-
"description": "Ids of the routes dokebi-template-inertia adds (D-0142), by route key: assets (default template-inertia.assets), viteDev (default template-inertia.viteDev). An id is 1-64 letters, digits, '.', '_' or '-' starting with a letter; an id another route of the same app has fails the start (DUPLICATE_ROUTE_ID)"
|
|
239
|
+
"description": "Ids of the routes dokebi-template-inertia adds (D-0142), by route key: assets (default template-inertia.assets), viteDev (default template-inertia.viteDev), templateFiles (default template-inertia.templateFiles). An id is 1-64 letters, digits, '.', '_' or '-' starting with a letter; an id another route of the same app has fails the start (DUPLICATE_ROUTE_ID)"
|
|
240
|
+
},
|
|
241
|
+
"templates": {
|
|
242
|
+
"type": "object",
|
|
243
|
+
"propertyNames": {
|
|
244
|
+
"pattern": "^[a-z][a-z0-9-]{0,31}$"
|
|
245
|
+
},
|
|
246
|
+
"maxProperties": 64,
|
|
247
|
+
"additionalProperties": {
|
|
248
|
+
"type": "object",
|
|
249
|
+
"propertyNames": {
|
|
250
|
+
"pattern": "^[a-z][a-z0-9-]{0,31}$"
|
|
251
|
+
},
|
|
252
|
+
"maxProperties": 64,
|
|
253
|
+
"additionalProperties": {
|
|
254
|
+
"type": "object",
|
|
255
|
+
"additionalProperties": false,
|
|
256
|
+
"properties": {
|
|
257
|
+
"path": {
|
|
258
|
+
"type": "string",
|
|
259
|
+
"minLength": 1,
|
|
260
|
+
"maxLength": 4096,
|
|
261
|
+
"description": "The bundle folder (dokebi-template.json, browser/, ssr/), absolute or relative to the working directory: a read-only mount is fine"
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
"required": [
|
|
265
|
+
"path"
|
|
266
|
+
]
|
|
267
|
+
},
|
|
268
|
+
"description": "Template name (dokebi-app domains.<host>.template) → its bundle"
|
|
269
|
+
},
|
|
270
|
+
"default": {},
|
|
271
|
+
"x-dokebi-reloadable": true,
|
|
272
|
+
"description": "D-0291: template bundles built apart from the app (npx dokebi template build), per app and template. A host whose template has a bundle gets its pages (then the app views/<template>, views/default and module pages, as before); a missing, broken or refused bundle leaves the template to the app pages and never stops the start. Not used under dokebi dev (Vite)."
|
|
273
|
+
},
|
|
274
|
+
"templateCheckMs": {
|
|
275
|
+
"type": "integer",
|
|
276
|
+
"minimum": 0,
|
|
277
|
+
"maximum": 3600000,
|
|
278
|
+
"default": 5000,
|
|
279
|
+
"x-dokebi-reloadable": true,
|
|
280
|
+
"description": "D-0291: how often each bundle folder's dokebi-template.json is looked at; a changed bundle is loaded beside the current one and replaces it for the next request. 0: only at start and reloads"
|
|
281
|
+
},
|
|
282
|
+
"templateMaxBytes": {
|
|
283
|
+
"type": "integer",
|
|
284
|
+
"minimum": 1024,
|
|
285
|
+
"maximum": 1073741824,
|
|
286
|
+
"default": 52428800,
|
|
287
|
+
"x-dokebi-reloadable": true,
|
|
288
|
+
"description": "D-0291: the largest template bundle (its files), 50 MiB by default"
|
|
289
|
+
},
|
|
290
|
+
"templateKeep": {
|
|
291
|
+
"type": "integer",
|
|
292
|
+
"minimum": 1,
|
|
293
|
+
"maximum": 16,
|
|
294
|
+
"default": 2,
|
|
295
|
+
"x-dokebi-reloadable": true,
|
|
296
|
+
"description": "D-0291: generations of a template kept in the cache and served (the current one and those before it), so pages opened before a swap still load their files"
|
|
297
|
+
},
|
|
298
|
+
"templateVerifyTimeoutMs": {
|
|
299
|
+
"type": "integer",
|
|
300
|
+
"minimum": 100,
|
|
301
|
+
"maximum": 600000,
|
|
302
|
+
"default": 10000,
|
|
303
|
+
"x-dokebi-reloadable": true,
|
|
304
|
+
"description": "D-0291: how long the entry's verifyTemplate may take for one bundle; past it the bundle is refused"
|
|
305
|
+
},
|
|
306
|
+
"templateCacheDir": {
|
|
307
|
+
"type": "string",
|
|
308
|
+
"minLength": 1,
|
|
309
|
+
"maxLength": 4096,
|
|
310
|
+
"default": ".dokebi/cache/templates",
|
|
311
|
+
"description": "D-0291: where checked copies of the bundles go (relative to the working directory); a writable folder of the process"
|
|
234
312
|
}
|
|
235
313
|
}
|
|
236
314
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dokebi/template-inertia",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.21",
|
|
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",
|
|
@@ -38,6 +38,10 @@
|
|
|
38
38
|
"types": "./dist/pages.d.ts",
|
|
39
39
|
"import": "./dist/pages.js"
|
|
40
40
|
},
|
|
41
|
+
"./template": {
|
|
42
|
+
"types": "./dist/template.d.ts",
|
|
43
|
+
"default": "./dist/template.js"
|
|
44
|
+
},
|
|
41
45
|
"./module-pages": {
|
|
42
46
|
"types": "./module-pages.d.ts"
|
|
43
47
|
}
|
|
@@ -145,7 +149,7 @@
|
|
|
145
149
|
"devDependencies": {
|
|
146
150
|
"@dokebi/i18n": "0.2.9",
|
|
147
151
|
"@dokebi/session": "0.4.11",
|
|
148
|
-
"@dokebi/web": "0.4.
|
|
152
|
+
"@dokebi/web": "0.4.27",
|
|
149
153
|
"@dokebi/websocket": "0.4.10",
|
|
150
154
|
"@dokebi/websocket-hub": "0.4.5",
|
|
151
155
|
"@inertiajs/vue3": "3.9.1",
|
package/templates/shared.ts
CHANGED
|
@@ -5,3 +5,7 @@ import type { FastifyRequest } from 'fastify';
|
|
|
5
5
|
export async function shared(_request: FastifyRequest) {
|
|
6
6
|
return {};
|
|
7
7
|
}
|
|
8
|
+
|
|
9
|
+
// D-0291: with template bundles (options.dokebi-template-inertia.templates), export
|
|
10
|
+
// verifyTemplate(bundle) here too: it judges a bundle (its signature, each page's meta.contract)
|
|
11
|
+
// before the app uses it; in production a bundle is not used without it (AI.md §4.3).
|