fondu 0.9.3 → 0.9.5
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/README.md +1 -1
- package/dist/paths.d.ts +4 -1
- package/dist/paths.js +7 -2
- package/dist/vite/index.js +2 -0
- package/dist/vite/layouts.d.ts +2 -0
- package/dist/vite/layouts.js +46 -15
- package/dist/vite/vitest-cache.d.ts +19 -0
- package/dist/vite/vitest-cache.js +30 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Fondu
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://fondu.dev)
|
|
4
4
|
|
|
5
5
|
Fondu is a small meta-framework for full-stack React apps, built on Hono, Vite and Inertia. It runs on Bun, Cloudflare
|
|
6
6
|
Workers and Node.
|
package/dist/paths.d.ts
CHANGED
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
export declare function routePatterns(file: string): string[];
|
|
12
12
|
/** a path inside `routes/` that is a route file: every `.ts` file but `_middleware.ts`, tests and declarations */
|
|
13
13
|
export declare function isRouteFile(file: string): boolean;
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* a path inside `pages/` that is a page: a .tsx, .jsx or .mdx file but tests (as for routes), neither its name nor a
|
|
16
|
+
* directory on its path starting with `_` (`_layout.tsx`, `_parts/badge.tsx`)
|
|
17
|
+
*/
|
|
15
18
|
export declare function isPageFile(file: string): boolean;
|
|
16
19
|
/** the directories of `pages/` whose `_layout.tsx` wraps a page, outermost first: `docs/intro.mdx` → `['', 'docs']` */
|
|
17
20
|
export declare function layoutsOf(file: string): string[];
|
package/dist/paths.js
CHANGED
|
@@ -35,9 +35,14 @@ export function isRouteFile(file) {
|
|
|
35
35
|
!/\.(test|spec)\.ts$/.test(file) &&
|
|
36
36
|
file.split('/').at(-1) !== '_middleware.ts');
|
|
37
37
|
}
|
|
38
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* a path inside `pages/` that is a page: a .tsx, .jsx or .mdx file but tests (as for routes), neither its name nor a
|
|
40
|
+
* directory on its path starting with `_` (`_layout.tsx`, `_parts/badge.tsx`)
|
|
41
|
+
*/
|
|
39
42
|
export function isPageFile(file) {
|
|
40
|
-
return /\.(?:[jt]sx|mdx)$/.test(file) &&
|
|
43
|
+
return (/\.(?:[jt]sx|mdx)$/.test(file) &&
|
|
44
|
+
!/\.(?:test|spec)\.[jt]sx$/.test(file) &&
|
|
45
|
+
!file.split('/').some((s) => s.startsWith('_')));
|
|
41
46
|
}
|
|
42
47
|
/** the directories of `pages/` whose `_layout.tsx` wraps a page, outermost first: `docs/intro.mdx` → `['', 'docs']` */
|
|
43
48
|
export function layoutsOf(file) {
|
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,18 +24,21 @@ 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
|
-
const chain = layoutsOf(rel)
|
|
28
|
-
.map(layoutFile)
|
|
29
|
-
.filter((f) => f !== undefined);
|
|
30
|
-
if (!chain.length)
|
|
31
|
-
return;
|
|
32
30
|
const found = defaultExport(this.parse(code), code);
|
|
33
|
-
|
|
31
|
+
// a component put in app/pages/ by mistake would be a page, named and typed: an error instead
|
|
32
|
+
if (!found) {
|
|
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 _`);
|
|
34
|
+
}
|
|
35
|
+
if (found.name === undefined)
|
|
34
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;
|
|
35
42
|
// added at the end (imports are hoisted) and within a line: the lines stay where they were (`map: null`)
|
|
36
43
|
const imports = chain
|
|
37
44
|
.map((f, i) => `import __fondu_layout_${i} from ${JSON.stringify(f)};`)
|
|
@@ -54,17 +61,41 @@ export function layouts() {
|
|
|
54
61
|
},
|
|
55
62
|
};
|
|
56
63
|
}
|
|
64
|
+
const nameOf = (n) => n.name ?? n.value;
|
|
57
65
|
/**
|
|
58
66
|
* The name of the module's default export, to set `layout` on: a named function or class as it is; an anonymous one,
|
|
59
|
-
* or an expression, given a name first (`const __fondu_page = …`, exported at the end)
|
|
67
|
+
* or an expression, given a name first (`const __fondu_page = …`, exported at the end). A component re-exported from
|
|
68
|
+
* another module (`export { default } from './edit.tsx'`) is wrapped in one of this page's own: two pages may share a
|
|
69
|
+
* component under different layouts. `name: undefined`: a default export to leave alone (`export * as default`).
|
|
60
70
|
*/
|
|
61
71
|
function defaultExport(program, code) {
|
|
62
72
|
for (const node of program.body) {
|
|
63
|
-
if (node.type === '
|
|
73
|
+
if (node.type === 'ExportAllDeclaration' && node.exported)
|
|
74
|
+
if (nameOf(node.exported) === 'default')
|
|
75
|
+
return { name: undefined, code };
|
|
76
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
64
77
|
const specifiers = node.specifiers;
|
|
65
|
-
const specifier = specifiers.find((sp) => sp.exported
|
|
66
|
-
if (specifier)
|
|
67
|
-
|
|
78
|
+
const specifier = specifiers.find((sp) => nameOf(sp.exported) === 'default');
|
|
79
|
+
if (!specifier)
|
|
80
|
+
continue;
|
|
81
|
+
if (!node.source)
|
|
82
|
+
return { name: nameOf(specifier.local), code };
|
|
83
|
+
const source = code.slice(node.source.start, node.source.end);
|
|
84
|
+
const others = specifiers.filter((sp) => sp !== specifier);
|
|
85
|
+
const kept = others.length
|
|
86
|
+
? `export { ${others.map((sp) => code.slice(sp.start, sp.end)).join(', ')} } from ${source};`
|
|
87
|
+
: '';
|
|
88
|
+
const local = nameOf(specifier.local);
|
|
89
|
+
const imported = /^[A-Za-z_$][\w$]*$/.test(local) ? local : JSON.stringify(local);
|
|
90
|
+
return {
|
|
91
|
+
name: '__fondu_page',
|
|
92
|
+
wrapped: true,
|
|
93
|
+
code: `${code.slice(0, node.start)}${kept}${code.slice(node.end)}\n` +
|
|
94
|
+
`import { ${imported} as __fondu_shared } from ${source};\n` +
|
|
95
|
+
`import { createElement as __fondu_h } from 'react';\n` +
|
|
96
|
+
`const __fondu_page = (props) => __fondu_h(__fondu_shared, props);\n` +
|
|
97
|
+
`export default __fondu_page;`,
|
|
98
|
+
};
|
|
68
99
|
}
|
|
69
100
|
if (node.type !== 'ExportDefaultDeclaration')
|
|
70
101
|
continue;
|
|
@@ -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
|
+
}
|