@sigil-dev/grimoire 0.8.6 → 0.9.1
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/package.json +4 -6
- package/server.ts +5 -1
- package/src/client/deps.ts +13 -0
- package/src/client/head.ts +1 -0
- package/src/client/router.ts +308 -228
- package/src/dev/compile-module.ts +1 -1
- package/src/dev/hmr-client.ts +30 -3
- package/src/dev/hmr-server.ts +187 -187
- package/src/dev/watcher.ts +4 -2
- package/src/env/dynamic-private.ts +19 -0
- package/src/env/dynamic-public.ts +22 -0
- package/src/env/plugin.ts +6 -0
- package/src/env/typegen.ts +10 -0
- package/src/logger/index.ts +0 -2
- package/src/rendering/index.ts +137 -63
- package/src/server/build.ts +199 -219
- package/src/server/coordinator.ts +316 -232
- package/src/server/index.ts +623 -622
- package/src/server/interactive.ts +56 -54
- package/src/server/plugins.ts +2 -1
- package/src/types.ts +282 -272
- package/test/client-router.test.ts +38 -0
- package/test/env.test.ts +90 -0
|
@@ -113,7 +113,7 @@ export async function compileForBrowser(
|
|
|
113
113
|
configFile: false,
|
|
114
114
|
babelrc: false,
|
|
115
115
|
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
116
|
-
plugins: [[sigilPlugin, { mode: "dom", hash }]],
|
|
116
|
+
plugins: [[sigilPlugin, { mode: "dom", hash, dev: true }]],
|
|
117
117
|
filename: filePath,
|
|
118
118
|
});
|
|
119
119
|
// strip TypeScript types
|
package/src/dev/hmr-client.ts
CHANGED
|
@@ -6,20 +6,20 @@ export const HMR_CLIENT_SOURCE = `
|
|
|
6
6
|
|
|
7
7
|
async function applySwap(p) {
|
|
8
8
|
try {
|
|
9
|
+
captureSignals(); // ← capture before
|
|
9
10
|
const url = '/__grimoire__/m/' + p.modUrl + '?v=' + p.version;
|
|
10
11
|
const mod = await import(url);
|
|
12
|
+
clearCapture(); // ← clear after
|
|
11
13
|
|
|
12
|
-
// update route map — accessed via grimoire's initRouter export
|
|
13
14
|
if (window.__grimoire_routes__) {
|
|
14
15
|
window.__grimoire_routes__[p.pattern] = mod.default;
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
-
// re-render if currently on this pattern
|
|
18
17
|
const currentPattern = window.__grimoire_current_pattern__;
|
|
19
18
|
if (currentPattern === p.pattern && window.__grimoire_rerender__) {
|
|
20
19
|
await window.__grimoire_rerender__();
|
|
21
20
|
}
|
|
22
21
|
} catch (e) {
|
|
22
|
+
clearCapture();
|
|
23
23
|
console.error('[sigil hmr] swap failed, reloading:', e);
|
|
24
24
|
location.reload();
|
|
25
25
|
}
|
|
@@ -43,6 +43,33 @@ export const HMR_CLIENT_SOURCE = `
|
|
|
43
43
|
el.textContent = text;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
const __hmrRegistry__ = new Map(); // key -> signal getter fn
|
|
47
|
+
|
|
48
|
+
window.__hmrAdopt__ = function(key, initial) {
|
|
49
|
+
if (!window.__hmrCapture__) return initial;
|
|
50
|
+
if (!window.__hmrCapture__.has(key)) return initial;
|
|
51
|
+
const stored = window.__hmrCapture__.get(key);
|
|
52
|
+
// shape guard — only adopt if same type and array-ness
|
|
53
|
+
if (typeof stored !== typeof initial) return initial;
|
|
54
|
+
if (Array.isArray(stored) !== Array.isArray(initial)) return initial;
|
|
55
|
+
return stored;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
window.__hmrRegister__ = function(key, signal) {
|
|
59
|
+
__hmrRegistry__.set(key, signal);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
function captureSignals() {
|
|
63
|
+
window.__hmrCapture__ = new Map();
|
|
64
|
+
for (const [key, signal] of __hmrRegistry__) {
|
|
65
|
+
try { window.__hmrCapture__.set(key, signal()); } catch {}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function clearCapture() {
|
|
70
|
+
window.__hmrCapture__ = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
46
73
|
function connect() {
|
|
47
74
|
const ws = new WebSocket(
|
|
48
75
|
(location.protocol === 'https:' ? 'wss' : 'ws') +
|
package/src/dev/hmr-server.ts
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
3
|
-
import type { ServerWebSocket } from "bun";
|
|
4
|
-
import { log } from "../logger/instance";
|
|
5
|
-
import { compileForBrowser } from "./compile-module";
|
|
6
|
-
import type { DevGraph } from "./graph";
|
|
7
|
-
import { normalizePath } from "./paths";
|
|
8
|
-
import { safeRead } from "./watcher";
|
|
9
|
-
|
|
10
|
-
const logger = log.scope("hmr");
|
|
11
|
-
|
|
12
|
-
export class HmrServer {
|
|
13
|
-
private clients = new Set<ServerWebSocket<any>>();
|
|
14
|
-
private v = 0;
|
|
15
|
-
get clientCount() {
|
|
16
|
-
return this.clients.size;
|
|
17
|
-
}
|
|
18
|
-
addClient(ws: ServerWebSocket<any>) {
|
|
19
|
-
this.clients.add(ws);
|
|
20
|
-
ws.send(JSON.stringify({ type: "connected" }));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
removeClient(ws: ServerWebSocket<any>) {
|
|
24
|
-
this.clients.delete(ws);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
send(msg: object) {
|
|
28
|
-
const s = JSON.stringify(msg);
|
|
29
|
-
for (const c of this.clients) c.send(s);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
batch(payloads: object[]) {
|
|
33
|
-
this.send({ type: "batch", payloads, v: ++this.v });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
reload() {
|
|
37
|
-
logger.info("sending reload to", this.clients.size, "clients");
|
|
38
|
-
this.batch([{ kind: "reload" }]);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
error(message: string, loc?: { file: string; line: number; col: number }) {
|
|
42
|
-
this.send({ type: "error", message, loc });
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function extractLoc(
|
|
47
|
-
e: any,
|
|
48
|
-
filePath: string,
|
|
49
|
-
): { file: string; line: number; col: number } | undefined {
|
|
50
|
-
// Babel errors have e.loc.line / e.loc.column
|
|
51
|
-
if (e?.loc?.line) {
|
|
52
|
-
return {
|
|
53
|
-
file: filePath,
|
|
54
|
-
line: e.loc.line,
|
|
55
|
-
col: e.loc.column ?? 0,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
// fallback: parse "file:line:col" from message
|
|
59
|
-
const m = String(e?.message ?? "").match(/:(\d+):(\d+)$/);
|
|
60
|
-
if (m) {
|
|
61
|
-
return {
|
|
62
|
-
file: filePath,
|
|
63
|
-
line: Number(m[1]),
|
|
64
|
-
col: Number(m[2]),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export async function compileAndSwap(
|
|
71
|
-
filePath: string,
|
|
72
|
-
graph: DevGraph,
|
|
73
|
-
hmr: HmrServer,
|
|
74
|
-
projectRoot: string,
|
|
75
|
-
_routeTree: any,
|
|
76
|
-
): Promise<void> {
|
|
77
|
-
const key = normalizePath(filePath);
|
|
78
|
-
const node = graph.get(key);
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
const result = await compileForBrowser(filePath, projectRoot);
|
|
82
|
-
|
|
83
|
-
// write to dev-modules cache
|
|
84
|
-
const devModulesDir = join(projectRoot, ".grimoire/dev-modules");
|
|
85
|
-
mkdirSync(devModulesDir, { recursive: true });
|
|
86
|
-
const { createHash } = await import("node:crypto");
|
|
87
|
-
const fileHash = createHash("md5")
|
|
88
|
-
.update(filePath)
|
|
89
|
-
.digest("hex")
|
|
90
|
-
.slice(0, 8);
|
|
91
|
-
const v = graph.bump(key);
|
|
92
|
-
const cachePath = join(devModulesDir, `${fileHash}_v${v}.js`);
|
|
93
|
-
writeFileSync(cachePath, result.js);
|
|
94
|
-
|
|
95
|
-
// store in graph node
|
|
96
|
-
if (node) {
|
|
97
|
-
node.compiled = {
|
|
98
|
-
js: result.js,
|
|
99
|
-
css: result.css,
|
|
100
|
-
cssHash: result.cssHash,
|
|
101
|
-
specifiers: [], // populated in Step 4
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// find affected route pattern
|
|
106
|
-
const affectedRoutes = graph.affectedRoutes(key);
|
|
107
|
-
const routeFile = node?.isRoute ?? affectedRoutes[0] ?? null;
|
|
108
|
-
|
|
109
|
-
if (!routeFile) {
|
|
110
|
-
// no route found — fall back to reload
|
|
111
|
-
logger.info("no route affected, reloading");
|
|
112
|
-
hmr.reload();
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// client-relative path for the module URL
|
|
117
|
-
const clientPath = relative(projectRoot, filePath).replace(/\\/g, "/");
|
|
118
|
-
const modUrl = `${clientPath}.js`;
|
|
119
|
-
|
|
120
|
-
logger.info(`compiled: ${filePath}, v=${v}`);
|
|
121
|
-
|
|
122
|
-
const payloads: object[] = [];
|
|
123
|
-
|
|
124
|
-
// CSS payload if style changed
|
|
125
|
-
if (result.css) {
|
|
126
|
-
payloads.push({
|
|
127
|
-
kind: "css",
|
|
128
|
-
hash: result.cssHash,
|
|
129
|
-
text: result.css,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// swap payload
|
|
134
|
-
payloads.push({
|
|
135
|
-
kind: "swap",
|
|
136
|
-
pattern: routeFile.path,
|
|
137
|
-
modUrl,
|
|
138
|
-
version: v,
|
|
139
|
-
cachePath: `${fileHash}_v${v}.js`, // server-side cache filename
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// stale payloads for other affected routes
|
|
143
|
-
const otherRoutes = affectedRoutes.filter((r) => r !== routeFile);
|
|
144
|
-
if (otherRoutes.length > 0) {
|
|
145
|
-
payloads.push({
|
|
146
|
-
kind: "stale",
|
|
147
|
-
patterns: otherRoutes.map((r) => r.path),
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
logger.info("sending batch:", JSON.stringify(payloads, null, 2));
|
|
151
|
-
hmr.batch(payloads);
|
|
152
|
-
} catch (e: any) {
|
|
153
|
-
const loc = extractLoc(e, filePath);
|
|
154
|
-
console.error("[sigil hmr] compile error:", e.message);
|
|
155
|
-
hmr.error(e.message ?? String(e), loc);
|
|
156
|
-
// no bump — old version stays live
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export async function handleChange(
|
|
161
|
-
filePath: string,
|
|
162
|
-
graph: DevGraph,
|
|
163
|
-
hmr: HmrServer,
|
|
164
|
-
_srcDir: string,
|
|
165
|
-
projectRoot: string,
|
|
166
|
-
routeTree: any,
|
|
167
|
-
): Promise<void> {
|
|
168
|
-
const key = normalizePath(filePath);
|
|
169
|
-
const source = await safeRead(filePath);
|
|
170
|
-
if (!source) {
|
|
171
|
-
hmr.reload();
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
if (graph.hasCycle(key)) {
|
|
175
|
-
hmr.reload();
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
const node = graph.get(key);
|
|
179
|
-
if (!node) {
|
|
180
|
-
logger.info("unknown file, reloading:", filePath);
|
|
181
|
-
hmr.reload();
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
logger.info("changed:", filePath);
|
|
186
|
-
await compileAndSwap(filePath, graph, hmr, projectRoot, routeTree);
|
|
187
|
-
}
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import type { ServerWebSocket } from "bun";
|
|
4
|
+
import { log } from "../logger/instance";
|
|
5
|
+
import { compileForBrowser } from "./compile-module";
|
|
6
|
+
import type { DevGraph } from "./graph";
|
|
7
|
+
import { normalizePath } from "./paths";
|
|
8
|
+
import { safeRead } from "./watcher";
|
|
9
|
+
|
|
10
|
+
const logger = log.scope("hmr");
|
|
11
|
+
|
|
12
|
+
export class HmrServer {
|
|
13
|
+
private clients = new Set<ServerWebSocket<any>>();
|
|
14
|
+
private v = 0;
|
|
15
|
+
get clientCount() {
|
|
16
|
+
return this.clients.size;
|
|
17
|
+
}
|
|
18
|
+
addClient(ws: ServerWebSocket<any>) {
|
|
19
|
+
this.clients.add(ws);
|
|
20
|
+
ws.send(JSON.stringify({ type: "connected" }));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
removeClient(ws: ServerWebSocket<any>) {
|
|
24
|
+
this.clients.delete(ws);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
send(msg: object) {
|
|
28
|
+
const s = JSON.stringify(msg);
|
|
29
|
+
for (const c of this.clients) c.send(s);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
batch(payloads: object[]) {
|
|
33
|
+
this.send({ type: "batch", payloads, v: ++this.v });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
reload() {
|
|
37
|
+
logger.info("sending reload to", this.clients.size, "clients");
|
|
38
|
+
this.batch([{ kind: "reload" }]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
error(message: string, loc?: { file: string; line: number; col: number }) {
|
|
42
|
+
this.send({ type: "error", message, loc });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function extractLoc(
|
|
47
|
+
e: any,
|
|
48
|
+
filePath: string,
|
|
49
|
+
): { file: string; line: number; col: number } | undefined {
|
|
50
|
+
// Babel errors have e.loc.line / e.loc.column
|
|
51
|
+
if (e?.loc?.line) {
|
|
52
|
+
return {
|
|
53
|
+
file: filePath,
|
|
54
|
+
line: e.loc.line,
|
|
55
|
+
col: e.loc.column ?? 0,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
// fallback: parse "file:line:col" from message
|
|
59
|
+
const m = String(e?.message ?? "").match(/:(\d+):(\d+)$/);
|
|
60
|
+
if (m) {
|
|
61
|
+
return {
|
|
62
|
+
file: filePath,
|
|
63
|
+
line: Number(m[1]),
|
|
64
|
+
col: Number(m[2]),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function compileAndSwap(
|
|
71
|
+
filePath: string,
|
|
72
|
+
graph: DevGraph,
|
|
73
|
+
hmr: HmrServer,
|
|
74
|
+
projectRoot: string,
|
|
75
|
+
_routeTree: any,
|
|
76
|
+
): Promise<void> {
|
|
77
|
+
const key = normalizePath(filePath);
|
|
78
|
+
const node = graph.get(key);
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const result = await compileForBrowser(filePath, projectRoot);
|
|
82
|
+
|
|
83
|
+
// write to dev-modules cache
|
|
84
|
+
const devModulesDir = join(projectRoot, ".grimoire/dev-modules");
|
|
85
|
+
mkdirSync(devModulesDir, { recursive: true });
|
|
86
|
+
const { createHash } = await import("node:crypto");
|
|
87
|
+
const fileHash = createHash("md5")
|
|
88
|
+
.update(filePath)
|
|
89
|
+
.digest("hex")
|
|
90
|
+
.slice(0, 8);
|
|
91
|
+
const v = graph.bump(key);
|
|
92
|
+
const cachePath = join(devModulesDir, `${fileHash}_v${v}.js`);
|
|
93
|
+
writeFileSync(cachePath, result.js);
|
|
94
|
+
|
|
95
|
+
// store in graph node
|
|
96
|
+
if (node) {
|
|
97
|
+
node.compiled = {
|
|
98
|
+
js: result.js,
|
|
99
|
+
css: result.css,
|
|
100
|
+
cssHash: result.cssHash,
|
|
101
|
+
specifiers: [], // populated in Step 4
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// find affected route pattern
|
|
106
|
+
const affectedRoutes = graph.affectedRoutes(key);
|
|
107
|
+
const routeFile = node?.isRoute ?? affectedRoutes[0] ?? null;
|
|
108
|
+
|
|
109
|
+
if (!routeFile) {
|
|
110
|
+
// no route found — fall back to reload
|
|
111
|
+
logger.info("no route affected, reloading");
|
|
112
|
+
hmr.reload();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// client-relative path for the module URL
|
|
117
|
+
const clientPath = relative(projectRoot, filePath).replace(/\\/g, "/");
|
|
118
|
+
const modUrl = `${clientPath}.js`;
|
|
119
|
+
|
|
120
|
+
logger.info(`compiled: ${filePath}, v=${v}`);
|
|
121
|
+
|
|
122
|
+
const payloads: object[] = [];
|
|
123
|
+
|
|
124
|
+
// CSS payload if style changed
|
|
125
|
+
if (result.css) {
|
|
126
|
+
payloads.push({
|
|
127
|
+
kind: "css",
|
|
128
|
+
hash: result.cssHash,
|
|
129
|
+
text: result.css,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// swap payload
|
|
134
|
+
payloads.push({
|
|
135
|
+
kind: "swap",
|
|
136
|
+
pattern: routeFile.path,
|
|
137
|
+
modUrl,
|
|
138
|
+
version: v,
|
|
139
|
+
cachePath: `${fileHash}_v${v}.js`, // server-side cache filename
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// stale payloads for other affected routes
|
|
143
|
+
const otherRoutes = affectedRoutes.filter((r) => r !== routeFile);
|
|
144
|
+
if (otherRoutes.length > 0) {
|
|
145
|
+
payloads.push({
|
|
146
|
+
kind: "stale",
|
|
147
|
+
patterns: otherRoutes.map((r) => r.path),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
logger.info("sending batch:", JSON.stringify(payloads, null, 2));
|
|
151
|
+
hmr.batch(payloads);
|
|
152
|
+
} catch (e: any) {
|
|
153
|
+
const loc = extractLoc(e, filePath);
|
|
154
|
+
console.error("[sigil hmr] compile error:", e.message);
|
|
155
|
+
hmr.error(e.message ?? String(e), loc);
|
|
156
|
+
// no bump — old version stays live
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export async function handleChange(
|
|
161
|
+
filePath: string,
|
|
162
|
+
graph: DevGraph,
|
|
163
|
+
hmr: HmrServer,
|
|
164
|
+
_srcDir: string,
|
|
165
|
+
projectRoot: string,
|
|
166
|
+
routeTree: any,
|
|
167
|
+
): Promise<void> {
|
|
168
|
+
const key = normalizePath(filePath);
|
|
169
|
+
const source = await safeRead(filePath);
|
|
170
|
+
if (!source) {
|
|
171
|
+
hmr.reload();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (graph.hasCycle(key)) {
|
|
175
|
+
hmr.reload();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const node = graph.get(key);
|
|
179
|
+
if (!node) {
|
|
180
|
+
logger.info("unknown file, reloading:", filePath);
|
|
181
|
+
hmr.reload();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
logger.info("changed:", filePath);
|
|
186
|
+
await compileAndSwap(filePath, graph, hmr, projectRoot, routeTree);
|
|
187
|
+
}
|
package/src/dev/watcher.ts
CHANGED
|
@@ -20,9 +20,11 @@ export function startWatcher(
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
const watcher = watch(srcDir, { recursive: true }, (_evt, filename) => {
|
|
23
|
-
console.log("[sigil hmr] fs.watch fired:", _evt, filename);
|
|
24
23
|
if (!filename) return;
|
|
25
|
-
const
|
|
24
|
+
const name = filename.toString();
|
|
25
|
+
// FUCKING WEBSTORM
|
|
26
|
+
if (name.endsWith("~") || name.endsWith(".tmp") || name.startsWith(".")) return;
|
|
27
|
+
const raw = join(srcDir, name);
|
|
26
28
|
pending.add(normalizePath(raw));
|
|
27
29
|
if (debounce) clearTimeout(debounce);
|
|
28
30
|
debounce = setTimeout(flush, 60);
|
|
@@ -0,0 +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
|
+
);
|
|
@@ -0,0 +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
|
+
);
|
package/src/env/plugin.ts
CHANGED
|
@@ -9,5 +9,11 @@ export const envPlugin = () => ({
|
|
|
9
9
|
build.onResolve({ filter: /^\$env\/static\/private$/ }, () => ({
|
|
10
10
|
path: join(import.meta.dir, "private.ts"),
|
|
11
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
|
+
}));
|
|
12
18
|
},
|
|
13
19
|
});
|
package/src/env/typegen.ts
CHANGED
|
@@ -42,10 +42,20 @@ declare module "$env/static/private" {
|
|
|
42
42
|
${privateVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
43
43
|
${privateVars.length === 0 ? "\texport {};" : ""}
|
|
44
44
|
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const dynamicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
48
|
+
declare module "$env/dynamic/public" {
|
|
49
|
+
\texport const env: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
declare module "$env/dynamic/private" {
|
|
52
|
+
\texport const env: Record<string, string>;
|
|
53
|
+
}
|
|
45
54
|
`;
|
|
46
55
|
|
|
47
56
|
await Promise.all([
|
|
48
57
|
writeFile(join(outDir, "env-public.d.ts"), publicTypeContent, "utf-8"),
|
|
49
58
|
writeFile(join(outDir, "env-private.d.ts"), privateTypeContent, "utf-8"),
|
|
59
|
+
writeFile(join(outDir, "env-dynamic.d.ts"), dynamicTypeContent, "utf-8"),
|
|
50
60
|
]);
|
|
51
61
|
}
|