@teyik0/furin 0.1.0-alpha.7 → 0.1.0-alpha.8

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/client.d.ts CHANGED
@@ -8,6 +8,14 @@ type ToRecord<T> = {
8
8
  type ResolvedSchema<T> = [T] extends [Unset] ? Unset : T extends AnySchema ? UnwrapSchema<T> : Unset;
9
9
  type MergeSchema<TParent, TOwn> = [TParent] extends [Unset] ? TOwn : [TOwn] extends [Unset] ? TParent : TParent & TOwn;
10
10
  type NormalizeUnset<T> = [T] extends [Unset] ? {} : T;
11
+ /**
12
+ * Transforms each key of a parent-data record into an individual `Promise<T>`.
13
+ * This is what child loaders see for inherited fields — direct values from
14
+ * `RouteContext` (request, params, set, …) remain unchanged.
15
+ */
16
+ type PromisifyData<T extends Record<string, unknown>> = {
17
+ [K in keyof T]: Promise<T[K]>;
18
+ };
11
19
  export interface RouteContext<TParams = {}, TQuery = {}> {
12
20
  cookie: Record<string, Cookie<unknown>>;
13
21
  headers: Record<string, string | undefined>;
@@ -89,14 +97,11 @@ export interface HeadOptions {
89
97
  children: string;
90
98
  }>;
91
99
  }
92
- export type LoaderDeps = (route: {
93
- __type: string;
94
- }) => Promise<Record<string, unknown>>;
95
100
  type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown ? Awaited<ReturnType<TLoader>> extends object ? ToRecord<Awaited<ReturnType<TLoader>>> : {} : {};
96
101
  export interface PageConfig<TParentData extends Record<string, unknown>, TParams, TQuery, TPageLoaderData extends object = {}> {
97
102
  component: React.FC<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
98
103
  head?: (ctx: ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData) => HeadOptions;
99
- loader?: (ctx: RouteContext<TParams, TQuery> & TParentData, deps: TypedDeps) => Promise<TPageLoaderData> | TPageLoaderData;
104
+ loader?: (ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>) => Promise<TPageLoaderData> | TPageLoaderData;
100
105
  staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
101
106
  }
102
107
  export interface RuntimeRoute {
@@ -104,7 +109,7 @@ export interface RuntimeRoute {
104
109
  layout?: React.FC<Record<string, unknown> & {
105
110
  children: React.ReactNode;
106
111
  }>;
107
- loader?(ctx: Record<string, unknown>, deps: LoaderDeps): Promise<Record<string, unknown>> | Record<string, unknown>;
112
+ loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
108
113
  mode?: "ssr" | "ssg" | "isr";
109
114
  params?: unknown;
110
115
  parent?: RuntimeRoute;
@@ -116,7 +121,7 @@ export interface RuntimePage {
116
121
  _route: RuntimeRoute;
117
122
  component: React.FC<Record<string, unknown>>;
118
123
  head?(ctx: Record<string, unknown>): HeadOptions;
119
- loader?(ctx: Record<string, unknown>, deps: LoaderDeps): Promise<Record<string, unknown>> | Record<string, unknown>;
124
+ loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
120
125
  staticParams?(): Promise<Record<string, string>[]> | Record<string, string>[];
121
126
  }
122
127
  export interface RouteRef<TData extends Record<string, unknown> = Record<string, unknown>, TParams = unknown, TQuery = unknown> {
@@ -132,16 +137,16 @@ interface PageResult<TData extends Record<string, unknown>, TParams, TQuery, TPa
132
137
  _route: Route<TData, TParams, TQuery>;
133
138
  component: React.FC<TData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
134
139
  head?: (ctx: ComponentProps<TParams, TQuery> & TData & TPageLoaderData) => HeadOptions;
135
- loader?: (ctx: RouteContext<TParams, TQuery> & TData, deps: TypedDeps) => Promise<TPageLoaderData> | TPageLoaderData;
140
+ loader?: (ctx: RouteContext<TParams, TQuery> & PromisifyData<TData>) => Promise<TPageLoaderData> | TPageLoaderData;
136
141
  }
137
142
  export interface Route<TParentData extends Record<string, unknown>, TParams, TQuery> {
138
143
  __type: "FURIN_ROUTE";
139
144
  layout?: React.FC<TParentData & {
140
145
  children: React.ReactNode;
141
146
  } & ComponentProps<TParams, TQuery>>;
142
- loader?(ctx: RouteContext<TParams, TQuery> & TParentData, deps: TypedDeps): Promise<TParentData> | TParentData;
147
+ loader?(ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>): Promise<TParentData> | TParentData;
143
148
  mode?: "ssr" | "ssg" | "isr";
144
- page<TLoader extends (ctx: RouteContext<TParams, TQuery> & TParentData, deps: TypedDeps) => unknown, TPageLoaderData extends object = ExtractLoaderReturn<TLoader>>(config: {
149
+ page<TLoader extends (ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>) => unknown, TPageLoaderData extends object = ExtractLoaderReturn<TLoader>>(config: {
145
150
  loader: TLoader;
146
151
  mode?: "ssr" | "ssg" | "isr";
147
152
  revalidate?: number;
@@ -163,7 +168,6 @@ export interface Route<TParentData extends Record<string, unknown>, TParams, TQu
163
168
  ref: RouteRef<TParentData, TParams, TQuery>;
164
169
  revalidate?: number;
165
170
  }
166
- type TypedDeps = <TData extends Record<string, unknown>>(route: Route<TData, any, any>) => Promise<TData>;
167
171
  export declare function createRoute<TParentRef extends RouteRef | undefined = undefined, TParamsSchema extends AnySchema | Unset = Unset, TQuerySchema extends AnySchema | Unset = Unset, TLoaderData extends object = {}>(config?: {
168
172
  parent?: {
169
173
  ref: TParentRef;
@@ -174,7 +178,7 @@ export declare function createRoute<TParentRef extends RouteRef | undefined = un
174
178
  revalidate?: number;
175
179
  params?: TParamsSchema;
176
180
  query?: TQuerySchema;
177
- loader?: (ctx: RouteContext<Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"], Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]> & ResolveParent<TParentRef>["data"], deps: TypedDeps) => Promise<TLoaderData> | TLoaderData;
181
+ loader?: (ctx: RouteContext<Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"], Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]> & PromisifyData<ResolveParent<TParentRef>["data"]>) => Promise<TLoaderData> | TLoaderData;
178
182
  layout?: React.FC<Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"] & {
179
183
  children: React.ReactNode;
180
184
  } & ComponentProps<Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"], Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]>>;
package/dist/config.d.ts CHANGED
@@ -1,16 +1,47 @@
1
1
  export type { BunPlugin } from "bun";
2
- export declare const BUILD_TARGETS: readonly ["bun", "node", "vercel", "cloudflare"];
2
+ export declare const BUILD_TARGETS: readonly ["bun", "node", "vercel", "cloudflare", "static"];
3
3
  export type BuildTarget = (typeof BUILD_TARGETS)[number];
4
+ /**
5
+ * Configuration for the `static` build target.
6
+ * Produces a fully pre-rendered directory deployable to any static host
7
+ * (GitHub Pages, Netlify, Surge.sh, …).
8
+ */
9
+ export interface StaticExportConfig {
10
+ /**
11
+ * Sub-path prefix for GitHub Pages sub-path deployments.
12
+ * e.g. "/furin" when the site is served at `https://user.github.io/furin/`.
13
+ * Must start with "/" and have no trailing slash.
14
+ * Default: "" (site served at root).
15
+ */
16
+ basePath?: string;
17
+ /**
18
+ * Behaviour when SSR or ISR routes are encountered during a static build.
19
+ * - "error" (default): throw at build time with the list of incompatible routes.
20
+ * - "skip": emit a warning and omit those routes from the output.
21
+ */
22
+ onSSR?: "error" | "skip";
23
+ /**
24
+ * Output directory for the static export.
25
+ * Default: "dist".
26
+ */
27
+ outDir?: string;
28
+ }
4
29
  export declare const configSchema: import("@sinclair/typebox").TObject<{
5
30
  rootDir: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
6
31
  pagesDir: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
7
32
  serverEntry: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
8
- targets: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<import("@sinclair/typebox").TLiteral<"bun" | "node" | "vercel" | "cloudflare">[]>>>;
33
+ targets: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<import("@sinclair/typebox").TLiteral<"bun" | "node" | "vercel" | "cloudflare" | "static">[]>>>;
9
34
  bun: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
10
35
  compile: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"server">, import("@sinclair/typebox").TLiteral<"embed">]>>;
11
36
  }>>;
37
+ static: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
38
+ basePath: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
39
+ outDir: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
40
+ onSSR: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"error">, import("@sinclair/typebox").TLiteral<"skip">]>>;
41
+ }>>;
12
42
  }>;
13
43
  export type FurinConfig = (typeof configSchema)["static"] & {
14
44
  plugins?: Bun.BunPlugin[];
45
+ static?: StaticExportConfig;
15
46
  };
16
47
  export declare function defineConfig(config: FurinConfig): FurinConfig;
package/dist/config.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  // src/config.ts
3
3
  import { t } from "elysia";
4
- var BUILD_TARGETS = ["bun", "node", "vercel", "cloudflare"];
4
+ var BUILD_TARGETS = ["bun", "node", "vercel", "cloudflare", "static"];
5
5
  var buildTargetSchema = t.Union(BUILD_TARGETS.map((v) => t.Literal(v)));
6
6
  var compileTargetSchema = t.Union([t.Literal("server"), t.Literal("embed")]);
7
7
  var configSchema = t.Object({
@@ -11,6 +11,11 @@ var configSchema = t.Object({
11
11
  targets: t.Optional(t.Array(buildTargetSchema)),
12
12
  bun: t.Optional(t.Object({
13
13
  compile: t.Optional(compileTargetSchema)
14
+ })),
15
+ static: t.Optional(t.Object({
16
+ basePath: t.Optional(t.String()),
17
+ outDir: t.Optional(t.String()),
18
+ onSSR: t.Optional(t.Union([t.Literal("error"), t.Literal("skip")]))
14
19
  }))
15
20
  });
16
21
  function defineConfig(config) {
@@ -26,7 +26,84 @@
26
26
  * Modules in a virtual namespace cannot resolve relative specifiers on their
27
27
  * own (there is no real directory context). We rewrite every `./` / `../`
28
28
  * path to an absolute one before returning the source.
29
+ *
30
+ * ## Bare specifier rewriting
31
+ *
32
+ * When `onLoad` returns `loader: "js"`, Bun resolves bare specifiers
33
+ * (e.g. `@teyik0/furin/link`, `shiki`) from the process CWD — not from the
34
+ * page file's directory. On a cold start the CWD and the file's directory
35
+ * often agree, but after `bun --hot` re-evaluates the server the module
36
+ * cache for project-local packages (like `packages/core/src/link.tsx`) may
37
+ * be cleared and re-loaded with a fresh React instance that diverges from the
38
+ * one already cached by `react-dom/server`. The result is the classic
39
+ * "two React copies" crash: `dispatcher.useState === null`.
40
+ *
41
+ * Fix: after transpilation, resolve every bare specifier from the page
42
+ * file's directory via `Bun.resolveSync` and replace it with the
43
+ * canonical on-disk path. This pins each import to exactly the same
44
+ * file-system location that Bun's native module resolver would choose,
45
+ * guaranteeing a single shared React instance regardless of CWD or
46
+ * hot-reload state.
47
+ *
48
+ * All import/re-export occurrences of each quoted specifier are replaced using
49
+ * a context-aware regex (requires a preceding `from` or `import` keyword) so
50
+ * that modules which both import and re-export from the same package are fully
51
+ * rewritten while string literals in non-import positions are left untouched.
52
+ */
53
+ /**
54
+ * Rewrites bare React/react-dom singleton imports in source text to their
55
+ * absolute resolved paths. Handles explicit static import / re-export forms:
56
+ *
57
+ * import { useState } from "react" → import { useState } from "/abs/…"
58
+ * import type { FC } from "react"
59
+ * export { x } from "react"
60
+ * import "react/jsx-dev-runtime"
61
+ *
62
+ * Does NOT touch dynamic `import("react")` or `require("react")`.
63
+ *
64
+ * @internal exported for testing
29
65
  */
66
+ export declare function rewriteSingletonImports(source: string): string;
67
+ /**
68
+ * `Bun.Transpiler.transformSync` converts JSX syntax to function calls using
69
+ * mangled helper names (e.g. `jsxDEV_7x81h0kn`, `Fragment_8vg9x3sq`) but does
70
+ * NOT emit the corresponding import statement — it expects Bun's full bundler
71
+ * pipeline to inject it later.
72
+ *
73
+ * When we return pre-transpiled JS from `onLoad`, that pipeline step never
74
+ * runs, leaving the helpers as undefined free variables. This function scans
75
+ * the transpiled source, detects which mangled helper names are used, and
76
+ * prepends the correct import statements using absolute on-disk paths from
77
+ * `SINGLETON_PATHS`.
78
+ *
79
+ * Helper → package mapping:
80
+ * jsxDEV_* → `{ jsxDEV as … }` from react/jsx-dev-runtime
81
+ * Fragment_* → `{ Fragment as … }` from react/jsx-dev-runtime
82
+ * jsx_* → `{ jsx as … }` from react/jsx-runtime (prod only)
83
+ * jsxs_* → `{ jsxs as … }` from react/jsx-runtime (prod only)
84
+ *
85
+ * @internal exported for testing
86
+ */
87
+ export declare function injectJsxHelperImports(transpiled: string): string;
30
88
  /** @internal exported for testing */
31
89
  export declare function rewriteRelativeImports(source: string, dir: string): string;
90
+ /**
91
+ * Rewrites all remaining bare specifiers in the transpiled JS to absolute
92
+ * on-disk paths resolved from `dir` (the page file's directory).
93
+ *
94
+ * Uses `Bun.Transpiler.scan()` on the original source to get the authoritative
95
+ * list of *actual* import specifiers (parsing correctly handles template
96
+ * literals, comments, etc.). For each bare specifier that is not already
97
+ * handled by `rewriteSingletonImports`, it calls `Bun.resolveSync` and
98
+ * replaces **every** import/re-export occurrence of the quoted specifier using
99
+ * a context-aware regex with the `g` flag — mirroring the approach used by
100
+ * `rewriteSingletonImports`. This correctly handles modules that both import
101
+ * and re-export from the same package without leaving any bare specifier
102
+ * behind. String literals that happen to contain the same text (inside
103
+ * template literals, JSX props, etc.) are not touched because the regex
104
+ * requires a preceding `from` or `import` keyword.
105
+ *
106
+ * @internal exported for testing
107
+ */
108
+ export declare function rewriteBareImports(source: string, transpiled: string, dir: string): string;
32
109
  export declare function registerDevPagePlugin(): void;