@pajecawav/yamf 0.0.6 → 0.0.7

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # yamf
2
2
 
3
- SSR meta-framework on top of [Vite](https://vite.dev), [Nitro](https://nitro.build/), and [Hono JSX](https://hono.dev/docs/guides/jsx). File-based routing for HTML pages, islands architecture for client interactivity, head/SEO via [unhead](https://unhead.unjs.io/), and full Nitro feature set (presets, caching, middleware, API routes).
3
+ SSR meta-framework on top of [Vite](https://vite.dev), [Nitro](https://nitro.build/), and [Hono JSX](https://hono.dev/docs/guides/jsx). File-based routing for HTML pages, islands architecture for client interactivity, client-side routing via [wouter](https://github.com/molefrog/wouter), React-ecosystem compatibility through [@hono/react-compat](https://github.com/honojs/react-compat), head/SEO via [unhead](https://unhead.unjs.io/), and full Nitro feature set (presets, caching, middleware, API routes).
4
4
 
5
5
  ## Install
6
6
 
@@ -149,6 +149,37 @@ export default definePage({
149
149
 
150
150
  Props are serialized with `devalue` (supports `Date`, `Map`, `Set`, `URL`, `RegExp`, `Error`, `BigInt`, cycles).
151
151
 
152
+ ### React ecosystem compatibility
153
+
154
+ The Vite plugin aliases `react` and `react-dom` to [`@hono/react-compat`](https://github.com/honojs/react-compat), which reimplements the React API on top of `hono/jsx`. This means libraries from the React ecosystem (wouter, tanstack/react-query, etc.) work inside islands and the render tree without shipping React. `use-sync-external-store` is also aliased to `@hono/react-compat`.
155
+
156
+ ## Routing
157
+
158
+ Every page renders inside a [wouter](https://github.com/molefrog/wouter) `<Router>`, seeded with the current request's `pathname` and `search` for SSR. After hydration, navigation is client-side — wouter hooks and components work inside islands and the root layout:
159
+
160
+ ```tsx
161
+ // src/components/Search.island.tsx
162
+ import { useSearchParams } from "wouter";
163
+
164
+ export const Search = () => {
165
+ const [params, setParams] = useSearchParams();
166
+
167
+ return (
168
+ <input
169
+ value={params.get("q") ?? ""}
170
+ onChange={e =>
171
+ setParams(prev => {
172
+ prev.set("q", e.target.value);
173
+ return prev;
174
+ })
175
+ }
176
+ />
177
+ );
178
+ };
179
+ ```
180
+
181
+ wouter's `Link`, `Route`, `useLocation`, `useRoute`, and `useSearchParams` are all available. Note that yamf's file-based routing (see [File routing](#file-routing)) handles full-page SSR routes, while wouter handles client-side navigation and query-param state within a page.
182
+
152
183
  ## Client entry
153
184
 
154
185
  ```ts
@@ -1 +1 @@
1
- {"version":3,"file":"entry.d.mts","names":[],"sources":["../../src/server/entry.tsx"],"mappings":";;;KA8CY,cAAc,sBAAsB,qBAAqB;UAEpD;EAChB,OAAO,aAAa,OAAO,YAAY;EACvC;;cAGY,oBAAqB,UAAU,6BAA2B"}
1
+ {"version":3,"file":"entry.d.mts","names":[],"sources":["../../src/server/entry.tsx"],"mappings":";;;KAsDY,cAAc,sBAAsB,qBAAqB;UAEpD;EAChB,OAAO,aAAa,OAAO,YAAY;EACvC;;cAGY,oBAAqB,UAAU,6BAA2B"}
@@ -3,6 +3,7 @@ import { HTTPError, defineHandler, writeEarlyHints } from "nitro/h3";
3
3
  import { addRoute, createRouter, findRoute } from "rou3";
4
4
  import { withLeadingSlash, withoutTrailingSlash } from "ufo";
5
5
  import { clientAssets } from "virtual:yamf:assets";
6
+ import { errorHandler } from "virtual:yamf:error-handler";
6
7
  import { assets, pages } from "virtual:yamf:pages";
7
8
  import { rootAssets } from "virtual:yamf:root";
8
9
  //#region src/server/entry.tsx
@@ -19,7 +20,7 @@ for (const [relativePath, handler] of Object.entries(pages).toSorted((a, b) => a
19
20
  });
20
21
  }
21
22
  const defineServerEntry = (options) => {
22
- return defineHandler(async (event) => {
23
+ const rootHandler = async (event) => {
23
24
  const route = findRoute(router, "GET", event.url.pathname);
24
25
  if (!route) throw new HTTPError("Not found", { status: 404 });
25
26
  const { handler, serverAssets } = route.data;
@@ -33,6 +34,14 @@ const defineServerEntry = (options) => {
33
34
  assets,
34
35
  head: typeof options?.head === "function" ? options.head(event) : options?.head
35
36
  });
37
+ };
38
+ return defineHandler(async (event) => {
39
+ if (!errorHandler) return rootHandler(event);
40
+ try {
41
+ return await rootHandler(event);
42
+ } catch (error) {
43
+ return errorHandler(error instanceof HTTPError ? error : new HTTPError({ cause: error }), event);
44
+ }
36
45
  });
37
46
  };
38
47
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"entry.mjs","names":["pagesServerAssets"],"sources":["../../src/server/entry.tsx"],"sourcesContent":["import path from \"node:path\";\nimport type { EventHandlerRequest, EventHandlerWithFetch, H3Event } from \"nitro/h3\";\nimport { defineHandler, HTTPError, writeEarlyHints } from \"nitro/h3\";\nimport { addRoute, createRouter, findRoute } from \"rou3\";\nimport { withLeadingSlash, withoutTrailingSlash } from \"ufo\";\nimport { clientAssets } from \"virtual:yamf:assets\";\nimport { pages, assets as pagesServerAssets } from \"virtual:yamf:pages\";\nimport { rootAssets } from \"virtual:yamf:root\";\nimport type { PageHandler } from \"#/page\";\nimport { YamfHead } from \"#/shared/head\";\n\ninterface Route {\n\thandler: () => Promise<PageHandler>;\n\tserverAssets?: ImportAssetsResult;\n}\n\nconst router = createRouter<Route>();\n\nfor (const [relativePath, handler] of Object.entries(pages).toSorted((a, b) =>\n\ta[0].localeCompare(b[0]),\n)) {\n\tconst ext = path.extname(relativePath);\n\n\tlet route = path.relative(\"/src/pages\", relativePath);\n\n\tif (ext.length) {\n\t\troute = route.slice(0, -ext.length);\n\t}\n\n\troute =\n\t\troute\n\t\t\t.replace(/\\.[A-Za-z]+$/, \"\")\n\t\t\t.replace(/\\(([^(/\\\\]+)\\)[/\\\\]/g, \"\")\n\t\t\t.replace(/\\[\\.{3}]/g, \"**\")\n\t\t\t.replace(/\\[\\.{3}([^\\]]+)]/g, (_, p: string) => \"**:\" + p.replace(/[^\\w-]/g, \"_\"))\n\t\t\t.replace(/\\[([^/\\]]+)]/g, (_, p: string) => \":\" + p.replace(/[^\\w-]/g, \"_\"))\n\t\t\t.replace(/(\\/|^)index$/, \"\") || \"/\";\n\n\troute = withLeadingSlash(withoutTrailingSlash(route));\n\n\taddRoute(router, \"GET\", route, {\n\t\thandler,\n\t\tserverAssets: pagesServerAssets[relativePath],\n\t});\n}\n\nexport type ServerEntry = EventHandlerWithFetch<EventHandlerRequest, Promise<unknown>>;\n\nexport interface DefineServerEntryOptions {\n\thead?: YamfHead | ((event: H3Event) => YamfHead);\n\tdisableEarlyHints?: boolean;\n}\n\nexport const defineServerEntry = (options?: DefineServerEntryOptions): ServerEntry => {\n\treturn defineHandler(async event => {\n\t\tconst route = findRoute(router, \"GET\", event.url.pathname);\n\n\t\tif (!route) {\n\t\t\tthrow new HTTPError(\"Not found\", { status: 404 });\n\t\t}\n\n\t\tconst { handler, serverAssets } = route.data;\n\t\tevent.context.params = route.params;\n\n\t\tconst assets = clientAssets.merge(...[rootAssets, serverAssets].filter(x => !!x));\n\n\t\tif (!options?.disableEarlyHints) {\n\t\t\tconst link = [\n\t\t\t\t...assets.js.map(script => `<${script.href}>; rel=modulepreload`),\n\t\t\t\t...assets.css.map(style => `<${style.href}>; rel=preload; as=style`),\n\t\t\t];\n\n\t\t\tif (link.length) {\n\t\t\t\tawait writeEarlyHints(event, { link });\n\t\t\t}\n\t\t}\n\n\t\treturn (await handler())(event, {\n\t\t\tassets,\n\t\t\thead: typeof options?.head === \"function\" ? options.head(event) : options?.head,\n\t\t});\n\t});\n};\n"],"mappings":";;;;;;;;AAgBA,MAAM,SAAS,aAAoB;AAEnC,KAAK,MAAM,CAAC,cAAc,YAAY,OAAO,QAAQ,KAAK,CAAC,CAAC,UAAU,GAAG,MACxE,EAAE,EAAE,CAAC,cAAc,EAAE,EAAE,CACxB,GAAG;CACF,MAAM,MAAM,KAAK,QAAQ,YAAY;CAErC,IAAI,QAAQ,KAAK,SAAS,cAAc,YAAY;CAEpD,IAAI,IAAI,QACP,QAAQ,MAAM,MAAM,GAAG,CAAC,IAAI,MAAM;CAGnC,QACC,MACE,QAAQ,gBAAgB,EAAE,CAAC,CAC3B,QAAQ,wBAAwB,EAAE,CAAC,CACnC,QAAQ,aAAa,IAAI,CAAC,CAC1B,QAAQ,sBAAsB,GAAG,MAAc,QAAQ,EAAE,QAAQ,WAAW,GAAG,CAAC,CAAC,CACjF,QAAQ,kBAAkB,GAAG,MAAc,MAAM,EAAE,QAAQ,WAAW,GAAG,CAAC,CAAC,CAC3E,QAAQ,gBAAgB,EAAE,KAAK;CAElC,QAAQ,iBAAiB,qBAAqB,KAAK,CAAC;CAEpD,SAAS,QAAQ,OAAO,OAAO;EAC9B;EACA,cAAcA,OAAkB;CACjC,CAAC;AACF;AASA,MAAa,qBAAqB,YAAoD;CACrF,OAAO,cAAc,OAAM,UAAS;EACnC,MAAM,QAAQ,UAAU,QAAQ,OAAO,MAAM,IAAI,QAAQ;EAEzD,IAAI,CAAC,OACJ,MAAM,IAAI,UAAU,aAAa,EAAE,QAAQ,IAAI,CAAC;EAGjD,MAAM,EAAE,SAAS,iBAAiB,MAAM;EACxC,MAAM,QAAQ,SAAS,MAAM;EAE7B,MAAM,SAAS,aAAa,MAAM,GAAG,CAAC,YAAY,YAAY,CAAC,CAAC,QAAO,MAAK,CAAC,CAAC,CAAC,CAAC;EAEhF,IAAI,CAAC,SAAS,mBAAmB;GAChC,MAAM,OAAO,CACZ,GAAG,OAAO,GAAG,KAAI,WAAU,IAAI,OAAO,KAAK,qBAAqB,GAChE,GAAG,OAAO,IAAI,KAAI,UAAS,IAAI,MAAM,KAAK,yBAAyB,CACpE;GAEA,IAAI,KAAK,QACR,MAAM,gBAAgB,OAAO,EAAE,KAAK,CAAC;EAEvC;EAEA,QAAQ,MAAM,QAAQ,EAAA,CAAG,OAAO;GAC/B;GACA,MAAM,OAAO,SAAS,SAAS,aAAa,QAAQ,KAAK,KAAK,IAAI,SAAS;EAC5E,CAAC;CACF,CAAC;AACF"}
1
+ {"version":3,"file":"entry.mjs","names":["pagesServerAssets"],"sources":["../../src/server/entry.tsx"],"sourcesContent":["import path from \"node:path\";\nimport {\n\tdefineHandler,\n\tEventHandler,\n\tH3Event,\n\tHTTPError,\n\twriteEarlyHints,\n\ttype EventHandlerRequest,\n\ttype EventHandlerWithFetch,\n} from \"nitro/h3\";\nimport { addRoute, createRouter, findRoute } from \"rou3\";\nimport { withLeadingSlash, withoutTrailingSlash } from \"ufo\";\nimport { clientAssets } from \"virtual:yamf:assets\";\nimport { errorHandler } from \"virtual:yamf:error-handler\";\nimport { pages, assets as pagesServerAssets } from \"virtual:yamf:pages\";\nimport { rootAssets } from \"virtual:yamf:root\";\nimport type { PageHandler } from \"#/page\";\nimport { YamfHead } from \"#/shared/head\";\n\ninterface Route {\n\thandler: () => Promise<PageHandler>;\n\tserverAssets?: ImportAssetsResult;\n}\n\nconst router = createRouter<Route>();\n\nfor (const [relativePath, handler] of Object.entries(pages).toSorted((a, b) =>\n\ta[0].localeCompare(b[0]),\n)) {\n\tconst ext = path.extname(relativePath);\n\n\tlet route = path.relative(\"/src/pages\", relativePath);\n\n\tif (ext.length) {\n\t\troute = route.slice(0, -ext.length);\n\t}\n\n\troute =\n\t\troute\n\t\t\t.replace(/\\.[A-Za-z]+$/, \"\")\n\t\t\t.replace(/\\(([^(/\\\\]+)\\)[/\\\\]/g, \"\")\n\t\t\t.replace(/\\[\\.{3}]/g, \"**\")\n\t\t\t.replace(/\\[\\.{3}([^\\]]+)]/g, (_, p: string) => \"**:\" + p.replace(/[^\\w-]/g, \"_\"))\n\t\t\t.replace(/\\[([^/\\]]+)]/g, (_, p: string) => \":\" + p.replace(/[^\\w-]/g, \"_\"))\n\t\t\t.replace(/(\\/|^)index$/, \"\") || \"/\";\n\n\troute = withLeadingSlash(withoutTrailingSlash(route));\n\n\taddRoute(router, \"GET\", route, {\n\t\thandler,\n\t\tserverAssets: pagesServerAssets[relativePath],\n\t});\n}\n\nexport type ServerEntry = EventHandlerWithFetch<EventHandlerRequest, Promise<unknown>>;\n\nexport interface DefineServerEntryOptions {\n\thead?: YamfHead | ((event: H3Event) => YamfHead);\n\tdisableEarlyHints?: boolean;\n}\n\nexport const defineServerEntry = (options?: DefineServerEntryOptions): ServerEntry => {\n\tconst rootHandler: EventHandler<EventHandlerRequest, Promise<unknown>> = async event => {\n\t\tconst route = findRoute(router, \"GET\", event.url.pathname);\n\n\t\tif (!route) {\n\t\t\tthrow new HTTPError(\"Not found\", { status: 404 });\n\t\t}\n\n\t\tconst { handler, serverAssets } = route.data;\n\t\tevent.context.params = route.params;\n\n\t\tconst assets = clientAssets.merge(...[rootAssets, serverAssets].filter(x => !!x));\n\n\t\tif (!options?.disableEarlyHints) {\n\t\t\tconst link = [\n\t\t\t\t...assets.js.map(script => `<${script.href}>; rel=modulepreload`),\n\t\t\t\t...assets.css.map(style => `<${style.href}>; rel=preload; as=style`),\n\t\t\t];\n\n\t\t\tif (link.length) {\n\t\t\t\tawait writeEarlyHints(event, { link });\n\t\t\t}\n\t\t}\n\n\t\treturn (await handler())(event, {\n\t\t\tassets,\n\t\t\thead: typeof options?.head === \"function\" ? options.head(event) : options?.head,\n\t\t});\n\t};\n\n\treturn defineHandler(async event => {\n\t\tif (!errorHandler) {\n\t\t\treturn rootHandler(event);\n\t\t}\n\n\t\ttry {\n\t\t\treturn await rootHandler(event);\n\t\t} catch (error) {\n\t\t\tconst httpError = error instanceof HTTPError ? error : new HTTPError({ cause: error });\n\n\t\t\treturn errorHandler(httpError, event);\n\t\t}\n\t});\n};\n"],"mappings":";;;;;;;;;AAwBA,MAAM,SAAS,aAAoB;AAEnC,KAAK,MAAM,CAAC,cAAc,YAAY,OAAO,QAAQ,KAAK,CAAC,CAAC,UAAU,GAAG,MACxE,EAAE,EAAE,CAAC,cAAc,EAAE,EAAE,CACxB,GAAG;CACF,MAAM,MAAM,KAAK,QAAQ,YAAY;CAErC,IAAI,QAAQ,KAAK,SAAS,cAAc,YAAY;CAEpD,IAAI,IAAI,QACP,QAAQ,MAAM,MAAM,GAAG,CAAC,IAAI,MAAM;CAGnC,QACC,MACE,QAAQ,gBAAgB,EAAE,CAAC,CAC3B,QAAQ,wBAAwB,EAAE,CAAC,CACnC,QAAQ,aAAa,IAAI,CAAC,CAC1B,QAAQ,sBAAsB,GAAG,MAAc,QAAQ,EAAE,QAAQ,WAAW,GAAG,CAAC,CAAC,CACjF,QAAQ,kBAAkB,GAAG,MAAc,MAAM,EAAE,QAAQ,WAAW,GAAG,CAAC,CAAC,CAC3E,QAAQ,gBAAgB,EAAE,KAAK;CAElC,QAAQ,iBAAiB,qBAAqB,KAAK,CAAC;CAEpD,SAAS,QAAQ,OAAO,OAAO;EAC9B;EACA,cAAcA,OAAkB;CACjC,CAAC;AACF;AASA,MAAa,qBAAqB,YAAoD;CACrF,MAAM,cAAmE,OAAM,UAAS;EACvF,MAAM,QAAQ,UAAU,QAAQ,OAAO,MAAM,IAAI,QAAQ;EAEzD,IAAI,CAAC,OACJ,MAAM,IAAI,UAAU,aAAa,EAAE,QAAQ,IAAI,CAAC;EAGjD,MAAM,EAAE,SAAS,iBAAiB,MAAM;EACxC,MAAM,QAAQ,SAAS,MAAM;EAE7B,MAAM,SAAS,aAAa,MAAM,GAAG,CAAC,YAAY,YAAY,CAAC,CAAC,QAAO,MAAK,CAAC,CAAC,CAAC,CAAC;EAEhF,IAAI,CAAC,SAAS,mBAAmB;GAChC,MAAM,OAAO,CACZ,GAAG,OAAO,GAAG,KAAI,WAAU,IAAI,OAAO,KAAK,qBAAqB,GAChE,GAAG,OAAO,IAAI,KAAI,UAAS,IAAI,MAAM,KAAK,yBAAyB,CACpE;GAEA,IAAI,KAAK,QACR,MAAM,gBAAgB,OAAO,EAAE,KAAK,CAAC;EAEvC;EAEA,QAAQ,MAAM,QAAQ,EAAA,CAAG,OAAO;GAC/B;GACA,MAAM,OAAO,SAAS,SAAS,aAAa,QAAQ,KAAK,KAAK,IAAI,SAAS;EAC5E,CAAC;CACF;CAEA,OAAO,cAAc,OAAM,UAAS;EACnC,IAAI,CAAC,cACJ,OAAO,YAAY,KAAK;EAGzB,IAAI;GACH,OAAO,MAAM,YAAY,KAAK;EAC/B,SAAS,OAAO;GAGf,OAAO,aAFW,iBAAiB,YAAY,QAAQ,IAAI,UAAU,EAAE,OAAO,MAAM,CAAC,GAEtD,KAAK;EACrC;CACD,CAAC;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/vite/index.ts"],"mappings":";;;UAUiB;EAChB,QAAQ;;cAGH,OAAQ,UAAU,gBAAc"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/vite/index.ts"],"mappings":";;;UAWiB;EAChB,QAAQ;;cAGH,OAAQ,UAAU,gBAAc"}
@@ -1,5 +1,6 @@
1
1
  import { islands } from "./islands.mjs";
2
2
  import { virtualAssets } from "./virtual-assets.mjs";
3
+ import { virtualErrorHandler } from "./virtual-error-handler.mjs";
3
4
  import { virtualPages } from "./virtual-pages.mjs";
4
5
  import { virtualRoot } from "./virtual-root.mjs";
5
6
  import { virtualTemplate } from "./virtual-template.mjs";
@@ -45,6 +46,7 @@ const yamf = (options) => {
45
46
  }
46
47
  });
47
48
  plugins.push(islands());
49
+ plugins.push(virtualErrorHandler(options));
48
50
  plugins.push(virtualAssets());
49
51
  plugins.push(virtualPages());
50
52
  plugins.push(virtualTemplate());
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../src/vite/index.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport type { NitroPluginConfig } from \"nitro/vite\";\nimport { nitro } from \"nitro/vite\";\nimport type { EnvironmentOptions, PluginOption } from \"vite\";\nimport { islands } from \"./islands\";\nimport { virtualAssets } from \"./virtual-assets\";\nimport { virtualPages } from \"./virtual-pages\";\nimport { virtualRoot } from \"./virtual-root\";\nimport { virtualTemplate } from \"./virtual-template\";\n\nexport interface YamfOptions {\n\tnitro?: NitroPluginConfig;\n}\n\nconst yamf = (options?: YamfOptions): PluginOption[] => {\n\tconst plugins: PluginOption[] = [];\n\n\t// TODO: extendable config\n\tplugins.push({\n\t\tname: \"yamf:config\",\n\t\tconfig() {\n\t\t\tconst ssrEnv: EnvironmentOptions = {\n\t\t\t\tbuild: {\n\t\t\t\t\t// TODO: figure out if this should be true\n\t\t\t\t\tcssCodeSplit: false,\n\t\t\t\t\trolldownOptions: {\n\t\t\t\t\t\tinput: \"./src/server.tsx\",\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\n\t\t\treturn {\n\t\t\t\tssr: {\n\t\t\t\t\t// we need to inline because otherwise fullstack plugin fails to build manifest for assets imports\n\t\t\t\t\t// also, we need to bundle everything so that the `react` -> `@hono/react-compat` alias\n\t\t\t\t\t// is applied to all dependencies (e.g. wouter) in dev mode. Externalized deps are\n\t\t\t\t\t// loaded natively by Node, bypassing Vite's resolve.alias.\n\t\t\t\t\tnoExternal: true,\n\t\t\t\t},\n\t\t\t\toptimizeDeps: {\n\t\t\t\t\tinclude: [\n\t\t\t\t\t\t\"hono\",\n\t\t\t\t\t\t\"hono/jsx/dom/client\",\n\t\t\t\t\t\t\"hono/jsx/jsx-runtime\",\n\t\t\t\t\t\t\"devalue\",\n\t\t\t\t\t\t\"ufo\",\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\tresolve: {\n\t\t\t\t\talias: [\n\t\t\t\t\t\t{ find: \"react\", replacement: \"@hono/react-compat\" },\n\t\t\t\t\t\t{ find: \"react-dom\", replacement: \"@hono/react-compat\" },\n\t\t\t\t\t\t// use-sync-external-store is a CJS package that requires(\"react\").\n\t\t\t\t\t\t// Vite's SSR module runner can't process CJS, so we alias it to\n\t\t\t\t\t\t// @hono/react-compat which exports useSyncExternalStore from hono/jsx.\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfind: /^use-sync-external-store(?:\\/shim(?:\\/.*)?)?$/,\n\t\t\t\t\t\t\treplacement: \"@hono/react-compat\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\tenvironments: {\n\t\t\t\t\t...(existsSync(\"./src/server.tsx\") ? { ssr: ssrEnv } : {}),\n\t\t\t\t\tclient: {\n\t\t\t\t\t\tbuild: {\n\t\t\t\t\t\t\trolldownOptions: {\n\t\t\t\t\t\t\t\tinput: \"./src/client/index.ts\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tplugins.push(islands());\n\n\tplugins.push(virtualAssets());\n\tplugins.push(virtualPages());\n\tplugins.push(virtualTemplate());\n\tplugins.push(virtualRoot());\n\n\tplugins.push(\n\t\tnitro({\n\t\t\tserverDir: \"./src\",\n\t\t\trenderer: false,\n\t\t\t...options?.nitro,\n\t\t\tcompressPublicAssets: {\n\t\t\t\tgzip: true,\n\t\t\t\tbrotli: true,\n\t\t\t},\n\t\t\tpublicAssets: [\n\t\t\t\t{\n\t\t\t\t\tbaseURL: \"assets\",\n\t\t\t\t\tdir: \"./public/assets\",\n\t\t\t\t\tmaxAge: 365 * 24 * 60 * 60,\n\t\t\t\t},\n\t\t\t],\n\t\t}),\n\t);\n\n\treturn plugins;\n};\n\nexport default yamf;\n"],"mappings":";;;;;;;;AAcA,MAAM,QAAQ,YAA0C;CACvD,MAAM,UAA0B,CAAC;CAGjC,QAAQ,KAAK;EACZ,MAAM;EACN,SAAS;GAWR,OAAO;IACN,KAAK,EAKJ,YAAY,KACb;IACA,cAAc,EACb,SAAS;KACR;KACA;KACA;KACA;KACA;IACD,EACD;IACA,SAAS,EACR,OAAO;KACN;MAAE,MAAM;MAAS,aAAa;KAAqB;KACnD;MAAE,MAAM;MAAa,aAAa;KAAqB;KAIvD;MACC,MAAM;MACN,aAAa;KACd;IACD,EACD;IACA,cAAc;KACb,GAAI,WAAW,kBAAkB,IAAI,EAAE,KAAK,EAxC7C,OAAO;MAEN,cAAc;MACd,iBAAiB,EAChB,OAAO,mBACR;KACD,EAkCkD,EAAE,IAAI,CAAC;KACxD,QAAQ,EACP,OAAO,EACN,iBAAiB,EAChB,OAAO,wBACR,EACD,EACD;IACD;GACD;EACD;CACD,CAAC;CAED,QAAQ,KAAK,QAAQ,CAAC;CAEtB,QAAQ,KAAK,cAAc,CAAC;CAC5B,QAAQ,KAAK,aAAa,CAAC;CAC3B,QAAQ,KAAK,gBAAgB,CAAC;CAC9B,QAAQ,KAAK,YAAY,CAAC;CAE1B,QAAQ,KACP,MAAM;EACL,WAAW;EACX,UAAU;EACV,GAAG,SAAS;EACZ,sBAAsB;GACrB,MAAM;GACN,QAAQ;EACT;EACA,cAAc,CACb;GACC,SAAS;GACT,KAAK;GACL,QAAQ,MAAM,KAAK,KAAK;EACzB,CACD;CACD,CAAC,CACF;CAEA,OAAO;AACR"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/vite/index.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport type { NitroPluginConfig } from \"nitro/vite\";\nimport { nitro } from \"nitro/vite\";\nimport type { EnvironmentOptions, PluginOption } from \"vite\";\nimport { islands } from \"./islands\";\nimport { virtualAssets } from \"./virtual-assets\";\nimport { virtualErrorHandler } from \"./virtual-error-handler\";\nimport { virtualPages } from \"./virtual-pages\";\nimport { virtualRoot } from \"./virtual-root\";\nimport { virtualTemplate } from \"./virtual-template\";\n\nexport interface YamfOptions {\n\tnitro?: NitroPluginConfig;\n}\n\nconst yamf = (options?: YamfOptions): PluginOption[] => {\n\tconst plugins: PluginOption[] = [];\n\n\t// TODO: extendable config\n\tplugins.push({\n\t\tname: \"yamf:config\",\n\t\tconfig() {\n\t\t\tconst ssrEnv: EnvironmentOptions = {\n\t\t\t\tbuild: {\n\t\t\t\t\t// TODO: figure out if this should be true\n\t\t\t\t\tcssCodeSplit: false,\n\t\t\t\t\trolldownOptions: {\n\t\t\t\t\t\tinput: \"./src/server.tsx\",\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\n\t\t\treturn {\n\t\t\t\tssr: {\n\t\t\t\t\t// we need to inline because otherwise fullstack plugin fails to build manifest for assets imports\n\t\t\t\t\t// also, we need to bundle everything so that the `react` -> `@hono/react-compat` alias\n\t\t\t\t\t// is applied to all dependencies (e.g. wouter) in dev mode. Externalized deps are\n\t\t\t\t\t// loaded natively by Node, bypassing Vite's resolve.alias.\n\t\t\t\t\tnoExternal: true,\n\t\t\t\t},\n\t\t\t\toptimizeDeps: {\n\t\t\t\t\tinclude: [\n\t\t\t\t\t\t\"hono\",\n\t\t\t\t\t\t\"hono/jsx/dom/client\",\n\t\t\t\t\t\t\"hono/jsx/jsx-runtime\",\n\t\t\t\t\t\t\"devalue\",\n\t\t\t\t\t\t\"ufo\",\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\tresolve: {\n\t\t\t\t\talias: [\n\t\t\t\t\t\t{ find: \"react\", replacement: \"@hono/react-compat\" },\n\t\t\t\t\t\t{ find: \"react-dom\", replacement: \"@hono/react-compat\" },\n\t\t\t\t\t\t// use-sync-external-store is a CJS package that requires(\"react\").\n\t\t\t\t\t\t// Vite's SSR module runner can't process CJS, so we alias it to\n\t\t\t\t\t\t// @hono/react-compat which exports useSyncExternalStore from hono/jsx.\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfind: /^use-sync-external-store(?:\\/shim(?:\\/.*)?)?$/,\n\t\t\t\t\t\t\treplacement: \"@hono/react-compat\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\tenvironments: {\n\t\t\t\t\t...(existsSync(\"./src/server.tsx\") ? { ssr: ssrEnv } : {}),\n\t\t\t\t\tclient: {\n\t\t\t\t\t\tbuild: {\n\t\t\t\t\t\t\trolldownOptions: {\n\t\t\t\t\t\t\t\tinput: \"./src/client/index.ts\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tplugins.push(islands());\n\n\tplugins.push(virtualErrorHandler(options));\n\tplugins.push(virtualAssets());\n\tplugins.push(virtualPages());\n\tplugins.push(virtualTemplate());\n\tplugins.push(virtualRoot());\n\n\tplugins.push(\n\t\tnitro({\n\t\t\tserverDir: \"./src\",\n\t\t\trenderer: false,\n\t\t\t...options?.nitro,\n\t\t\tcompressPublicAssets: {\n\t\t\t\tgzip: true,\n\t\t\t\tbrotli: true,\n\t\t\t},\n\t\t\tpublicAssets: [\n\t\t\t\t{\n\t\t\t\t\tbaseURL: \"assets\",\n\t\t\t\t\tdir: \"./public/assets\",\n\t\t\t\t\tmaxAge: 365 * 24 * 60 * 60,\n\t\t\t\t},\n\t\t\t],\n\t\t}),\n\t);\n\n\treturn plugins;\n};\n\nexport default yamf;\n"],"mappings":";;;;;;;;;AAeA,MAAM,QAAQ,YAA0C;CACvD,MAAM,UAA0B,CAAC;CAGjC,QAAQ,KAAK;EACZ,MAAM;EACN,SAAS;GAWR,OAAO;IACN,KAAK,EAKJ,YAAY,KACb;IACA,cAAc,EACb,SAAS;KACR;KACA;KACA;KACA;KACA;IACD,EACD;IACA,SAAS,EACR,OAAO;KACN;MAAE,MAAM;MAAS,aAAa;KAAqB;KACnD;MAAE,MAAM;MAAa,aAAa;KAAqB;KAIvD;MACC,MAAM;MACN,aAAa;KACd;IACD,EACD;IACA,cAAc;KACb,GAAI,WAAW,kBAAkB,IAAI,EAAE,KAAK,EAxC7C,OAAO;MAEN,cAAc;MACd,iBAAiB,EAChB,OAAO,mBACR;KACD,EAkCkD,EAAE,IAAI,CAAC;KACxD,QAAQ,EACP,OAAO,EACN,iBAAiB,EAChB,OAAO,wBACR,EACD,EACD;IACD;GACD;EACD;CACD,CAAC;CAED,QAAQ,KAAK,QAAQ,CAAC;CAEtB,QAAQ,KAAK,oBAAoB,OAAO,CAAC;CACzC,QAAQ,KAAK,cAAc,CAAC;CAC5B,QAAQ,KAAK,aAAa,CAAC;CAC3B,QAAQ,KAAK,gBAAgB,CAAC;CAC9B,QAAQ,KAAK,YAAY,CAAC;CAE1B,QAAQ,KACP,MAAM;EACL,WAAW;EACX,UAAU;EACV,GAAG,SAAS;EACZ,sBAAsB;GACrB,MAAM;GACN,QAAQ;EACT;EACA,cAAc,CACb;GACC,SAAS;GACT,KAAK;GACL,QAAQ,MAAM,KAAK,KAAK;EACzB,CACD;CACD,CAAC,CACF;CAEA,OAAO;AACR"}
@@ -1,81 +1,60 @@
1
- import { generate } from "@babel/generator";
2
- import { parse } from "@babel/parser";
3
- import _traverse from "@babel/traverse";
4
- import { callExpression, exportDefaultDeclaration, exportNamedDeclaration, functionDeclaration, functionExpression, identifier, importDeclaration, importDefaultSpecifier, importNamespaceSpecifier, memberExpression, stringLiteral, variableDeclaration, variableDeclarator } from "@babel/types";
1
+ import { withMagicString } from "rolldown-string";
2
+ import { Visitor } from "vite";
5
3
  //#region src/vite/islands.ts
6
- const traverse = _traverse.default ?? _traverse;
7
4
  const ISLAND_REGEX = /\.island\.(j|t)sx?$/;
5
+ function langFromId(id) {
6
+ if (id.endsWith(".tsx")) return "tsx";
7
+ if (id.endsWith(".ts")) return "ts";
8
+ if (id.endsWith(".jsx")) return "jsx";
9
+ return "js";
10
+ }
8
11
  const islands = () => {
9
12
  return [{
10
13
  name: "yamf:islands",
11
14
  transform: {
12
15
  filter: { id: ISLAND_REGEX },
13
- handler(code, id) {
16
+ handler: withMagicString(function(s, id) {
14
17
  if (this.environment.name !== "ssr") return;
18
+ const program = this.parse(s.original, { lang: langFromId(id) });
19
+ s.prepend(`import * as __runtime from "@pajecawav/yamf/server";\n`);
20
+ s.prepend(`import __assets from "${id}?assets=client";\n`);
15
21
  const seenExports = /* @__PURE__ */ new Set();
16
- const ast = parse(code, {
17
- sourceType: "module",
18
- plugins: ["typescript", "jsx"]
19
- });
20
- traverse(ast, {
21
- Program(path) {
22
- path.unshiftContainer("body", importDeclaration([importNamespaceSpecifier(identifier("__runtime"))], stringLiteral("@pajecawav/yamf/server")));
23
- path.unshiftContainer("body", importDeclaration([importDefaultSpecifier(identifier("__assets"))], stringLiteral(`${id}?assets=client`)));
22
+ new Visitor({
23
+ ExportDefaultDeclaration(node) {
24
+ const declaration = node.declaration;
25
+ if (declaration.type !== "FunctionDeclaration" && declaration.type !== "FunctionExpression" && declaration.type !== "ArrowFunctionExpression") return;
26
+ s.overwrite(node.start, declaration.start, "const __ISLAND__ = ");
27
+ s.appendRight(declaration.end, `; export default __runtime.createIsland(__ISLAND__, "default", __assets)`);
24
28
  },
25
- ExportDefaultDeclaration(path) {
26
- const declarationType = path.node.declaration.type;
27
- if (!(declarationType === "FunctionDeclaration" || declarationType === "FunctionExpression" || declarationType === "ArrowFunctionExpression")) return;
28
- const originalFunction = path.node.declaration.type === "FunctionExpression" || path.node.declaration.type === "ArrowFunctionExpression" ? path.node.declaration : functionExpression(null, path.node.declaration.params, path.node.declaration.body, void 0, path.node.declaration.async);
29
- const islandIdentifier = identifier("__ISLAND__");
30
- path.insertBefore(variableDeclaration("const", [variableDeclarator(islandIdentifier, originalFunction)]));
31
- path.replaceWith(exportDefaultDeclaration(callExpression(memberExpression(identifier("__runtime"), identifier("createIsland")), [
32
- islandIdentifier,
33
- stringLiteral("default"),
34
- identifier("__assets")
35
- ])));
36
- },
37
- ExportNamedDeclaration(path) {
38
- if (path.node.declaration?.type === "VariableDeclaration") {
39
- const declaration = path.node.declaration.declarations.at(0);
40
- if (!declaration || declaration.id.type !== "Identifier" || seenExports.has(declaration.id.name)) return;
41
- const exportName = declaration.id.name;
29
+ ExportNamedDeclaration(node) {
30
+ const declaration = node.declaration;
31
+ if (declaration?.type === "VariableDeclaration") {
32
+ const declarator = declaration.declarations[0];
33
+ if (!declarator || declarator.id.type !== "Identifier" || !declarator.init || seenExports.has(declarator.id.name)) return;
34
+ const exportName = declarator.id.name;
42
35
  seenExports.add(exportName);
43
- const islandIdentifier = identifier(`__wrap_${exportName}`);
44
- path.insertBefore(variableDeclaration("const", [variableDeclarator(islandIdentifier, declaration.init)]));
45
- path.replaceWith(exportNamedDeclaration(variableDeclaration("const", [variableDeclarator(identifier(exportName), callExpression(memberExpression(identifier("__runtime"), identifier("createIsland")), [
46
- islandIdentifier,
47
- stringLiteral(exportName),
48
- identifier("__assets")
49
- ]))])));
50
- } else if (path.node.declaration?.type === "FunctionDeclaration") {
51
- const declaration = path.node.declaration;
52
- if (!declaration || declaration.id?.type !== "Identifier" || seenExports.has(declaration.id.name)) return;
53
- const exportName = declaration.id.name;
36
+ s.overwrite(node.start, declarator.init.start, `const __wrap_${exportName} = `);
37
+ s.appendRight(declarator.init.end, `; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, "${exportName}", __assets)`);
38
+ } else if (declaration?.type === "FunctionDeclaration") {
39
+ const fnId = declaration.id;
40
+ if (!fnId || fnId.type !== "Identifier" || seenExports.has(fnId.name)) return;
41
+ const exportName = fnId.name;
54
42
  seenExports.add(exportName);
55
- const islandIdentifier = identifier(`__wrap_${exportName}`);
56
- path.insertBefore(functionDeclaration(islandIdentifier, declaration.params, declaration.body, declaration.generator, declaration.async));
57
- path.replaceWith(exportNamedDeclaration(variableDeclaration("const", [variableDeclarator(identifier(exportName), callExpression(memberExpression(identifier("__runtime"), identifier("createIsland")), [
58
- islandIdentifier,
59
- stringLiteral(exportName),
60
- identifier("__assets")
61
- ]))])));
43
+ s.remove(node.start, declaration.start);
44
+ s.overwrite(fnId.start, fnId.end, `__wrap_${exportName}`);
45
+ s.appendRight(declaration.end, `; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, "${exportName}", __assets)`);
62
46
  }
63
47
  }
64
- });
65
- const result = generate(ast);
66
- return {
67
- code: result.code,
68
- map: result.map
69
- };
70
- }
48
+ }).visit(program);
49
+ })
71
50
  }
72
51
  }, {
73
52
  name: "yamf:islands:raw-import",
74
53
  transform: {
75
54
  order: "post",
76
- handler(code) {
77
- if (code.includes("__island_raw_import__")) return code.replaceAll("__island_raw_import__", "import");
78
- }
55
+ handler: withMagicString(function(s) {
56
+ if (s.original.includes("__island_raw_import__")) s.replaceAll("__island_raw_import__", "import");
57
+ })
79
58
  }
80
59
  }];
81
60
  };
@@ -1 +1 @@
1
- {"version":3,"file":"islands.mjs","names":[],"sources":["../../src/vite/islands.ts"],"sourcesContent":["// reference https://github.com/hi-ogawa/vite-plugin-fullstack/blob/28e9540a68529c58842e9a3bf17d2193a065d524/examples/island/src/framework/island/plugin.ts\nimport { generate } from \"@babel/generator\";\nimport { parse } from \"@babel/parser\";\nimport _traverse from \"@babel/traverse\";\nimport {\n\tcallExpression,\n\texportDefaultDeclaration,\n\texportNamedDeclaration,\n\tfunctionDeclaration,\n\tfunctionExpression,\n\tidentifier,\n\timportDeclaration,\n\timportDefaultSpecifier,\n\timportNamespaceSpecifier,\n\tmemberExpression,\n\tstringLiteral,\n\tvariableDeclaration,\n\tvariableDeclarator,\n} from \"@babel/types\";\nimport type { Plugin } from \"vite\";\n\n// @ts-ignore\n// oxlint-disable-next-line typescript/no-unsafe-type-assertion\nconst traverse = (_traverse.default as typeof _traverse) ?? _traverse;\n\nconst ISLAND_REGEX = /\\.island\\.(j|t)sx?$/;\n\nexport const islands = (): Plugin[] => {\n\treturn [\n\t\t{\n\t\t\tname: \"yamf:islands\",\n\t\t\ttransform: {\n\t\t\t\tfilter: { id: ISLAND_REGEX },\n\t\t\t\thandler(code, id) {\n\t\t\t\t\tif (this.environment.name !== \"ssr\") {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst seenExports = new Set<string>();\n\n\t\t\t\t\tconst ast = parse(code, {\n\t\t\t\t\t\tsourceType: \"module\",\n\t\t\t\t\t\tplugins: [\"typescript\", \"jsx\"],\n\t\t\t\t\t});\n\n\t\t\t\t\ttraverse(ast, {\n\t\t\t\t\t\tProgram(path) {\n\t\t\t\t\t\t\t// prepends server island runtime\n\t\t\t\t\t\t\t// import * as __runtime from \"yamf/server\";\n\t\t\t\t\t\t\tpath.unshiftContainer(\n\t\t\t\t\t\t\t\t\"body\",\n\t\t\t\t\t\t\t\timportDeclaration(\n\t\t\t\t\t\t\t\t\t[importNamespaceSpecifier(identifier(\"__runtime\"))],\n\t\t\t\t\t\t\t\t\tstringLiteral(\"@pajecawav/yamf/server\"),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t// prepends asset imports for the island:\n\t\t\t\t\t\t\t// import __assets from \"MODULE_ID?assets=client\";\n\t\t\t\t\t\t\tpath.unshiftContainer(\n\t\t\t\t\t\t\t\t\"body\",\n\t\t\t\t\t\t\t\timportDeclaration(\n\t\t\t\t\t\t\t\t\t[importDefaultSpecifier(identifier(\"__assets\"))],\n\t\t\t\t\t\t\t\t\tstringLiteral(`${id}?assets=client`),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tExportDefaultDeclaration(path) {\n\t\t\t\t\t\t\tconst declarationType = path.node.declaration.type;\n\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!(\n\t\t\t\t\t\t\t\t\tdeclarationType === \"FunctionDeclaration\" ||\n\t\t\t\t\t\t\t\t\tdeclarationType === \"FunctionExpression\" ||\n\t\t\t\t\t\t\t\t\tdeclarationType === \"ArrowFunctionExpression\"\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst originalFunction =\n\t\t\t\t\t\t\t\tpath.node.declaration.type === \"FunctionExpression\" ||\n\t\t\t\t\t\t\t\tpath.node.declaration.type === \"ArrowFunctionExpression\"\n\t\t\t\t\t\t\t\t\t? path.node.declaration\n\t\t\t\t\t\t\t\t\t: functionExpression(\n\t\t\t\t\t\t\t\t\t\t\tnull,\n\t\t\t\t\t\t\t\t\t\t\tpath.node.declaration.params,\n\t\t\t\t\t\t\t\t\t\t\tpath.node.declaration.body,\n\t\t\t\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t\t\t\t\tpath.node.declaration.async,\n\t\t\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tconst islandIdentifier = identifier(\"__ISLAND__\");\n\n\t\t\t\t\t\t\tpath.insertBefore(\n\t\t\t\t\t\t\t\tvariableDeclaration(\"const\", [\n\t\t\t\t\t\t\t\t\tvariableDeclarator(islandIdentifier, originalFunction),\n\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tpath.replaceWith(\n\t\t\t\t\t\t\t\texportDefaultDeclaration(\n\t\t\t\t\t\t\t\t\tcallExpression(\n\t\t\t\t\t\t\t\t\t\tmemberExpression(\n\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__runtime\"),\n\t\t\t\t\t\t\t\t\t\t\tidentifier(\"createIsland\"),\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t\t\tislandIdentifier,\n\t\t\t\t\t\t\t\t\t\t\tstringLiteral(\"default\"),\n\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__assets\"),\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tExportNamedDeclaration(path) {\n\t\t\t\t\t\t\tif (path.node.declaration?.type === \"VariableDeclaration\") {\n\t\t\t\t\t\t\t\tconst declaration = path.node.declaration.declarations.at(0);\n\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t!declaration ||\n\t\t\t\t\t\t\t\t\tdeclaration.id.type !== \"Identifier\" ||\n\t\t\t\t\t\t\t\t\tseenExports.has(declaration.id.name)\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst exportName = declaration.id.name;\n\t\t\t\t\t\t\t\tseenExports.add(exportName);\n\n\t\t\t\t\t\t\t\tconst islandIdentifier = identifier(`__wrap_${exportName}`);\n\n\t\t\t\t\t\t\t\tpath.insertBefore(\n\t\t\t\t\t\t\t\t\tvariableDeclaration(\"const\", [\n\t\t\t\t\t\t\t\t\t\tvariableDeclarator(islandIdentifier, declaration.init),\n\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tpath.replaceWith(\n\t\t\t\t\t\t\t\t\texportNamedDeclaration(\n\t\t\t\t\t\t\t\t\t\tvariableDeclaration(\"const\", [\n\t\t\t\t\t\t\t\t\t\t\tvariableDeclarator(\n\t\t\t\t\t\t\t\t\t\t\t\tidentifier(exportName),\n\t\t\t\t\t\t\t\t\t\t\t\tcallExpression(\n\t\t\t\t\t\t\t\t\t\t\t\t\tmemberExpression(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__runtime\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"createIsland\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tislandIdentifier,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tstringLiteral(exportName),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__assets\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} else if (path.node.declaration?.type === \"FunctionDeclaration\") {\n\t\t\t\t\t\t\t\tconst declaration = path.node.declaration;\n\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t!declaration ||\n\t\t\t\t\t\t\t\t\tdeclaration.id?.type !== \"Identifier\" ||\n\t\t\t\t\t\t\t\t\tseenExports.has(declaration.id.name)\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst exportName = declaration.id.name;\n\t\t\t\t\t\t\t\tseenExports.add(exportName);\n\n\t\t\t\t\t\t\t\tconst islandIdentifier = identifier(`__wrap_${exportName}`);\n\n\t\t\t\t\t\t\t\tpath.insertBefore(\n\t\t\t\t\t\t\t\t\tfunctionDeclaration(\n\t\t\t\t\t\t\t\t\t\tislandIdentifier,\n\t\t\t\t\t\t\t\t\t\tdeclaration.params,\n\t\t\t\t\t\t\t\t\t\tdeclaration.body,\n\t\t\t\t\t\t\t\t\t\tdeclaration.generator,\n\t\t\t\t\t\t\t\t\t\tdeclaration.async,\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tpath.replaceWith(\n\t\t\t\t\t\t\t\t\texportNamedDeclaration(\n\t\t\t\t\t\t\t\t\t\tvariableDeclaration(\"const\", [\n\t\t\t\t\t\t\t\t\t\t\tvariableDeclarator(\n\t\t\t\t\t\t\t\t\t\t\t\tidentifier(exportName),\n\t\t\t\t\t\t\t\t\t\t\t\tcallExpression(\n\t\t\t\t\t\t\t\t\t\t\t\t\tmemberExpression(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__runtime\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"createIsland\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tislandIdentifier,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tstringLiteral(exportName),\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tidentifier(\"__assets\"),\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\n\t\t\t\t\tconst result = generate(ast);\n\n\t\t\t\t\treturn { code: result.code, map: result.map };\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"yamf:islands:raw-import\",\n\t\t\ttransform: {\n\t\t\t\torder: \"post\",\n\t\t\t\thandler(code) {\n\t\t\t\t\tif (code.includes(\"__island_raw_import__\")) {\n\t\t\t\t\t\treturn code.replaceAll(\"__island_raw_import__\", \"import\");\n\t\t\t\t\t}\n\n\t\t\t\t\treturn undefined;\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t];\n};\n"],"mappings":";;;;;AAuBA,MAAM,WAAY,UAAU,WAAgC;AAE5D,MAAM,eAAe;AAErB,MAAa,gBAA0B;CACtC,OAAO,CACN;EACC,MAAM;EACN,WAAW;GACV,QAAQ,EAAE,IAAI,aAAa;GAC3B,QAAQ,MAAM,IAAI;IACjB,IAAI,KAAK,YAAY,SAAS,OAC7B;IAGD,MAAM,8BAAc,IAAI,IAAY;IAEpC,MAAM,MAAM,MAAM,MAAM;KACvB,YAAY;KACZ,SAAS,CAAC,cAAc,KAAK;IAC9B,CAAC;IAED,SAAS,KAAK;KACb,QAAQ,MAAM;MAGb,KAAK,iBACJ,QACA,kBACC,CAAC,yBAAyB,WAAW,WAAW,CAAC,CAAC,GAClD,cAAc,wBAAwB,CACvC,CACD;MAIA,KAAK,iBACJ,QACA,kBACC,CAAC,uBAAuB,WAAW,UAAU,CAAC,CAAC,GAC/C,cAAc,GAAG,GAAG,eAAe,CACpC,CACD;KACD;KACA,yBAAyB,MAAM;MAC9B,MAAM,kBAAkB,KAAK,KAAK,YAAY;MAE9C,IACC,EACC,oBAAoB,yBACpB,oBAAoB,wBACpB,oBAAoB,4BAGrB;MAGD,MAAM,mBACL,KAAK,KAAK,YAAY,SAAS,wBAC/B,KAAK,KAAK,YAAY,SAAS,4BAC5B,KAAK,KAAK,cACV,mBACA,MACA,KAAK,KAAK,YAAY,QACtB,KAAK,KAAK,YAAY,MACtB,KAAA,GACA,KAAK,KAAK,YAAY,KACvB;MAEH,MAAM,mBAAmB,WAAW,YAAY;MAEhD,KAAK,aACJ,oBAAoB,SAAS,CAC5B,mBAAmB,kBAAkB,gBAAgB,CACtD,CAAC,CACF;MAEA,KAAK,YACJ,yBACC,eACC,iBACC,WAAW,WAAW,GACtB,WAAW,cAAc,CAC1B,GACA;OACC;OACA,cAAc,SAAS;OACvB,WAAW,UAAU;MACtB,CACD,CACD,CACD;KACD;KACA,uBAAuB,MAAM;MAC5B,IAAI,KAAK,KAAK,aAAa,SAAS,uBAAuB;OAC1D,MAAM,cAAc,KAAK,KAAK,YAAY,aAAa,GAAG,CAAC;OAE3D,IACC,CAAC,eACD,YAAY,GAAG,SAAS,gBACxB,YAAY,IAAI,YAAY,GAAG,IAAI,GAEnC;OAED,MAAM,aAAa,YAAY,GAAG;OAClC,YAAY,IAAI,UAAU;OAE1B,MAAM,mBAAmB,WAAW,UAAU,YAAY;OAE1D,KAAK,aACJ,oBAAoB,SAAS,CAC5B,mBAAmB,kBAAkB,YAAY,IAAI,CACtD,CAAC,CACF;OAEA,KAAK,YACJ,uBACC,oBAAoB,SAAS,CAC5B,mBACC,WAAW,UAAU,GACrB,eACC,iBACC,WAAW,WAAW,GACtB,WAAW,cAAc,CAC1B,GACA;QACC;QACA,cAAc,UAAU;QACxB,WAAW,UAAU;OACtB,CACD,CACD,CACD,CAAC,CACF,CACD;MACD,OAAO,IAAI,KAAK,KAAK,aAAa,SAAS,uBAAuB;OACjE,MAAM,cAAc,KAAK,KAAK;OAE9B,IACC,CAAC,eACD,YAAY,IAAI,SAAS,gBACzB,YAAY,IAAI,YAAY,GAAG,IAAI,GAEnC;OAED,MAAM,aAAa,YAAY,GAAG;OAClC,YAAY,IAAI,UAAU;OAE1B,MAAM,mBAAmB,WAAW,UAAU,YAAY;OAE1D,KAAK,aACJ,oBACC,kBACA,YAAY,QACZ,YAAY,MACZ,YAAY,WACZ,YAAY,KACb,CACD;OAEA,KAAK,YACJ,uBACC,oBAAoB,SAAS,CAC5B,mBACC,WAAW,UAAU,GACrB,eACC,iBACC,WAAW,WAAW,GACtB,WAAW,cAAc,CAC1B,GACA;QACC;QACA,cAAc,UAAU;QACxB,WAAW,UAAU;OACtB,CACD,CACD,CACD,CAAC,CACF,CACD;MACD;KACD;IACD,CAAC;IAED,MAAM,SAAS,SAAS,GAAG;IAE3B,OAAO;KAAE,MAAM,OAAO;KAAM,KAAK,OAAO;IAAI;GAC7C;EACD;CACD,GACA;EACC,MAAM;EACN,WAAW;GACV,OAAO;GACP,QAAQ,MAAM;IACb,IAAI,KAAK,SAAS,uBAAuB,GACxC,OAAO,KAAK,WAAW,yBAAyB,QAAQ;GAI1D;EACD;CACD,CACD;AACD"}
1
+ {"version":3,"file":"islands.mjs","names":[],"sources":["../../src/vite/islands.ts"],"sourcesContent":["// reference https://github.com/hi-ogawa/vite-plugin-fullstack/blob/28e9540a68529c58842e9a3bf17d2193a065d524/examples/island/src/framework/island/plugin.ts\nimport { withMagicString } from \"rolldown-string\";\nimport type { ParserOptions, Plugin } from \"vite\";\nimport { Visitor } from \"vite\";\n\nconst ISLAND_REGEX = /\\.island\\.(j|t)sx?$/;\n\ntype Lang = NonNullable<ParserOptions[\"lang\"]>;\n\nfunction langFromId(id: string): Lang {\n\tif (id.endsWith(\".tsx\")) return \"tsx\";\n\tif (id.endsWith(\".ts\")) return \"ts\";\n\tif (id.endsWith(\".jsx\")) return \"jsx\";\n\treturn \"js\";\n}\n\nexport const islands = (): Plugin[] => {\n\treturn [\n\t\t{\n\t\t\tname: \"yamf:islands\",\n\t\t\ttransform: {\n\t\t\t\tfilter: { id: ISLAND_REGEX },\n\t\t\t\thandler: withMagicString(function (this, s, id) {\n\t\t\t\t\tif (this.environment.name !== \"ssr\") {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst program = this.parse(s.original, {\n\t\t\t\t\t\tlang: langFromId(id),\n\t\t\t\t\t});\n\n\t\t\t\t\t// prepend server island runtime + asset imports:\n\t\t\t\t\t// import * as __runtime from \"@pajecawav/yamf/server\";\n\t\t\t\t\t// import __assets from \"<id>?assets=client\";\n\t\t\t\t\ts.prepend(`import * as __runtime from \"@pajecawav/yamf/server\";\\n`);\n\t\t\t\t\ts.prepend(`import __assets from \"${id}?assets=client\";\\n`);\n\n\t\t\t\t\tconst seenExports = new Set<string>();\n\n\t\t\t\t\tnew Visitor({\n\t\t\t\t\t\tExportDefaultDeclaration(node) {\n\t\t\t\t\t\t\tconst declaration = node.declaration;\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tdeclaration.type !== \"FunctionDeclaration\" &&\n\t\t\t\t\t\t\t\tdeclaration.type !== \"FunctionExpression\" &&\n\t\t\t\t\t\t\t\tdeclaration.type !== \"ArrowFunctionExpression\"\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// export default <fn> ->\n\t\t\t\t\t\t\t// const __ISLAND__ = <fn>; export default __runtime.createIsland(__ISLAND__, \"default\", __assets)\n\t\t\t\t\t\t\ts.overwrite(node.start, declaration.start, \"const __ISLAND__ = \");\n\t\t\t\t\t\t\ts.appendRight(\n\t\t\t\t\t\t\t\tdeclaration.end,\n\t\t\t\t\t\t\t\t`; export default __runtime.createIsland(__ISLAND__, \"default\", __assets)`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tExportNamedDeclaration(node) {\n\t\t\t\t\t\t\tconst declaration = node.declaration;\n\t\t\t\t\t\t\tif (declaration?.type === \"VariableDeclaration\") {\n\t\t\t\t\t\t\t\tconst declarator = declaration.declarations[0];\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t!declarator ||\n\t\t\t\t\t\t\t\t\tdeclarator.id.type !== \"Identifier\" ||\n\t\t\t\t\t\t\t\t\t!declarator.init ||\n\t\t\t\t\t\t\t\t\tseenExports.has(declarator.id.name)\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst exportName = declarator.id.name;\n\t\t\t\t\t\t\t\tseenExports.add(exportName);\n\n\t\t\t\t\t\t\t\t// export const <name> = <init> ->\n\t\t\t\t\t\t\t\t// const __wrap_<name> = <init>; export const <name> = __runtime.createIsland(__wrap_<name>, \"<name>\", __assets)\n\t\t\t\t\t\t\t\ts.overwrite(\n\t\t\t\t\t\t\t\t\tnode.start,\n\t\t\t\t\t\t\t\t\tdeclarator.init.start,\n\t\t\t\t\t\t\t\t\t`const __wrap_${exportName} = `,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\ts.appendRight(\n\t\t\t\t\t\t\t\t\tdeclarator.init.end,\n\t\t\t\t\t\t\t\t\t`; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, \"${exportName}\", __assets)`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} else if (declaration?.type === \"FunctionDeclaration\") {\n\t\t\t\t\t\t\t\tconst fnId = declaration.id;\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t!fnId ||\n\t\t\t\t\t\t\t\t\tfnId.type !== \"Identifier\" ||\n\t\t\t\t\t\t\t\t\tseenExports.has(fnId.name)\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst exportName = fnId.name;\n\t\t\t\t\t\t\t\tseenExports.add(exportName);\n\n\t\t\t\t\t\t\t\t// export function <name>() {} ->\n\t\t\t\t\t\t\t\t// function __wrap_<name>() {}; export const <name> = __runtime.createIsland(__wrap_<name>, \"<name>\", __assets)\n\t\t\t\t\t\t\t\ts.remove(node.start, declaration.start);\n\t\t\t\t\t\t\t\ts.overwrite(fnId.start, fnId.end, `__wrap_${exportName}`);\n\t\t\t\t\t\t\t\ts.appendRight(\n\t\t\t\t\t\t\t\t\tdeclaration.end,\n\t\t\t\t\t\t\t\t\t`; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, \"${exportName}\", __assets)`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}).visit(program);\n\t\t\t\t}),\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"yamf:islands:raw-import\",\n\t\t\ttransform: {\n\t\t\t\torder: \"post\",\n\t\t\t\thandler: withMagicString(function (s) {\n\t\t\t\t\tif (s.original.includes(\"__island_raw_import__\")) {\n\t\t\t\t\t\ts.replaceAll(\"__island_raw_import__\", \"import\");\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t},\n\t\t},\n\t];\n};\n"],"mappings":";;;AAKA,MAAM,eAAe;AAIrB,SAAS,WAAW,IAAkB;CACrC,IAAI,GAAG,SAAS,MAAM,GAAG,OAAO;CAChC,IAAI,GAAG,SAAS,KAAK,GAAG,OAAO;CAC/B,IAAI,GAAG,SAAS,MAAM,GAAG,OAAO;CAChC,OAAO;AACR;AAEA,MAAa,gBAA0B;CACtC,OAAO,CACN;EACC,MAAM;EACN,WAAW;GACV,QAAQ,EAAE,IAAI,aAAa;GAC3B,SAAS,gBAAgB,SAAgB,GAAG,IAAI;IAC/C,IAAI,KAAK,YAAY,SAAS,OAC7B;IAGD,MAAM,UAAU,KAAK,MAAM,EAAE,UAAU,EACtC,MAAM,WAAW,EAAE,EACpB,CAAC;IAKD,EAAE,QAAQ,wDAAwD;IAClE,EAAE,QAAQ,yBAAyB,GAAG,mBAAmB;IAEzD,MAAM,8BAAc,IAAI,IAAY;IAEpC,IAAI,QAAQ;KACX,yBAAyB,MAAM;MAC9B,MAAM,cAAc,KAAK;MACzB,IACC,YAAY,SAAS,yBACrB,YAAY,SAAS,wBACrB,YAAY,SAAS,2BAErB;MAKD,EAAE,UAAU,KAAK,OAAO,YAAY,OAAO,qBAAqB;MAChE,EAAE,YACD,YAAY,KACZ,0EACD;KACD;KACA,uBAAuB,MAAM;MAC5B,MAAM,cAAc,KAAK;MACzB,IAAI,aAAa,SAAS,uBAAuB;OAChD,MAAM,aAAa,YAAY,aAAa;OAC5C,IACC,CAAC,cACD,WAAW,GAAG,SAAS,gBACvB,CAAC,WAAW,QACZ,YAAY,IAAI,WAAW,GAAG,IAAI,GAElC;OAED,MAAM,aAAa,WAAW,GAAG;OACjC,YAAY,IAAI,UAAU;OAI1B,EAAE,UACD,KAAK,OACL,WAAW,KAAK,OAChB,gBAAgB,WAAW,IAC5B;OACA,EAAE,YACD,WAAW,KAAK,KAChB,kBAAkB,WAAW,mCAAmC,WAAW,KAAK,WAAW,aAC5F;MACD,OAAO,IAAI,aAAa,SAAS,uBAAuB;OACvD,MAAM,OAAO,YAAY;OACzB,IACC,CAAC,QACD,KAAK,SAAS,gBACd,YAAY,IAAI,KAAK,IAAI,GAEzB;OAED,MAAM,aAAa,KAAK;OACxB,YAAY,IAAI,UAAU;OAI1B,EAAE,OAAO,KAAK,OAAO,YAAY,KAAK;OACtC,EAAE,UAAU,KAAK,OAAO,KAAK,KAAK,UAAU,YAAY;OACxD,EAAE,YACD,YAAY,KACZ,kBAAkB,WAAW,mCAAmC,WAAW,KAAK,WAAW,aAC5F;MACD;KACD;IACD,CAAC,CAAC,CAAC,MAAM,OAAO;GACjB,CAAC;EACF;CACD,GACA;EACC,MAAM;EACN,WAAW;GACV,OAAO;GACP,SAAS,gBAAgB,SAAU,GAAG;IACrC,IAAI,EAAE,SAAS,SAAS,uBAAuB,GAC9C,EAAE,WAAW,yBAAyB,QAAQ;GAEhD,CAAC;EACF;CACD,CACD;AACD"}
@@ -0,0 +1,23 @@
1
+ import { js } from "./shared/utils.mjs";
2
+ //#region src/vite/virtual-error-handler.ts
3
+ const virtualErrorHandler = (options) => {
4
+ const virtualModuleId = "virtual:yamf:error-handler";
5
+ const resolvedVirtualModuleId = "\0virtual:yamf:error-handler";
6
+ return {
7
+ name: "yamf:virtual-error-handler",
8
+ resolveId(id) {
9
+ if (id === virtualModuleId) return resolvedVirtualModuleId;
10
+ },
11
+ load(id) {
12
+ if (id !== resolvedVirtualModuleId) return;
13
+ const errorHandler = options?.nitro?.errorHandler;
14
+ if (!errorHandler) return js`export const errorHandler = null;`;
15
+ if (Array.isArray(errorHandler)) throw new Error("Multiple error handlers are not supported");
16
+ return js`export { default as errorHandler } from "${errorHandler}";`;
17
+ }
18
+ };
19
+ };
20
+ //#endregion
21
+ export { virtualErrorHandler };
22
+
23
+ //# sourceMappingURL=virtual-error-handler.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"virtual-error-handler.mjs","names":[],"sources":["../../src/vite/virtual-error-handler.ts"],"sourcesContent":["import type { NitroPluginConfig } from \"nitro/vite\";\nimport type { Plugin } from \"vite\";\nimport { js } from \"../shared/utils\";\n\nexport interface VirtualErrorHandlerOptions {\n\tnitro?: NitroPluginConfig;\n}\n\nexport const virtualErrorHandler = (options?: VirtualErrorHandlerOptions): Plugin => {\n\tconst virtualModuleId = \"virtual:yamf:error-handler\";\n\tconst resolvedVirtualModuleId = \"\\0\" + virtualModuleId;\n\n\treturn {\n\t\tname: \"yamf:virtual-error-handler\",\n\t\tresolveId(id) {\n\t\t\tif (id === virtualModuleId) {\n\t\t\t\treturn resolvedVirtualModuleId;\n\t\t\t}\n\n\t\t\treturn undefined;\n\t\t},\n\t\tload(id) {\n\t\t\tif (id !== resolvedVirtualModuleId) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst errorHandler = options?.nitro?.errorHandler;\n\n\t\t\tif (!errorHandler) {\n\t\t\t\treturn js`export const errorHandler = null;`;\n\t\t\t}\n\n\t\t\tif (Array.isArray(errorHandler)) {\n\t\t\t\tthrow new Error(\"Multiple error handlers are not supported\");\n\t\t\t}\n\n\t\t\treturn js`export { default as errorHandler } from \"${errorHandler}\";`;\n\t\t},\n\t};\n};\n"],"mappings":";;AAQA,MAAa,uBAAuB,YAAiD;CACpF,MAAM,kBAAkB;CACxB,MAAM,0BAA0B;CAEhC,OAAO;EACN,MAAM;EACN,UAAU,IAAI;GACb,IAAI,OAAO,iBACV,OAAO;EAIT;EACA,KAAK,IAAI;GACR,IAAI,OAAO,yBACV;GAGD,MAAM,eAAe,SAAS,OAAO;GAErC,IAAI,CAAC,cACJ,OAAO,EAAE;GAGV,IAAI,MAAM,QAAQ,YAAY,GAC7B,MAAM,IAAI,MAAM,2CAA2C;GAG5D,OAAO,EAAE,4CAA4C,aAAa;EACnE;CACD;AACD"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pajecawav/yamf",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "description": "Yet another meta framework",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/pajecawav/yamf#readme",
@@ -30,43 +30,36 @@
30
30
  "access": "public"
31
31
  },
32
32
  "peerDependencies": {
33
- "@hono/react-compat": "^0.0.3",
34
33
  "hono": "^4.12.30",
35
34
  "vite": "^8.1.5"
36
35
  },
37
36
  "dependencies": {
38
- "@babel/generator": "^7.29.7",
39
- "@babel/parser": "^7.29.7",
40
- "@babel/traverse": "^7.29.7",
41
- "@babel/types": "^7.29.7",
42
- "devalue": "^5.8.1",
37
+ "@hono/react-compat": "^0.0.3",
38
+ "devalue": "^5.8.2",
43
39
  "exsolve": "^1.1.0",
44
40
  "nitro": "3.0.260610-beta",
41
+ "rolldown-string": "^0.3.1",
45
42
  "rou3": "^0.9.1",
46
43
  "ufo": "^1.6.4",
47
- "unhead": "^3.1.8",
44
+ "unhead": "^3.2.3",
48
45
  "wouter": "^3.10.0"
49
46
  },
50
47
  "devDependencies": {
51
48
  "@hono/react-compat": "^0.0.3",
52
49
  "@pajecawav/tools": "^0.0.7",
53
- "@types/babel__generator": "^7.27.0",
54
- "@types/babel__traverse": "^7.28.0",
50
+ "@playwright/test": "^1.61.1",
55
51
  "@types/node": "^24.13.3",
56
- "@vitest/coverage-v8": "^4.1.10",
57
- "@vitest/ui": "^4.1.10",
58
52
  "cross-env": "^10.1.0",
59
- "hono": "^4.12.30",
53
+ "hono": "^4.12.32",
60
54
  "husky": "^9.1.7",
61
55
  "npm-run-all2": "^9.0.2",
62
- "oxfmt": "^0.59.0",
63
- "oxlint": "^1.74.0",
64
- "oxlint-tsgolint": "^0.25.0",
65
- "publint": "^0.3.21",
66
- "tsdown": "0.22.9",
56
+ "oxfmt": "^0.60.0",
57
+ "oxlint": "^1.75.0",
58
+ "oxlint-tsgolint": "^7.0.2001",
59
+ "publint": "^0.3.22",
60
+ "tsdown": "0.22.14",
67
61
  "typescript": "~7.0.2",
68
- "vite": "^8.1.5",
69
- "vitest": "^4.1.10"
62
+ "vite": "^8.1.5"
70
63
  },
71
64
  "scripts": {
72
65
  "build": "tsdown",
@@ -74,10 +67,8 @@
74
67
  "play": "pnpm --filter=playground dev",
75
68
  "play:build": "pnpm --filter=playground build",
76
69
  "play:preview": "pnpm --filter=playground preview",
77
- "test": "vitest run --passWithNoTests",
78
- "test:watch": "vitest watch",
79
- "test:coverage": "vitest run --coverage",
80
- "test:ui": "vitest --ui",
70
+ "test": "playwright test",
71
+ "test:ui": "playwright test --ui",
81
72
  "lint": "cross-env FORCE_COLOR=1 run-p -l lint:*",
82
73
  "lint:oxlint": "oxlint .",
83
74
  "lint:tsc": "tsc -b --noEmit",
@@ -1,9 +1,17 @@
1
1
  import path from "node:path";
2
- import type { EventHandlerRequest, EventHandlerWithFetch, H3Event } from "nitro/h3";
3
- import { defineHandler, HTTPError, writeEarlyHints } from "nitro/h3";
2
+ import {
3
+ defineHandler,
4
+ EventHandler,
5
+ H3Event,
6
+ HTTPError,
7
+ writeEarlyHints,
8
+ type EventHandlerRequest,
9
+ type EventHandlerWithFetch,
10
+ } from "nitro/h3";
4
11
  import { addRoute, createRouter, findRoute } from "rou3";
5
12
  import { withLeadingSlash, withoutTrailingSlash } from "ufo";
6
13
  import { clientAssets } from "virtual:yamf:assets";
14
+ import { errorHandler } from "virtual:yamf:error-handler";
7
15
  import { pages, assets as pagesServerAssets } from "virtual:yamf:pages";
8
16
  import { rootAssets } from "virtual:yamf:root";
9
17
  import type { PageHandler } from "#/page";
@@ -52,7 +60,7 @@ export interface DefineServerEntryOptions {
52
60
  }
53
61
 
54
62
  export const defineServerEntry = (options?: DefineServerEntryOptions): ServerEntry => {
55
- return defineHandler(async event => {
63
+ const rootHandler: EventHandler<EventHandlerRequest, Promise<unknown>> = async event => {
56
64
  const route = findRoute(router, "GET", event.url.pathname);
57
65
 
58
66
  if (!route) {
@@ -79,5 +87,19 @@ export const defineServerEntry = (options?: DefineServerEntryOptions): ServerEnt
79
87
  assets,
80
88
  head: typeof options?.head === "function" ? options.head(event) : options?.head,
81
89
  });
90
+ };
91
+
92
+ return defineHandler(async event => {
93
+ if (!errorHandler) {
94
+ return rootHandler(event);
95
+ }
96
+
97
+ try {
98
+ return await rootHandler(event);
99
+ } catch (error) {
100
+ const httpError = error instanceof HTTPError ? error : new HTTPError({ cause: error });
101
+
102
+ return errorHandler(httpError, event);
103
+ }
82
104
  });
83
105
  };
package/src/virtual.d.ts CHANGED
@@ -18,3 +18,11 @@ declare module "virtual:yamf:root" {
18
18
 
19
19
  export { Root, rootAssets };
20
20
  }
21
+
22
+ declare module "virtual:yamf:error-handler" {
23
+ import type { H3Event, HTTPError } from "nitro/h3";
24
+
25
+ type ErrorHandler = (error: HTTPError, event: H3Event) => Promise<Response | undefined>;
26
+
27
+ export const errorHandler: ErrorHandler | null;
28
+ }
package/src/vite/index.ts CHANGED
@@ -4,6 +4,7 @@ import { nitro } from "nitro/vite";
4
4
  import type { EnvironmentOptions, PluginOption } from "vite";
5
5
  import { islands } from "./islands";
6
6
  import { virtualAssets } from "./virtual-assets";
7
+ import { virtualErrorHandler } from "./virtual-error-handler";
7
8
  import { virtualPages } from "./virtual-pages";
8
9
  import { virtualRoot } from "./virtual-root";
9
10
  import { virtualTemplate } from "./virtual-template";
@@ -75,6 +76,7 @@ const yamf = (options?: YamfOptions): PluginOption[] => {
75
76
 
76
77
  plugins.push(islands());
77
78
 
79
+ plugins.push(virtualErrorHandler(options));
78
80
  plugins.push(virtualAssets());
79
81
  plugins.push(virtualPages());
80
82
  plugins.push(virtualTemplate());
@@ -1,227 +1,122 @@
1
1
  // reference https://github.com/hi-ogawa/vite-plugin-fullstack/blob/28e9540a68529c58842e9a3bf17d2193a065d524/examples/island/src/framework/island/plugin.ts
2
- import { generate } from "@babel/generator";
3
- import { parse } from "@babel/parser";
4
- import _traverse from "@babel/traverse";
5
- import {
6
- callExpression,
7
- exportDefaultDeclaration,
8
- exportNamedDeclaration,
9
- functionDeclaration,
10
- functionExpression,
11
- identifier,
12
- importDeclaration,
13
- importDefaultSpecifier,
14
- importNamespaceSpecifier,
15
- memberExpression,
16
- stringLiteral,
17
- variableDeclaration,
18
- variableDeclarator,
19
- } from "@babel/types";
20
- import type { Plugin } from "vite";
21
-
22
- // @ts-ignore
23
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
24
- const traverse = (_traverse.default as typeof _traverse) ?? _traverse;
2
+ import { withMagicString } from "rolldown-string";
3
+ import type { ParserOptions, Plugin } from "vite";
4
+ import { Visitor } from "vite";
25
5
 
26
6
  const ISLAND_REGEX = /\.island\.(j|t)sx?$/;
27
7
 
8
+ type Lang = NonNullable<ParserOptions["lang"]>;
9
+
10
+ function langFromId(id: string): Lang {
11
+ if (id.endsWith(".tsx")) return "tsx";
12
+ if (id.endsWith(".ts")) return "ts";
13
+ if (id.endsWith(".jsx")) return "jsx";
14
+ return "js";
15
+ }
16
+
28
17
  export const islands = (): Plugin[] => {
29
18
  return [
30
19
  {
31
20
  name: "yamf:islands",
32
21
  transform: {
33
22
  filter: { id: ISLAND_REGEX },
34
- handler(code, id) {
23
+ handler: withMagicString(function (this, s, id) {
35
24
  if (this.environment.name !== "ssr") {
36
25
  return;
37
26
  }
38
27
 
39
- const seenExports = new Set<string>();
40
-
41
- const ast = parse(code, {
42
- sourceType: "module",
43
- plugins: ["typescript", "jsx"],
28
+ const program = this.parse(s.original, {
29
+ lang: langFromId(id),
44
30
  });
45
31
 
46
- traverse(ast, {
47
- Program(path) {
48
- // prepends server island runtime
49
- // import * as __runtime from "yamf/server";
50
- path.unshiftContainer(
51
- "body",
52
- importDeclaration(
53
- [importNamespaceSpecifier(identifier("__runtime"))],
54
- stringLiteral("@pajecawav/yamf/server"),
55
- ),
56
- );
32
+ // prepend server island runtime + asset imports:
33
+ // import * as __runtime from "@pajecawav/yamf/server";
34
+ // import __assets from "<id>?assets=client";
35
+ s.prepend(`import * as __runtime from "@pajecawav/yamf/server";\n`);
36
+ s.prepend(`import __assets from "${id}?assets=client";\n`);
57
37
 
58
- // prepends asset imports for the island:
59
- // import __assets from "MODULE_ID?assets=client";
60
- path.unshiftContainer(
61
- "body",
62
- importDeclaration(
63
- [importDefaultSpecifier(identifier("__assets"))],
64
- stringLiteral(`${id}?assets=client`),
65
- ),
66
- );
67
- },
68
- ExportDefaultDeclaration(path) {
69
- const declarationType = path.node.declaration.type;
38
+ const seenExports = new Set<string>();
70
39
 
40
+ new Visitor({
41
+ ExportDefaultDeclaration(node) {
42
+ const declaration = node.declaration;
71
43
  if (
72
- !(
73
- declarationType === "FunctionDeclaration" ||
74
- declarationType === "FunctionExpression" ||
75
- declarationType === "ArrowFunctionExpression"
76
- )
44
+ declaration.type !== "FunctionDeclaration" &&
45
+ declaration.type !== "FunctionExpression" &&
46
+ declaration.type !== "ArrowFunctionExpression"
77
47
  ) {
78
48
  return;
79
49
  }
80
50
 
81
- const originalFunction =
82
- path.node.declaration.type === "FunctionExpression" ||
83
- path.node.declaration.type === "ArrowFunctionExpression"
84
- ? path.node.declaration
85
- : functionExpression(
86
- null,
87
- path.node.declaration.params,
88
- path.node.declaration.body,
89
- undefined,
90
- path.node.declaration.async,
91
- );
92
-
93
- const islandIdentifier = identifier("__ISLAND__");
94
-
95
- path.insertBefore(
96
- variableDeclaration("const", [
97
- variableDeclarator(islandIdentifier, originalFunction),
98
- ]),
99
- );
100
-
101
- path.replaceWith(
102
- exportDefaultDeclaration(
103
- callExpression(
104
- memberExpression(
105
- identifier("__runtime"),
106
- identifier("createIsland"),
107
- ),
108
- [
109
- islandIdentifier,
110
- stringLiteral("default"),
111
- identifier("__assets"),
112
- ],
113
- ),
114
- ),
51
+ // export default <fn> ->
52
+ // const __ISLAND__ = <fn>; export default __runtime.createIsland(__ISLAND__, "default", __assets)
53
+ s.overwrite(node.start, declaration.start, "const __ISLAND__ = ");
54
+ s.appendRight(
55
+ declaration.end,
56
+ `; export default __runtime.createIsland(__ISLAND__, "default", __assets)`,
115
57
  );
116
58
  },
117
- ExportNamedDeclaration(path) {
118
- if (path.node.declaration?.type === "VariableDeclaration") {
119
- const declaration = path.node.declaration.declarations.at(0);
120
-
59
+ ExportNamedDeclaration(node) {
60
+ const declaration = node.declaration;
61
+ if (declaration?.type === "VariableDeclaration") {
62
+ const declarator = declaration.declarations[0];
121
63
  if (
122
- !declaration ||
123
- declaration.id.type !== "Identifier" ||
124
- seenExports.has(declaration.id.name)
64
+ !declarator ||
65
+ declarator.id.type !== "Identifier" ||
66
+ !declarator.init ||
67
+ seenExports.has(declarator.id.name)
125
68
  ) {
126
69
  return;
127
70
  }
128
- const exportName = declaration.id.name;
71
+ const exportName = declarator.id.name;
129
72
  seenExports.add(exportName);
130
73
 
131
- const islandIdentifier = identifier(`__wrap_${exportName}`);
132
-
133
- path.insertBefore(
134
- variableDeclaration("const", [
135
- variableDeclarator(islandIdentifier, declaration.init),
136
- ]),
74
+ // export const <name> = <init> ->
75
+ // const __wrap_<name> = <init>; export const <name> = __runtime.createIsland(__wrap_<name>, "<name>", __assets)
76
+ s.overwrite(
77
+ node.start,
78
+ declarator.init.start,
79
+ `const __wrap_${exportName} = `,
137
80
  );
138
-
139
- path.replaceWith(
140
- exportNamedDeclaration(
141
- variableDeclaration("const", [
142
- variableDeclarator(
143
- identifier(exportName),
144
- callExpression(
145
- memberExpression(
146
- identifier("__runtime"),
147
- identifier("createIsland"),
148
- ),
149
- [
150
- islandIdentifier,
151
- stringLiteral(exportName),
152
- identifier("__assets"),
153
- ],
154
- ),
155
- ),
156
- ]),
157
- ),
81
+ s.appendRight(
82
+ declarator.init.end,
83
+ `; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, "${exportName}", __assets)`,
158
84
  );
159
- } else if (path.node.declaration?.type === "FunctionDeclaration") {
160
- const declaration = path.node.declaration;
161
-
85
+ } else if (declaration?.type === "FunctionDeclaration") {
86
+ const fnId = declaration.id;
162
87
  if (
163
- !declaration ||
164
- declaration.id?.type !== "Identifier" ||
165
- seenExports.has(declaration.id.name)
88
+ !fnId ||
89
+ fnId.type !== "Identifier" ||
90
+ seenExports.has(fnId.name)
166
91
  ) {
167
92
  return;
168
93
  }
169
- const exportName = declaration.id.name;
94
+ const exportName = fnId.name;
170
95
  seenExports.add(exportName);
171
96
 
172
- const islandIdentifier = identifier(`__wrap_${exportName}`);
173
-
174
- path.insertBefore(
175
- functionDeclaration(
176
- islandIdentifier,
177
- declaration.params,
178
- declaration.body,
179
- declaration.generator,
180
- declaration.async,
181
- ),
182
- );
183
-
184
- path.replaceWith(
185
- exportNamedDeclaration(
186
- variableDeclaration("const", [
187
- variableDeclarator(
188
- identifier(exportName),
189
- callExpression(
190
- memberExpression(
191
- identifier("__runtime"),
192
- identifier("createIsland"),
193
- ),
194
- [
195
- islandIdentifier,
196
- stringLiteral(exportName),
197
- identifier("__assets"),
198
- ],
199
- ),
200
- ),
201
- ]),
202
- ),
97
+ // export function <name>() {} ->
98
+ // function __wrap_<name>() {}; export const <name> = __runtime.createIsland(__wrap_<name>, "<name>", __assets)
99
+ s.remove(node.start, declaration.start);
100
+ s.overwrite(fnId.start, fnId.end, `__wrap_${exportName}`);
101
+ s.appendRight(
102
+ declaration.end,
103
+ `; export const ${exportName} = __runtime.createIsland(__wrap_${exportName}, "${exportName}", __assets)`,
203
104
  );
204
105
  }
205
106
  },
206
- });
207
-
208
- const result = generate(ast);
209
-
210
- return { code: result.code, map: result.map };
211
- },
107
+ }).visit(program);
108
+ }),
212
109
  },
213
110
  },
214
111
  {
215
112
  name: "yamf:islands:raw-import",
216
113
  transform: {
217
114
  order: "post",
218
- handler(code) {
219
- if (code.includes("__island_raw_import__")) {
220
- return code.replaceAll("__island_raw_import__", "import");
115
+ handler: withMagicString(function (s) {
116
+ if (s.original.includes("__island_raw_import__")) {
117
+ s.replaceAll("__island_raw_import__", "import");
221
118
  }
222
-
223
- return undefined;
224
- },
119
+ }),
225
120
  },
226
121
  },
227
122
  ];
@@ -0,0 +1,40 @@
1
+ import type { NitroPluginConfig } from "nitro/vite";
2
+ import type { Plugin } from "vite";
3
+ import { js } from "../shared/utils";
4
+
5
+ export interface VirtualErrorHandlerOptions {
6
+ nitro?: NitroPluginConfig;
7
+ }
8
+
9
+ export const virtualErrorHandler = (options?: VirtualErrorHandlerOptions): Plugin => {
10
+ const virtualModuleId = "virtual:yamf:error-handler";
11
+ const resolvedVirtualModuleId = "\0" + virtualModuleId;
12
+
13
+ return {
14
+ name: "yamf:virtual-error-handler",
15
+ resolveId(id) {
16
+ if (id === virtualModuleId) {
17
+ return resolvedVirtualModuleId;
18
+ }
19
+
20
+ return undefined;
21
+ },
22
+ load(id) {
23
+ if (id !== resolvedVirtualModuleId) {
24
+ return;
25
+ }
26
+
27
+ const errorHandler = options?.nitro?.errorHandler;
28
+
29
+ if (!errorHandler) {
30
+ return js`export const errorHandler = null;`;
31
+ }
32
+
33
+ if (Array.isArray(errorHandler)) {
34
+ throw new Error("Multiple error handlers are not supported");
35
+ }
36
+
37
+ return js`export { default as errorHandler } from "${errorHandler}";`;
38
+ },
39
+ };
40
+ };