fondu 0.9.4 → 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.
@@ -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(),
@@ -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
@@ -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 file = id.split('?')[0];
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
- const chain = layoutsOf(rel)
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` +
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "A small meta-framework for full-stack React apps, built on Hono, Vite and Inertia. It runs on Bun, Cloudflare Workers and Node.",
5
5
  "keywords": [
6
6
  "bun",