fondu 0.9.4 → 0.9.6
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/dist/vite/index.js +2 -0
- package/dist/vite/layouts.d.ts +2 -0
- package/dist/vite/layouts.js +14 -8
- package/dist/vite/prerender.d.ts +6 -2
- package/dist/vite/prerender.js +35 -7
- package/dist/vite/vitest-cache.d.ts +19 -0
- package/dist/vite/vitest-cache.js +30 -0
- package/package.json +1 -1
package/dist/vite/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import { hmr, PAGE_EXPORTS, PROPS_EVENT } from './hmr.js';
|
|
|
11
11
|
import { layouts } from './layouts.js';
|
|
12
12
|
import { nodeServer, prerender, worker } from './prerender.js';
|
|
13
13
|
import { typegen } from './typegen.js';
|
|
14
|
+
import { vitestCache } from './vitest-cache.js';
|
|
14
15
|
const ENTRY = 'app/app.tsx';
|
|
15
16
|
/** what fondu's server code imports from Hono (test/hono.test.ts keeps the list complete) */
|
|
16
17
|
export const HONO = [
|
|
@@ -95,6 +96,7 @@ export default function fondu(options) {
|
|
|
95
96
|
inertia({ ssr: options.ssr === false ? false : {}, frameworks: inertiaInProcess }),
|
|
96
97
|
core(options),
|
|
97
98
|
layouts(),
|
|
99
|
+
vitestCache(ENTRY),
|
|
98
100
|
hmr(),
|
|
99
101
|
loadFailure(),
|
|
100
102
|
typegen(),
|
package/dist/vite/layouts.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { Plugin } from 'vite';
|
|
2
|
+
/** the `_layout.tsx` files around a page (its path in `app/pages/`), outermost first */
|
|
3
|
+
export declare function layoutChain(root: string, page: string): string[];
|
|
2
4
|
/**
|
|
3
5
|
* `_layout.tsx` in a directory of `app/pages/` wraps the pages below it, outermost first: each page module imports
|
|
4
6
|
* its chain and sets it as the component's `layout` (Inertia's page-level layout), unless the page set its own. So a
|
package/dist/vite/layouts.js
CHANGED
|
@@ -2,6 +2,12 @@ import { existsSync } from 'node:fs';
|
|
|
2
2
|
import { join, relative, sep } from 'node:path';
|
|
3
3
|
import { isPageFile, layoutsOf } from '../paths.js';
|
|
4
4
|
const LAYOUT_EXTENSIONS = ['.tsx', '.jsx'];
|
|
5
|
+
/** the `_layout.tsx` files around a page (its path in `app/pages/`), outermost first */
|
|
6
|
+
export function layoutChain(root, page) {
|
|
7
|
+
return layoutsOf(page)
|
|
8
|
+
.map((dir) => LAYOUT_EXTENSIONS.map((ext) => join(root, 'app/pages', dir, `_layout${ext}`)).find((f) => existsSync(f)))
|
|
9
|
+
.filter((f) => f !== undefined);
|
|
10
|
+
}
|
|
5
11
|
/**
|
|
6
12
|
* `_layout.tsx` in a directory of `app/pages/` wraps the pages below it, outermost first: each page module imports
|
|
7
13
|
* its chain and sets it as the component's `layout` (Inertia's page-level layout), unless the page set its own. So a
|
|
@@ -11,8 +17,6 @@ const LAYOUT_EXTENSIONS = ['.tsx', '.jsx'];
|
|
|
11
17
|
export function layouts() {
|
|
12
18
|
let root = process.cwd();
|
|
13
19
|
const pagesDir = () => join(root, 'app/pages');
|
|
14
|
-
// the `_layout.tsx` of a directory of app/pages/ ('' is the root), if there is one
|
|
15
|
-
const layoutFile = (dir) => LAYOUT_EXTENSIONS.map((ext) => join(pagesDir(), dir, `_layout${ext}`)).find((f) => existsSync(f));
|
|
16
20
|
return {
|
|
17
21
|
name: 'fondu:layouts',
|
|
18
22
|
enforce: 'post', // after the JSX and MDX are compiled: plain JavaScript to read
|
|
@@ -20,8 +24,7 @@ export function layouts() {
|
|
|
20
24
|
root = c.root;
|
|
21
25
|
},
|
|
22
26
|
transform(code, id) {
|
|
23
|
-
const
|
|
24
|
-
const rel = relative(pagesDir(), file).split(sep).join('/');
|
|
27
|
+
const rel = relative(pagesDir(), id.split('?')[0]).split(sep).join('/');
|
|
25
28
|
if (rel.startsWith('..') || !isPageFile(rel))
|
|
26
29
|
return;
|
|
27
30
|
const found = defaultExport(this.parse(code), code);
|
|
@@ -29,11 +32,13 @@ export function layouts() {
|
|
|
29
32
|
if (!found) {
|
|
30
33
|
this.error(`fondu: app/pages/${rel} has no default export. A page is its module's default export; a file in app/pages/ that is not a page goes elsewhere (components/, say), or its name starts with _`);
|
|
31
34
|
}
|
|
32
|
-
|
|
33
|
-
.map(layoutFile)
|
|
34
|
-
.filter((f) => f !== undefined);
|
|
35
|
-
if (!chain.length || found.name === undefined)
|
|
35
|
+
if (found.name === undefined)
|
|
36
36
|
return;
|
|
37
|
+
const chain = layoutChain(root, rel);
|
|
38
|
+
// a re-exported component is wrapped with no layout above too: the app's default layout for this page, not the
|
|
39
|
+
// layouts its other page set on it
|
|
40
|
+
if (!chain.length)
|
|
41
|
+
return found.wrapped ? { code: found.code, map: null } : undefined;
|
|
37
42
|
// added at the end (imports are hoisted) and within a line: the lines stay where they were (`map: null`)
|
|
38
43
|
const imports = chain
|
|
39
44
|
.map((f, i) => `import __fondu_layout_${i} from ${JSON.stringify(f)};`)
|
|
@@ -84,6 +89,7 @@ function defaultExport(program, code) {
|
|
|
84
89
|
const imported = /^[A-Za-z_$][\w$]*$/.test(local) ? local : JSON.stringify(local);
|
|
85
90
|
return {
|
|
86
91
|
name: '__fondu_page',
|
|
92
|
+
wrapped: true,
|
|
87
93
|
code: `${code.slice(0, node.start)}${kept}${code.slice(node.end)}\n` +
|
|
88
94
|
`import { ${imported} as __fondu_shared } from ${source};\n` +
|
|
89
95
|
`import { createElement as __fondu_h } from 'react';\n` +
|
package/dist/vite/prerender.d.ts
CHANGED
|
@@ -16,8 +16,12 @@ export declare function ownScripts(html: string, policy: string | undefined): {
|
|
|
16
16
|
* allowed by their hashes instead, and `'self'` scripts
|
|
17
17
|
*/
|
|
18
18
|
export declare function staticPolicy(policy: string, hashes?: string[]): string;
|
|
19
|
-
/**
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* `_headers` with Fondu's rules in it: the documents' security headers (`/*`), and the page objects' X-Inertia
|
|
21
|
+
* (Inertia requires it). A path the file has a rule for gets the headers it doesn't set there, so the app's own stay
|
|
22
|
+
* (Workers keep one rule per path: a second `/*` would replace the first); any other, a rule at the end.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withHeaderRules(file: string, headers: Record<string, string>): string;
|
|
21
25
|
/** the built server, running for the prerender: fetch a path of it, stop it */
|
|
22
26
|
interface Server {
|
|
23
27
|
fetch(path: string, headers?: Record<string, string>): Promise<Response>;
|
package/dist/vite/prerender.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
|
-
import {
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { createRequire } from 'node:module';
|
|
5
5
|
import { createServer } from 'node:net';
|
|
6
6
|
import { dirname, join, resolve } from 'node:path';
|
|
@@ -47,10 +47,35 @@ export function staticPolicy(policy, hashes = []) {
|
|
|
47
47
|
.filter(Boolean)
|
|
48
48
|
.join('; ');
|
|
49
49
|
}
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
/**
|
|
51
|
+
* `_headers` with Fondu's rules in it: the documents' security headers (`/*`), and the page objects' X-Inertia
|
|
52
|
+
* (Inertia requires it). A path the file has a rule for gets the headers it doesn't set there, so the app's own stay
|
|
53
|
+
* (Workers keep one rule per path: a second `/*` would replace the first); any other, a rule at the end.
|
|
54
|
+
*/
|
|
55
|
+
export function withHeaderRules(file, headers) {
|
|
56
|
+
const lines = file.replace(/\n+$/, '').split('\n');
|
|
57
|
+
const rules = [
|
|
58
|
+
['/*', headers],
|
|
59
|
+
[`${PAGE_DATA}/*`, { 'X-Inertia': 'true' }],
|
|
60
|
+
];
|
|
61
|
+
for (const [path, set] of rules) {
|
|
62
|
+
const start = lines.findIndex((line) => line.trimEnd() === path);
|
|
63
|
+
let end = start + 1;
|
|
64
|
+
while (start !== -1 && /^\s+\S/.test(lines[end] ?? ''))
|
|
65
|
+
end++;
|
|
66
|
+
// the names set or removed (`! name`) there
|
|
67
|
+
const own = new Set(lines
|
|
68
|
+
.slice(start + 1, end)
|
|
69
|
+
.map((l) => l.trim().replace(/^!\s*/, '').split(':')[0].toLowerCase()));
|
|
70
|
+
const added = Object.entries(set)
|
|
71
|
+
.filter(([name]) => start === -1 || !own.has(name.toLowerCase()))
|
|
72
|
+
.map(([name, value]) => ` ${name}: ${value}`);
|
|
73
|
+
if (start === -1)
|
|
74
|
+
lines.push(...(lines.at(-1) ? [''] : []), path, ...added);
|
|
75
|
+
else
|
|
76
|
+
lines.splice(end, 0, ...added);
|
|
77
|
+
}
|
|
78
|
+
return `${lines.join('\n')}\n`;
|
|
54
79
|
}
|
|
55
80
|
/**
|
|
56
81
|
* After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
|
|
@@ -115,8 +140,11 @@ export async function prerender(start, clientDir, logger, headersFile) {
|
|
|
115
140
|
}
|
|
116
141
|
if (errors.length)
|
|
117
142
|
return fail(errors);
|
|
118
|
-
if (headersFile)
|
|
119
|
-
|
|
143
|
+
if (headersFile) {
|
|
144
|
+
const file = join(clientDir, '_headers');
|
|
145
|
+
const own = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
146
|
+
writeFileSync(file, withHeaderRules(own, security ?? {}));
|
|
147
|
+
}
|
|
120
148
|
}
|
|
121
149
|
finally {
|
|
122
150
|
await server.dispose();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
type CacheKeyGenerator = (context: {
|
|
3
|
+
id: string;
|
|
4
|
+
}) => string | false | undefined;
|
|
5
|
+
type CacheKeyContext = {
|
|
6
|
+
defineCacheKeyGenerator?: (generator: CacheKeyGenerator) => void;
|
|
7
|
+
/** Vitest 4's name for it */
|
|
8
|
+
experimental_defineCacheKeyGenerator?: (generator: CacheKeyGenerator) => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Vitest's file cache keys a transformed module by its code. Two modules' transforms read more than that, so the next
|
|
12
|
+
* run would see them as they were: a page's layout chain (the `_layout.tsx` files above it) goes in its key; the
|
|
13
|
+
* entry, `app/app.tsx`, is not cached, its transform writing the list of pages (`@inertiajs/vite`'s glob) and the code
|
|
14
|
+
* Fondu's options add. Vitest itself skips only modules whose own code has `import.meta.glob`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function vitestCache(entry: string): Plugin & {
|
|
17
|
+
configureVitest(context: CacheKeyContext): void;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { relative, sep } from 'node:path';
|
|
2
|
+
import { isPageFile } from '../paths.js';
|
|
3
|
+
import { layoutChain } from './layouts.js';
|
|
4
|
+
/**
|
|
5
|
+
* Vitest's file cache keys a transformed module by its code. Two modules' transforms read more than that, so the next
|
|
6
|
+
* run would see them as they were: a page's layout chain (the `_layout.tsx` files above it) goes in its key; the
|
|
7
|
+
* entry, `app/app.tsx`, is not cached, its transform writing the list of pages (`@inertiajs/vite`'s glob) and the code
|
|
8
|
+
* Fondu's options add. Vitest itself skips only modules whose own code has `import.meta.glob`.
|
|
9
|
+
*/
|
|
10
|
+
export function vitestCache(entry) {
|
|
11
|
+
let root = process.cwd();
|
|
12
|
+
return {
|
|
13
|
+
name: 'fondu:vitest-cache',
|
|
14
|
+
configResolved(c) {
|
|
15
|
+
root = c.root;
|
|
16
|
+
},
|
|
17
|
+
configureVitest(context) {
|
|
18
|
+
const define = context.defineCacheKeyGenerator ?? context.experimental_defineCacheKeyGenerator;
|
|
19
|
+
define?.(({ id }) => {
|
|
20
|
+
const file = relative(root, id.split('?')[0]).split(sep).join('/');
|
|
21
|
+
if (file === entry)
|
|
22
|
+
return false;
|
|
23
|
+
const page = file.startsWith('app/pages/') ? file.slice('app/pages/'.length) : undefined;
|
|
24
|
+
if (page === undefined || !isPageFile(page))
|
|
25
|
+
return undefined;
|
|
26
|
+
return `fondu:layouts:${layoutChain(root, page).join('|')}`;
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|