@sigil-dev/grimoire 0.9.4 → 0.9.6
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/index.ts +37 -37
- package/package.json +3 -3
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -36
- package/src/client/enhance.ts +102 -102
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +128 -38
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +52 -47
- package/src/rendering/hydrate.ts +16 -3
- package/src/rendering/index.ts +43 -17
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +55 -55
- package/src/routing/router.ts +135 -135
- package/src/routing/scanner.ts +196 -187
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +250 -255
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +1 -1
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +110 -69
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -389
- package/src/types.ts +286 -286
- package/test/client-router.test.ts +76 -8
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +719 -728
- package/test/routing.test.ts +197 -194
- package/test/scanning.test.ts +314 -254
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -354
- package/test/streaming.test.ts +40 -1
- package/test/transform-routes.test.ts +87 -87
- package/test/typegen.test.ts +697 -697
- package/tsconfig.json +10 -10
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
|
|
4
|
+
const EXTS = [".ts", ".tsx", ".js", ".jsx", ".mts", ".mjs"];
|
|
5
|
+
|
|
6
|
+
function isFile(p: string): boolean {
|
|
7
|
+
try {
|
|
8
|
+
return statSync(p).isFile();
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Add extensions / index files until an on-disk source file is found. */
|
|
15
|
+
function resolveFile(base: string): string | null {
|
|
16
|
+
if (isFile(base)) return base;
|
|
17
|
+
for (const ext of EXTS) if (isFile(base + ext)) return base + ext;
|
|
18
|
+
for (const ext of EXTS) {
|
|
19
|
+
const indexed = join(base, `index${ext}`);
|
|
20
|
+
if (isFile(indexed)) return indexed;
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolve an import specifier to an absolute project source file.
|
|
27
|
+
*
|
|
28
|
+
* Returns `null` for anything outside the project's own sources — bare npm
|
|
29
|
+
* specifiers, `$env/*` virtual modules and `@sigil-dev/*` packages. Those are
|
|
30
|
+
* never part of the dev graph: they can't be hot-swapped, and their browser
|
|
31
|
+
* URLs are stable.
|
|
32
|
+
*
|
|
33
|
+
* `aliases` mirrors `GrimoireConfig.alias` (prefix → project-relative dir).
|
|
34
|
+
*/
|
|
35
|
+
export function resolveProjectImport(
|
|
36
|
+
specifier: string,
|
|
37
|
+
fromFile: string,
|
|
38
|
+
projectRoot: string,
|
|
39
|
+
aliases: Record<string, string> = {},
|
|
40
|
+
): string | null {
|
|
41
|
+
if (specifier.startsWith("$env/")) return null;
|
|
42
|
+
if (specifier.startsWith("@sigil-dev/")) return null;
|
|
43
|
+
|
|
44
|
+
if (specifier.startsWith(".")) {
|
|
45
|
+
return resolveFile(join(dirname(fromFile), specifier));
|
|
46
|
+
}
|
|
47
|
+
if (specifier === "$lib" || specifier.startsWith("$lib/")) {
|
|
48
|
+
return resolveFile(join(projectRoot, "src/lib", specifier.slice(5)));
|
|
49
|
+
}
|
|
50
|
+
for (const [prefix, target] of Object.entries(aliases)) {
|
|
51
|
+
if (specifier !== prefix && !specifier.startsWith(`${prefix}/`)) continue;
|
|
52
|
+
const rest = specifier.slice(prefix.length);
|
|
53
|
+
return resolveFile(join(projectRoot, target, rest));
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Source files the dev graph tracks. */
|
|
59
|
+
export function isSourceFile(filePath: string): boolean {
|
|
60
|
+
return /\.[cm]?[jt]sx?$/.test(filePath);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Files that only ever run on the server — never sent to the browser. */
|
|
64
|
+
export function isServerOnly(filePath: string): boolean {
|
|
65
|
+
return (
|
|
66
|
+
/(^|[\\/])(\+page\.server|\+layout\.server|\+server|hooks\.server)\.[cm]?[jt]s$/.test(
|
|
67
|
+
filePath,
|
|
68
|
+
) || /\.server\.[cm]?[jt]sx?$/.test(filePath)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { mkdirSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { log } from "../logger/instance";
|
|
4
|
-
|
|
5
|
-
const logger = log.scope("runtime-bundle");
|
|
6
|
-
|
|
7
|
-
export async function ensureRuntimeBundle(projectRoot: string): Promise<void> {
|
|
8
|
-
const outDir = join(projectRoot, "public/__grimoire__");
|
|
9
|
-
mkdirSync(outDir, { recursive: true });
|
|
10
|
-
|
|
11
|
-
// runtime
|
|
12
|
-
const runtimeOut = join(outDir, "runtime.js");
|
|
13
|
-
if (!(await Bun.file(runtimeOut).exists())) {
|
|
14
|
-
logger.debug("bundling runtime...");
|
|
15
|
-
const result = await Bun.build({
|
|
16
|
-
entrypoints: [
|
|
17
|
-
join(projectRoot, "node_modules/@sigil-dev/runtime/index.ts"),
|
|
18
|
-
],
|
|
19
|
-
outdir: outDir,
|
|
20
|
-
naming: "runtime.js",
|
|
21
|
-
target: "browser",
|
|
22
|
-
format: "esm",
|
|
23
|
-
minify: false,
|
|
24
|
-
});
|
|
25
|
-
if (!result.success)
|
|
26
|
-
logger.error("[sigil hmr] runtime bundle failed:", result.logs);
|
|
27
|
-
else logger.debug("[sigil hmr] runtime bundled");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// grimoire client
|
|
31
|
-
const grimClientOut = join(outDir, "grimoire-client.js");
|
|
32
|
-
const ref = Bun.file(grimClientOut);
|
|
33
|
-
if (await ref.exists()) await ref.delete();
|
|
34
|
-
await Bun.write(
|
|
35
|
-
grimClientOut,
|
|
36
|
-
`
|
|
37
|
-
// Grimoire client shim for HMR dev mode
|
|
38
|
-
export function Head({ children }) {
|
|
39
|
-
if (children instanceof Node) {
|
|
40
|
-
document.head.appendChild(children);
|
|
41
|
-
}
|
|
42
|
-
return document.createComment("head");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const navigate = (...args) => window.__grimoire_navigate__?.(...args);
|
|
46
|
-
export const beforeNavigate = (cb) => window.__grimoire_beforeNavigate__?.(cb);
|
|
47
|
-
export const onNavigate = (cb) => window.__grimoire_onNavigate__?.(cb);
|
|
48
|
-
export const afterNavigate = (cb) => window.__grimoire_afterNavigate__?.(cb);
|
|
49
|
-
`.trim(),
|
|
50
|
-
);
|
|
51
|
-
logger.info("grimoire client shim written");
|
|
52
|
-
}
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { log } from "../logger/instance";
|
|
4
|
+
|
|
5
|
+
const logger = log.scope("runtime-bundle");
|
|
6
|
+
|
|
7
|
+
export async function ensureRuntimeBundle(projectRoot: string): Promise<void> {
|
|
8
|
+
const outDir = join(projectRoot, "public/__grimoire__");
|
|
9
|
+
mkdirSync(outDir, { recursive: true });
|
|
10
|
+
|
|
11
|
+
// runtime
|
|
12
|
+
const runtimeOut = join(outDir, "runtime.js");
|
|
13
|
+
if (!(await Bun.file(runtimeOut).exists())) {
|
|
14
|
+
logger.debug("bundling runtime...");
|
|
15
|
+
const result = await Bun.build({
|
|
16
|
+
entrypoints: [
|
|
17
|
+
join(projectRoot, "node_modules/@sigil-dev/runtime/index.ts"),
|
|
18
|
+
],
|
|
19
|
+
outdir: outDir,
|
|
20
|
+
naming: "runtime.js",
|
|
21
|
+
target: "browser",
|
|
22
|
+
format: "esm",
|
|
23
|
+
minify: false,
|
|
24
|
+
});
|
|
25
|
+
if (!result.success)
|
|
26
|
+
logger.error("[sigil hmr] runtime bundle failed:", result.logs);
|
|
27
|
+
else logger.debug("[sigil hmr] runtime bundled");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// grimoire client
|
|
31
|
+
const grimClientOut = join(outDir, "grimoire-client.js");
|
|
32
|
+
const ref = Bun.file(grimClientOut);
|
|
33
|
+
if (await ref.exists()) await ref.delete();
|
|
34
|
+
await Bun.write(
|
|
35
|
+
grimClientOut,
|
|
36
|
+
`
|
|
37
|
+
// Grimoire client shim for HMR dev mode
|
|
38
|
+
export function Head({ children }) {
|
|
39
|
+
if (children instanceof Node) {
|
|
40
|
+
document.head.appendChild(children);
|
|
41
|
+
}
|
|
42
|
+
return document.createComment("head");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const navigate = (...args) => window.__grimoire_navigate__?.(...args);
|
|
46
|
+
export const beforeNavigate = (cb) => window.__grimoire_beforeNavigate__?.(cb);
|
|
47
|
+
export const onNavigate = (cb) => window.__grimoire_onNavigate__?.(cb);
|
|
48
|
+
export const afterNavigate = (cb) => window.__grimoire_afterNavigate__?.(cb);
|
|
49
|
+
`.trim(),
|
|
50
|
+
);
|
|
51
|
+
logger.info("grimoire client shim written");
|
|
52
|
+
}
|
package/src/dev/scan.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { DevGraph } from "./graph";
|
|
2
|
+
import { normalizePath } from "./paths";
|
|
3
|
+
import { isSourceFile, resolveProjectImport } from "./resolve";
|
|
4
|
+
import { safeRead } from "./watcher";
|
|
5
|
+
|
|
6
|
+
export interface ScanContext {
|
|
7
|
+
graph: DevGraph;
|
|
8
|
+
projectRoot: string;
|
|
9
|
+
/** GrimoireConfig.alias — prefix → project-relative directory. */
|
|
10
|
+
aliases: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const tsxTranspiler = new Bun.Transpiler({ loader: "tsx" });
|
|
14
|
+
const tsTranspiler = new Bun.Transpiler({ loader: "ts" });
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Rescan one module's imports and replace its graph edges. Returns the
|
|
18
|
+
* resolved project files it imports (externals are not tracked).
|
|
19
|
+
*/
|
|
20
|
+
export async function scanModule(
|
|
21
|
+
filePath: string,
|
|
22
|
+
ctx: ScanContext,
|
|
23
|
+
): Promise<string[]> {
|
|
24
|
+
const source = await safeRead(filePath);
|
|
25
|
+
if (source === null) return [];
|
|
26
|
+
|
|
27
|
+
// .ts must not be parsed as .tsx: `<T>(x) => x` is a cast there, JSX here
|
|
28
|
+
const transpiler = /\.[jt]sx$/.test(filePath) ? tsxTranspiler : tsTranspiler;
|
|
29
|
+
let specifiers: string[];
|
|
30
|
+
try {
|
|
31
|
+
specifiers = transpiler.scanImports(source).map((i) => i.path);
|
|
32
|
+
} catch {
|
|
33
|
+
// syntax error mid-edit — keep the previous edges
|
|
34
|
+
return [...(ctx.graph.get(filePath)?.imports ?? [])];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const targets: string[] = [];
|
|
38
|
+
for (const specifier of specifiers) {
|
|
39
|
+
const resolved = resolveProjectImport(
|
|
40
|
+
specifier,
|
|
41
|
+
filePath,
|
|
42
|
+
ctx.projectRoot,
|
|
43
|
+
ctx.aliases,
|
|
44
|
+
);
|
|
45
|
+
if (resolved && isSourceFile(resolved)) targets.push(resolved);
|
|
46
|
+
}
|
|
47
|
+
ctx.graph.setImports(filePath, targets);
|
|
48
|
+
return targets;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Walk the import graph from `entries` (route/layout/server files), recording
|
|
53
|
+
* every project module reachable from them. Without the transitive walk a
|
|
54
|
+
* component two hops from a page has no importer chain, so an edit to it can't
|
|
55
|
+
* be traced back to the route that renders it.
|
|
56
|
+
*/
|
|
57
|
+
export async function crawlFrom(
|
|
58
|
+
entries: string[],
|
|
59
|
+
ctx: ScanContext,
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
const seen = new Set<string>();
|
|
62
|
+
let frontier = entries.filter(isSourceFile);
|
|
63
|
+
while (frontier.length) {
|
|
64
|
+
const next: string[] = [];
|
|
65
|
+
const scanned = await Promise.all(
|
|
66
|
+
frontier
|
|
67
|
+
.filter((file) => {
|
|
68
|
+
const key = normalizePath(file);
|
|
69
|
+
if (seen.has(key)) return false;
|
|
70
|
+
seen.add(key);
|
|
71
|
+
return true;
|
|
72
|
+
})
|
|
73
|
+
.map((file) => scanModule(file, ctx)),
|
|
74
|
+
);
|
|
75
|
+
for (const targets of scanned) next.push(...targets);
|
|
76
|
+
frontier = next;
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/dev/watcher.ts
CHANGED
|
@@ -1,47 +1,69 @@
|
|
|
1
|
-
import { watch } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { normalizePath } from "./paths";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
1
|
+
import { statSync, watch } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { normalizePath } from "./paths";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Editor scratch files. Atomic saves (VSCode, WebStorm, vim, sed -i) write a
|
|
7
|
+
* sibling temp file and rename it over the target, so the watcher sees names
|
|
8
|
+
* that are not the file the developer edited.
|
|
9
|
+
*/
|
|
10
|
+
const IGNORED_NAME = /^(\.|4913$)|~$|\.(tmp|swp|swx|swo|bak|orig|rej)$/;
|
|
11
|
+
|
|
12
|
+
export type ChangeHandler = (filePath: string) => Promise<void>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Watch every root for source changes, coalescing bursts (editors write a
|
|
16
|
+
* file two or three times per save) into one sequential flush.
|
|
17
|
+
*
|
|
18
|
+
* Roots are plural because components live outside `src/routes`: watching only
|
|
19
|
+
* the routes directory means an edit to `src/lib/Card.tsx` never fires.
|
|
20
|
+
*/
|
|
21
|
+
export function startWatcher(
|
|
22
|
+
roots: string[],
|
|
23
|
+
onChange: ChangeHandler,
|
|
24
|
+
): () => void {
|
|
25
|
+
const pending = new Set<string>();
|
|
26
|
+
let debounce: Timer | undefined;
|
|
27
|
+
let flushing: Promise<void> = Promise.resolve();
|
|
28
|
+
|
|
29
|
+
const flush = () => {
|
|
30
|
+
const files = [...pending];
|
|
31
|
+
pending.clear();
|
|
32
|
+
// serialize flushes: two saves in flight would race on the graph
|
|
33
|
+
flushing = flushing.then(async () => {
|
|
34
|
+
for (const f of files) await onChange(f);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const watchers = roots.map((root) =>
|
|
39
|
+
watch(root, { recursive: true }, (_evt, filename) => {
|
|
40
|
+
if (!filename) return;
|
|
41
|
+
const path = join(root, filename.toString());
|
|
42
|
+
if (IGNORED_NAME.test(basename(path))) return;
|
|
43
|
+
// directory events carry no module to recompile; deletions do fall
|
|
44
|
+
// through, so a removed file still rescans and reloads
|
|
45
|
+
try {
|
|
46
|
+
if (statSync(path).isDirectory()) return;
|
|
47
|
+
} catch {}
|
|
48
|
+
pending.add(normalizePath(path));
|
|
49
|
+
clearTimeout(debounce);
|
|
50
|
+
debounce = setTimeout(flush, 60);
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
for (const w of watchers) w.close();
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Read file with atomic-write retry — VSCode on Windows writes temp-then-rename */
|
|
60
|
+
export async function safeRead(filePath: string): Promise<string | null> {
|
|
61
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
62
|
+
const text = await Bun.file(filePath)
|
|
63
|
+
.text()
|
|
64
|
+
.catch(() => null);
|
|
65
|
+
if (text && text.length > 0) return text;
|
|
66
|
+
await Bun.sleep(30);
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// all env vars — server-only, never bundle for client
|
|
2
|
-
export const env: Record<string, string> = new Proxy(
|
|
3
|
-
{} as Record<string, string>,
|
|
4
|
-
{
|
|
5
|
-
get(_, key: string) {
|
|
6
|
-
return process.env[key as string] ?? "";
|
|
7
|
-
},
|
|
8
|
-
ownKeys() {
|
|
9
|
-
return Object.keys(process.env);
|
|
10
|
-
},
|
|
11
|
-
getOwnPropertyDescriptor(_, key: string) {
|
|
12
|
-
return {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
value: process.env[key] ?? "",
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
);
|
|
1
|
+
// all env vars — server-only, never bundle for client
|
|
2
|
+
export const env: Record<string, string> = new Proxy(
|
|
3
|
+
{} as Record<string, string>,
|
|
4
|
+
{
|
|
5
|
+
get(_, key: string) {
|
|
6
|
+
return process.env[key as string] ?? "";
|
|
7
|
+
},
|
|
8
|
+
ownKeys() {
|
|
9
|
+
return Object.keys(process.env);
|
|
10
|
+
},
|
|
11
|
+
getOwnPropertyDescriptor(_, key: string) {
|
|
12
|
+
return {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
value: process.env[key] ?? "",
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
);
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
// PUBLIC_ prefixed env vars — safe to expose to client
|
|
2
|
-
export const env: Record<string, string> = new Proxy(
|
|
3
|
-
{} as Record<string, string>,
|
|
4
|
-
{
|
|
5
|
-
get(_, key: string) {
|
|
6
|
-
const val = process.env[key];
|
|
7
|
-
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
8
|
-
return val ?? "";
|
|
9
|
-
},
|
|
10
|
-
ownKeys() {
|
|
11
|
-
return Object.keys(process.env).filter((k) => k.startsWith("PUBLIC_"));
|
|
12
|
-
},
|
|
13
|
-
getOwnPropertyDescriptor(_, key: string) {
|
|
14
|
-
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
15
|
-
return {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
value: process.env[key] ?? "",
|
|
19
|
-
};
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
);
|
|
1
|
+
// PUBLIC_ prefixed env vars — safe to expose to client
|
|
2
|
+
export const env: Record<string, string> = new Proxy(
|
|
3
|
+
{} as Record<string, string>,
|
|
4
|
+
{
|
|
5
|
+
get(_, key: string) {
|
|
6
|
+
const val = process.env[key];
|
|
7
|
+
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
8
|
+
return val ?? "";
|
|
9
|
+
},
|
|
10
|
+
ownKeys() {
|
|
11
|
+
return Object.keys(process.env).filter((k) => k.startsWith("PUBLIC_"));
|
|
12
|
+
},
|
|
13
|
+
getOwnPropertyDescriptor(_, key: string) {
|
|
14
|
+
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
15
|
+
return {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
value: process.env[key] ?? "",
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
);
|
package/src/env/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export const publicPrefix = "PUBLIC_";
|
|
2
|
-
|
|
3
|
-
export function filterPublic(
|
|
4
|
-
env: Record<string, string | undefined>,
|
|
5
|
-
): Record<string, string> {
|
|
6
|
-
const result: Record<string, string> = {};
|
|
7
|
-
for (const [key, value] of Object.entries(env)) {
|
|
8
|
-
if (key.startsWith(publicPrefix) && value !== undefined) {
|
|
9
|
-
result[key] = value;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return result;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function filterPrivate(
|
|
16
|
-
env: Record<string, string | undefined>,
|
|
17
|
-
): Record<string, string> {
|
|
18
|
-
const result: Record<string, string> = {};
|
|
19
|
-
for (const [key, value] of Object.entries(env)) {
|
|
20
|
-
if (!key.startsWith(publicPrefix) && value !== undefined) {
|
|
21
|
-
result[key] = value;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
1
|
+
export const publicPrefix = "PUBLIC_";
|
|
2
|
+
|
|
3
|
+
export function filterPublic(
|
|
4
|
+
env: Record<string, string | undefined>,
|
|
5
|
+
): Record<string, string> {
|
|
6
|
+
const result: Record<string, string> = {};
|
|
7
|
+
for (const [key, value] of Object.entries(env)) {
|
|
8
|
+
if (key.startsWith(publicPrefix) && value !== undefined) {
|
|
9
|
+
result[key] = value;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function filterPrivate(
|
|
16
|
+
env: Record<string, string | undefined>,
|
|
17
|
+
): Record<string, string> {
|
|
18
|
+
const result: Record<string, string> = {};
|
|
19
|
+
for (const [key, value] of Object.entries(env)) {
|
|
20
|
+
if (!key.startsWith(publicPrefix) && value !== undefined) {
|
|
21
|
+
result[key] = value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
package/src/env/plugin.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
|
|
3
|
-
export const envPlugin = () => ({
|
|
4
|
-
name: "grimoire-env",
|
|
5
|
-
setup(build: any) {
|
|
6
|
-
build.onResolve({ filter: /^\$env\/static\/public$/ }, () => ({
|
|
7
|
-
path: join(import.meta.dir, "public.ts"),
|
|
8
|
-
}));
|
|
9
|
-
build.onResolve({ filter: /^\$env\/static\/private$/ }, () => ({
|
|
10
|
-
path: join(import.meta.dir, "private.ts"),
|
|
11
|
-
}));
|
|
12
|
-
build.onResolve({ filter: /^\$env\/dynamic\/public$/ }, () => ({
|
|
13
|
-
path: join(import.meta.dir, "dynamic-public.ts"),
|
|
14
|
-
}));
|
|
15
|
-
build.onResolve({ filter: /^\$env\/dynamic\/private$/ }, () => ({
|
|
16
|
-
path: join(import.meta.dir, "dynamic-private.ts"),
|
|
17
|
-
}));
|
|
18
|
-
},
|
|
19
|
-
});
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
|
|
3
|
+
export const envPlugin = () => ({
|
|
4
|
+
name: "grimoire-env",
|
|
5
|
+
setup(build: any) {
|
|
6
|
+
build.onResolve({ filter: /^\$env\/static\/public$/ }, () => ({
|
|
7
|
+
path: join(import.meta.dir, "public.ts"),
|
|
8
|
+
}));
|
|
9
|
+
build.onResolve({ filter: /^\$env\/static\/private$/ }, () => ({
|
|
10
|
+
path: join(import.meta.dir, "private.ts"),
|
|
11
|
+
}));
|
|
12
|
+
build.onResolve({ filter: /^\$env\/dynamic\/public$/ }, () => ({
|
|
13
|
+
path: join(import.meta.dir, "dynamic-public.ts"),
|
|
14
|
+
}));
|
|
15
|
+
build.onResolve({ filter: /^\$env\/dynamic\/private$/ }, () => ({
|
|
16
|
+
path: join(import.meta.dir, "dynamic-private.ts"),
|
|
17
|
+
}));
|
|
18
|
+
},
|
|
19
|
+
});
|
package/src/env/private.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { filterPrivate } from "./index";
|
|
2
|
-
|
|
3
|
-
// Runtime values — reads from live process.env on each access.
|
|
4
|
-
// Never sent to the client; server-only.
|
|
5
|
-
export const env: Record<string, string> = filterPrivate(process.env);
|
|
1
|
+
import { filterPrivate } from "./index";
|
|
2
|
+
|
|
3
|
+
// Runtime values — reads from live process.env on each access.
|
|
4
|
+
// Never sent to the client; server-only.
|
|
5
|
+
export const env: Record<string, string> = filterPrivate(process.env);
|
package/src/env/public.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { filterPublic } from "./index";
|
|
2
|
-
|
|
3
|
-
// Inlined at build time — values captured from process.env during the build.
|
|
4
|
-
// For runtime reads (when not compiled), fall back to live process.env.
|
|
5
|
-
export const env = filterPublic(
|
|
6
|
-
typeof process !== "undefined" ? process.env : {},
|
|
7
|
-
);
|
|
1
|
+
import { filterPublic } from "./index";
|
|
2
|
+
|
|
3
|
+
// Inlined at build time — values captured from process.env during the build.
|
|
4
|
+
// For runtime reads (when not compiled), fall back to live process.env.
|
|
5
|
+
export const env = filterPublic(
|
|
6
|
+
typeof process !== "undefined" ? process.env : {},
|
|
7
|
+
);
|