@kiln-js/core 0.0.0-stage → 0.1.0
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/env.d.ts +14 -0
- package/dist/head.d.ts +12 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +126 -0
- package/dist/match.d.ts +8 -0
- package/dist/router.d.ts +11 -0
- package/dist/scan.d.ts +25 -0
- package/dist/scan.js +107 -0
- package/package.json +30 -5
- package/README.md +0 -3
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type EnvType = "string" | "number" | "boolean";
|
|
2
|
+
type EnvValue<T extends EnvType> = T extends "number" ? number : T extends "boolean" ? boolean : string;
|
|
3
|
+
export interface EnvField<T extends EnvType = EnvType> {
|
|
4
|
+
type: T;
|
|
5
|
+
default?: EnvValue<T>;
|
|
6
|
+
}
|
|
7
|
+
type Schema = Record<string, EnvType | EnvField>;
|
|
8
|
+
export type EnvOf<S extends Schema> = {
|
|
9
|
+
[K in keyof S]: S[K] extends EnvType ? EnvValue<S[K]> : S[K] extends {
|
|
10
|
+
type: infer T extends EnvType;
|
|
11
|
+
} ? EnvValue<T> : never;
|
|
12
|
+
};
|
|
13
|
+
export declare function defineEnv<S extends Schema>(schema: S, source?: Record<string, string | undefined>): EnvOf<S>;
|
|
14
|
+
export {};
|
package/dist/head.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface HeadSink {
|
|
3
|
+
title?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare const HeadContext: import("react").Context<HeadSink | null>;
|
|
6
|
+
export declare function HeadProvider({ value, children }: {
|
|
7
|
+
value: HeadSink;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export declare function Title({ children }: {
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}): null;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { matchRoute, compose } from "./match";
|
|
2
|
+
export { RouteProvider, usePathname, useParams } from "./router";
|
|
3
|
+
export type { RouteState } from "./router";
|
|
4
|
+
export { HeadProvider, Title } from "./head";
|
|
5
|
+
export type { HeadSink } from "./head";
|
|
6
|
+
export { defineEnv } from "./env";
|
|
7
|
+
export type { EnvField, EnvType, EnvOf } from "./env";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/match.tsx
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
function matchRoute(routes, pathname) {
|
|
5
|
+
const parts = pathname.split("/").filter(Boolean);
|
|
6
|
+
for (const route of routes) {
|
|
7
|
+
const segs = route.pattern.split("/").filter(Boolean);
|
|
8
|
+
const params = {};
|
|
9
|
+
let ok = true;
|
|
10
|
+
let i = 0;
|
|
11
|
+
for (const seg of segs) {
|
|
12
|
+
if (seg.startsWith("*")) {
|
|
13
|
+
params[seg.slice(1)] = parts.slice(i).map(decodeURIComponent).join("/");
|
|
14
|
+
i = parts.length;
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
if (i >= parts.length) {
|
|
18
|
+
ok = false;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
if (seg.startsWith(":"))
|
|
22
|
+
params[seg.slice(1)] = decodeURIComponent(parts[i]);
|
|
23
|
+
else if (seg !== parts[i]) {
|
|
24
|
+
ok = false;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
if (ok && i === parts.length)
|
|
30
|
+
return { route, params };
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function compose(page, layouts) {
|
|
35
|
+
let tree = page;
|
|
36
|
+
for (const Layout of [...layouts].reverse())
|
|
37
|
+
tree = /* @__PURE__ */ jsx(Layout, {
|
|
38
|
+
children: tree
|
|
39
|
+
});
|
|
40
|
+
return tree;
|
|
41
|
+
}
|
|
42
|
+
// src/router.tsx
|
|
43
|
+
import { createContext, useContext } from "react";
|
|
44
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
45
|
+
var RouteContext = createContext({ pathname: "/", params: {} });
|
|
46
|
+
function RouteProvider({ value, children }) {
|
|
47
|
+
return /* @__PURE__ */ jsx2(RouteContext.Provider, {
|
|
48
|
+
value,
|
|
49
|
+
children
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
var usePathname = () => useContext(RouteContext).pathname;
|
|
53
|
+
var useParams = () => useContext(RouteContext).params;
|
|
54
|
+
// src/head.tsx
|
|
55
|
+
import { createContext as createContext2, useContext as useContext2, useEffect, useLayoutEffect } from "react";
|
|
56
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
57
|
+
var HeadContext = createContext2(null);
|
|
58
|
+
function HeadProvider({ value, children }) {
|
|
59
|
+
return /* @__PURE__ */ jsx3(HeadContext.Provider, {
|
|
60
|
+
value,
|
|
61
|
+
children
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function textOf(node) {
|
|
65
|
+
if (node == null || typeof node === "boolean")
|
|
66
|
+
return "";
|
|
67
|
+
if (typeof node === "string" || typeof node === "number")
|
|
68
|
+
return String(node);
|
|
69
|
+
if (Array.isArray(node))
|
|
70
|
+
return node.map(textOf).join("");
|
|
71
|
+
return "";
|
|
72
|
+
}
|
|
73
|
+
var useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
74
|
+
function Title({ children }) {
|
|
75
|
+
const sink = useContext2(HeadContext);
|
|
76
|
+
const title = textOf(children);
|
|
77
|
+
if (sink)
|
|
78
|
+
sink.title = title;
|
|
79
|
+
useIsoLayoutEffect(() => {
|
|
80
|
+
document.title = title;
|
|
81
|
+
}, [title]);
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
// src/env.ts
|
|
85
|
+
function defineEnv(schema, source = typeof process !== "undefined" ? process.env : {}) {
|
|
86
|
+
const out = {};
|
|
87
|
+
const errors = [];
|
|
88
|
+
for (const [key, def] of Object.entries(schema)) {
|
|
89
|
+
const field = typeof def === "string" ? { type: def } : def;
|
|
90
|
+
const raw = source[key];
|
|
91
|
+
if (raw === undefined || raw === "") {
|
|
92
|
+
if (field.default !== undefined)
|
|
93
|
+
out[key] = field.default;
|
|
94
|
+
else
|
|
95
|
+
errors.push(`${key} is required`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (field.type === "number") {
|
|
99
|
+
const n = Number(raw);
|
|
100
|
+
if (Number.isNaN(n))
|
|
101
|
+
errors.push(`${key} must be a number, got "${raw}"`);
|
|
102
|
+
else
|
|
103
|
+
out[key] = n;
|
|
104
|
+
} else if (field.type === "boolean") {
|
|
105
|
+
out[key] = ["1", "true", "yes", "on"].includes(raw.toLowerCase());
|
|
106
|
+
} else {
|
|
107
|
+
out[key] = raw;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (errors.length) {
|
|
111
|
+
throw new Error(`Invalid environment:
|
|
112
|
+
- ${errors.join(`
|
|
113
|
+
- `)}`);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
export {
|
|
118
|
+
HeadProvider,
|
|
119
|
+
RouteProvider,
|
|
120
|
+
Title,
|
|
121
|
+
compose,
|
|
122
|
+
defineEnv,
|
|
123
|
+
matchRoute,
|
|
124
|
+
useParams,
|
|
125
|
+
usePathname
|
|
126
|
+
};
|
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ComponentType, ReactElement } from "react";
|
|
2
|
+
export declare function matchRoute<R extends {
|
|
3
|
+
pattern: string;
|
|
4
|
+
}>(routes: R[], pathname: string): {
|
|
5
|
+
route: R;
|
|
6
|
+
params: Record<string, string>;
|
|
7
|
+
} | null;
|
|
8
|
+
export declare function compose(page: ReactElement, layouts: ComponentType<any>[]): ReactElement<any, string | import("react").JSXElementConstructor<any>>;
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface RouteState {
|
|
3
|
+
pathname: string;
|
|
4
|
+
params: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
export declare function RouteProvider({ value, children }: {
|
|
7
|
+
value: RouteState;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export declare const usePathname: () => string;
|
|
11
|
+
export declare const useParams: () => Record<string, string>;
|
package/dist/scan.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type RenderMode = "universal" | "server" | "client";
|
|
2
|
+
export type Segment = {
|
|
3
|
+
type: "static";
|
|
4
|
+
value: string;
|
|
5
|
+
} | {
|
|
6
|
+
type: "param";
|
|
7
|
+
name: string;
|
|
8
|
+
} | {
|
|
9
|
+
type: "splat";
|
|
10
|
+
name: string;
|
|
11
|
+
};
|
|
12
|
+
export interface RouteRecord {
|
|
13
|
+
file: string;
|
|
14
|
+
pattern: string;
|
|
15
|
+
mode: RenderMode;
|
|
16
|
+
segments: Segment[];
|
|
17
|
+
layouts: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface ApiRouteRecord {
|
|
20
|
+
file: string;
|
|
21
|
+
pattern: string;
|
|
22
|
+
segments: Segment[];
|
|
23
|
+
}
|
|
24
|
+
export declare function scanRoutes(pagesDir: string): RouteRecord[];
|
|
25
|
+
export declare function scanApi(apiDir: string): ApiRouteRecord[];
|
package/dist/scan.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/scan.ts
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
var EXT_RE = /\.(tsx|jsx|ts|js)$/;
|
|
6
|
+
var MODE_RE = /\.(server|client)$/;
|
|
7
|
+
var GROUP_RE = /^\(.+\)$/;
|
|
8
|
+
function walk(dir) {
|
|
9
|
+
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
10
|
+
const full = path.join(dir, entry.name);
|
|
11
|
+
return entry.isDirectory() ? walk(full) : [full];
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function toSegment(part) {
|
|
15
|
+
const splat = part.match(/^\[\.\.\.(\w+)\]$/);
|
|
16
|
+
if (splat)
|
|
17
|
+
return { type: "splat", name: splat[1] };
|
|
18
|
+
const param = part.match(/^\[(\w+)\]$/);
|
|
19
|
+
if (param)
|
|
20
|
+
return { type: "param", name: param[1] };
|
|
21
|
+
return { type: "static", value: part };
|
|
22
|
+
}
|
|
23
|
+
function urlParts(name) {
|
|
24
|
+
return name.split("/").filter((p) => p !== "index" && !GROUP_RE.test(p));
|
|
25
|
+
}
|
|
26
|
+
function toPattern(segments) {
|
|
27
|
+
return "/" + segments.map((s) => s.type === "static" ? s.value : s.type === "param" ? `:${s.name}` : `*${s.name}`).join("/");
|
|
28
|
+
}
|
|
29
|
+
function findLayouts(pagesDir, relFile) {
|
|
30
|
+
const dirs = relFile.split("/").slice(0, -1);
|
|
31
|
+
const found = [];
|
|
32
|
+
for (let i = 0;i <= dirs.length; i++) {
|
|
33
|
+
const dir = path.join(pagesDir, ...dirs.slice(0, i));
|
|
34
|
+
for (const ext of ["tsx", "jsx", "ts", "js"]) {
|
|
35
|
+
const file = path.join(dir, `_layout.${ext}`);
|
|
36
|
+
if (fs.existsSync(file)) {
|
|
37
|
+
found.push(file);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
var rank = (s) => s.type === "static" ? 0 : s.type === "param" ? 1 : 2;
|
|
45
|
+
function compareRoutes(a, b) {
|
|
46
|
+
const n = Math.max(a.segments.length, b.segments.length);
|
|
47
|
+
for (let i = 0;i < n; i++) {
|
|
48
|
+
const sa = a.segments[i];
|
|
49
|
+
const sb = b.segments[i];
|
|
50
|
+
if (!sa)
|
|
51
|
+
return 1;
|
|
52
|
+
if (!sb)
|
|
53
|
+
return -1;
|
|
54
|
+
const d = rank(sa) - rank(sb);
|
|
55
|
+
if (d !== 0)
|
|
56
|
+
return d;
|
|
57
|
+
}
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
function scanRoutes(pagesDir) {
|
|
61
|
+
const byPattern = new Map;
|
|
62
|
+
const routes = [];
|
|
63
|
+
for (const file of walk(pagesDir)) {
|
|
64
|
+
if (!EXT_RE.test(file))
|
|
65
|
+
continue;
|
|
66
|
+
const rel = path.relative(pagesDir, file).replace(/\\/g, "/");
|
|
67
|
+
const name = rel.replace(EXT_RE, "");
|
|
68
|
+
if (path.basename(name).startsWith("_"))
|
|
69
|
+
continue;
|
|
70
|
+
const mode = name.match(MODE_RE)?.[1] ?? "universal";
|
|
71
|
+
const segments = urlParts(name.replace(MODE_RE, "")).map(toSegment);
|
|
72
|
+
const pattern = toPattern(segments);
|
|
73
|
+
const existing = byPattern.get(pattern);
|
|
74
|
+
if (existing) {
|
|
75
|
+
throw new Error(`Route conflict: ${rel} and ${existing} both map to ${pattern}`);
|
|
76
|
+
}
|
|
77
|
+
byPattern.set(pattern, rel);
|
|
78
|
+
routes.push({ file, pattern, mode, segments, layouts: findLayouts(pagesDir, rel) });
|
|
79
|
+
}
|
|
80
|
+
return routes.sort(compareRoutes);
|
|
81
|
+
}
|
|
82
|
+
function scanApi(apiDir) {
|
|
83
|
+
if (!fs.existsSync(apiDir))
|
|
84
|
+
return [];
|
|
85
|
+
const seen = new Map;
|
|
86
|
+
const routes = [];
|
|
87
|
+
for (const file of walk(apiDir)) {
|
|
88
|
+
if (!EXT_RE.test(file))
|
|
89
|
+
continue;
|
|
90
|
+
const rel = path.relative(apiDir, file).replace(/\\/g, "/");
|
|
91
|
+
const name = rel.replace(EXT_RE, "");
|
|
92
|
+
if (path.basename(name).startsWith("_"))
|
|
93
|
+
continue;
|
|
94
|
+
const segments = [{ type: "static", value: "api" }, ...urlParts(name).map(toSegment)];
|
|
95
|
+
const pattern = toPattern(segments);
|
|
96
|
+
const prev = seen.get(pattern);
|
|
97
|
+
if (prev)
|
|
98
|
+
throw new Error(`API route conflict: ${rel} and ${prev} both map to ${pattern}`);
|
|
99
|
+
seen.set(pattern, rel);
|
|
100
|
+
routes.push({ file, pattern, segments });
|
|
101
|
+
}
|
|
102
|
+
return routes.sort(compareRoutes);
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
scanApi,
|
|
106
|
+
scanRoutes
|
|
107
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"
|
|
5
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@kiln-js/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"react": "\u003e=18"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/node": "^26.6.11",
|
|
13
|
+
"@types/react": "^18.3.0",
|
|
14
|
+
"react": "^18.3.1"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./scan": {
|
|
22
|
+
"types": "./dist/scan.d.ts",
|
|
23
|
+
"import": "./dist/scan.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "bun build ./src/index.ts ./src/scan.ts --outdir ./dist --target bun --external react --external react-dom \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
30
|
+
}
|
|
6
31
|
}
|
package/README.md
DELETED