@sigil-dev/grimoire 0.9.2 → 0.9.4
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 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +31 -8
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +529 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- 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 +47 -47
- package/src/rendering/hydrate.ts +146 -119
- package/src/rendering/index.ts +573 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +55 -42
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +187 -141
- 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 +255 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +677 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +8 -4
- package/test/client-router.test.ts +295 -35
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- 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 +728 -579
- package/test/routing.test.ts +194 -81
- package/test/scanning.test.ts +254 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +354 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/src/dev/hmr-client.ts
CHANGED
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
export const HMR_CLIENT_SOURCE = `
|
|
2
|
-
(function() {
|
|
3
|
-
let version = 0;
|
|
4
|
-
|
|
5
|
-
const staleRoutes = new Set();
|
|
6
|
-
|
|
7
|
-
async function applySwap(p) {
|
|
8
|
-
try {
|
|
9
|
-
captureSignals(); // ← capture before
|
|
10
|
-
const url = '/__grimoire__/m/' + p.modUrl + '?v=' + p.version;
|
|
11
|
-
const mod = await import(url);
|
|
12
|
-
clearCapture(); // ← clear after
|
|
13
|
-
|
|
14
|
-
if (window.__grimoire_routes__) {
|
|
15
|
-
window.__grimoire_routes__[p.pattern] = mod.default;
|
|
16
|
-
}
|
|
17
|
-
const currentPattern = window.__grimoire_current_pattern__;
|
|
18
|
-
if (currentPattern === p.pattern && window.__grimoire_rerender__) {
|
|
19
|
-
await window.__grimoire_rerender__();
|
|
20
|
-
}
|
|
21
|
-
} catch (e) {
|
|
22
|
-
clearCapture();
|
|
23
|
-
console.error('[sigil hmr] swap failed, reloading:', e);
|
|
24
|
-
location.reload();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async function refetchCurrent() {
|
|
29
|
-
if (window.__grimoire_navigate__) {
|
|
30
|
-
await window.__grimoire_navigate__(location.pathname, true);
|
|
31
|
-
} else {
|
|
32
|
-
location.reload();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function applyCss(hash, text) {
|
|
37
|
-
let el = document.getElementById('sigil-' + hash);
|
|
38
|
-
if (!el) {
|
|
39
|
-
el = document.createElement('style');
|
|
40
|
-
el.id = 'sigil-' + hash;
|
|
41
|
-
document.head.appendChild(el);
|
|
42
|
-
}
|
|
43
|
-
el.textContent = text;
|
|
44
|
-
}
|
|
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
|
-
|
|
73
|
-
function connect() {
|
|
74
|
-
const ws = new WebSocket(
|
|
75
|
-
(location.protocol === 'https:' ? 'wss' : 'ws') +
|
|
76
|
-
'://' + location.host + '/__grimoire__/hmr'
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
ws.onopen = () => {
|
|
80
|
-
console.log('[sigil hmr] connected');
|
|
81
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
ws.onmessage = async (ev) => {
|
|
85
|
-
const msg = JSON.parse(ev.data);
|
|
86
|
-
if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
|
|
87
|
-
if (msg.v !== undefined && msg.v <= version) return;
|
|
88
|
-
if (msg.v !== undefined) version = msg.v;
|
|
89
|
-
|
|
90
|
-
if (msg.type === 'batch') {
|
|
91
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
92
|
-
|
|
93
|
-
let hasReload = false;
|
|
94
|
-
let swapPayload = null;
|
|
95
|
-
const cssPayloads = [];
|
|
96
|
-
const stalePatterns = [];
|
|
97
|
-
|
|
98
|
-
for (const p of msg.payloads) {
|
|
99
|
-
if (p.kind === 'reload') { hasReload = true; break; }
|
|
100
|
-
if (p.kind === 'css') cssPayloads.push(p);
|
|
101
|
-
if (p.kind === 'swap') swapPayload = p;
|
|
102
|
-
if (p.kind === 'stale') stalePatterns.push(...p.patterns);
|
|
103
|
-
if (p.kind === 'invalidate') refetchCurrent();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (hasReload) { location.reload(); return; }
|
|
107
|
-
|
|
108
|
-
// apply CSS instantly
|
|
109
|
-
for (const p of cssPayloads) applyCss(p.hash, p.text);
|
|
110
|
-
|
|
111
|
-
// mark stale routes
|
|
112
|
-
for (const pattern of stalePatterns) staleRoutes.add(pattern);
|
|
113
|
-
|
|
114
|
-
// apply swap
|
|
115
|
-
if (swapPayload) {
|
|
116
|
-
await applySwap(swapPayload);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
ws.onclose = () => {
|
|
122
|
-
console.log('[sigil hmr] disconnected, reconnecting...');
|
|
123
|
-
const retry = setInterval(async () => {
|
|
124
|
-
try {
|
|
125
|
-
await fetch('/', { method: 'HEAD' });
|
|
126
|
-
clearInterval(retry);
|
|
127
|
-
location.reload();
|
|
128
|
-
} catch {}
|
|
129
|
-
}, 300);
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function stripAnsi(str) {
|
|
134
|
-
return str.replace(/\u001b[[0-9;]*m/g, '');
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function unescapeHtml(str) {
|
|
138
|
-
let result = str;
|
|
139
|
-
// run twice to handle double-escaped entities
|
|
140
|
-
for (let i = 0; i < 2; i++) {
|
|
141
|
-
result = result
|
|
142
|
-
.replace(/&/g, '&')
|
|
143
|
-
.replace(/</g, '<')
|
|
144
|
-
.replace(/>/g, '>')
|
|
145
|
-
.replace(/"/g, '"')
|
|
146
|
-
.replace(/'/g, "'");
|
|
147
|
-
}
|
|
148
|
-
return result;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function showOverlay(message, loc) {
|
|
152
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
153
|
-
const clean = unescapeHtml(stripAnsi(message));
|
|
154
|
-
const d = document.createElement('div');
|
|
155
|
-
d.id = '__grimoire_overlay__';
|
|
156
|
-
d.style.cssText = [
|
|
157
|
-
'position:fixed;inset:0;z-index:99999',
|
|
158
|
-
'background:rgba(15,15,20,.94)',
|
|
159
|
-
'color:#ff8a8a',
|
|
160
|
-
'font:13px/1.6 monospace',
|
|
161
|
-
'padding:40px',
|
|
162
|
-
'overflow:auto',
|
|
163
|
-
'white-space:pre-wrap',
|
|
164
|
-
].join(';');
|
|
165
|
-
|
|
166
|
-
d.textContent = '[sigil] compile error\\n\\n' + clean;
|
|
167
|
-
|
|
168
|
-
if (loc) {
|
|
169
|
-
const link = document.createElement('a');
|
|
170
|
-
link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
|
|
171
|
-
'&line=' + loc.line + '&col=' + loc.col;
|
|
172
|
-
link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
|
|
173
|
-
link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
|
|
174
|
-
link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
|
|
175
|
-
d.appendChild(link);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
d.title = 'Click anywhere to dismiss';
|
|
179
|
-
d.onclick = (ev) => { if (ev.target === d) d.remove(); };
|
|
180
|
-
document.body.appendChild(d);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
connect();
|
|
184
|
-
})();
|
|
185
|
-
`;
|
|
1
|
+
export const HMR_CLIENT_SOURCE = `
|
|
2
|
+
(function() {
|
|
3
|
+
let version = 0;
|
|
4
|
+
|
|
5
|
+
const staleRoutes = new Set();
|
|
6
|
+
|
|
7
|
+
async function applySwap(p) {
|
|
8
|
+
try {
|
|
9
|
+
captureSignals(); // ← capture before
|
|
10
|
+
const url = '/__grimoire__/m/' + p.modUrl + '?v=' + p.version;
|
|
11
|
+
const mod = await import(url);
|
|
12
|
+
clearCapture(); // ← clear after
|
|
13
|
+
|
|
14
|
+
if (window.__grimoire_routes__) {
|
|
15
|
+
window.__grimoire_routes__[p.pattern] = mod.default;
|
|
16
|
+
}
|
|
17
|
+
const currentPattern = window.__grimoire_current_pattern__;
|
|
18
|
+
if (currentPattern === p.pattern && window.__grimoire_rerender__) {
|
|
19
|
+
await window.__grimoire_rerender__();
|
|
20
|
+
}
|
|
21
|
+
} catch (e) {
|
|
22
|
+
clearCapture();
|
|
23
|
+
console.error('[sigil hmr] swap failed, reloading:', e);
|
|
24
|
+
location.reload();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function refetchCurrent() {
|
|
29
|
+
if (window.__grimoire_navigate__) {
|
|
30
|
+
await window.__grimoire_navigate__(location.pathname, true);
|
|
31
|
+
} else {
|
|
32
|
+
location.reload();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function applyCss(hash, text) {
|
|
37
|
+
let el = document.getElementById('sigil-' + hash);
|
|
38
|
+
if (!el) {
|
|
39
|
+
el = document.createElement('style');
|
|
40
|
+
el.id = 'sigil-' + hash;
|
|
41
|
+
document.head.appendChild(el);
|
|
42
|
+
}
|
|
43
|
+
el.textContent = text;
|
|
44
|
+
}
|
|
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
|
+
|
|
73
|
+
function connect() {
|
|
74
|
+
const ws = new WebSocket(
|
|
75
|
+
(location.protocol === 'https:' ? 'wss' : 'ws') +
|
|
76
|
+
'://' + location.host + '/__grimoire__/hmr'
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
ws.onopen = () => {
|
|
80
|
+
console.log('[sigil hmr] connected');
|
|
81
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
ws.onmessage = async (ev) => {
|
|
85
|
+
const msg = JSON.parse(ev.data);
|
|
86
|
+
if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
|
|
87
|
+
if (msg.v !== undefined && msg.v <= version) return;
|
|
88
|
+
if (msg.v !== undefined) version = msg.v;
|
|
89
|
+
|
|
90
|
+
if (msg.type === 'batch') {
|
|
91
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
92
|
+
|
|
93
|
+
let hasReload = false;
|
|
94
|
+
let swapPayload = null;
|
|
95
|
+
const cssPayloads = [];
|
|
96
|
+
const stalePatterns = [];
|
|
97
|
+
|
|
98
|
+
for (const p of msg.payloads) {
|
|
99
|
+
if (p.kind === 'reload') { hasReload = true; break; }
|
|
100
|
+
if (p.kind === 'css') cssPayloads.push(p);
|
|
101
|
+
if (p.kind === 'swap') swapPayload = p;
|
|
102
|
+
if (p.kind === 'stale') stalePatterns.push(...p.patterns);
|
|
103
|
+
if (p.kind === 'invalidate') refetchCurrent();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (hasReload) { location.reload(); return; }
|
|
107
|
+
|
|
108
|
+
// apply CSS instantly
|
|
109
|
+
for (const p of cssPayloads) applyCss(p.hash, p.text);
|
|
110
|
+
|
|
111
|
+
// mark stale routes
|
|
112
|
+
for (const pattern of stalePatterns) staleRoutes.add(pattern);
|
|
113
|
+
|
|
114
|
+
// apply swap
|
|
115
|
+
if (swapPayload) {
|
|
116
|
+
await applySwap(swapPayload);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
ws.onclose = () => {
|
|
122
|
+
console.log('[sigil hmr] disconnected, reconnecting...');
|
|
123
|
+
const retry = setInterval(async () => {
|
|
124
|
+
try {
|
|
125
|
+
await fetch('/', { method: 'HEAD' });
|
|
126
|
+
clearInterval(retry);
|
|
127
|
+
location.reload();
|
|
128
|
+
} catch {}
|
|
129
|
+
}, 300);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function stripAnsi(str) {
|
|
134
|
+
return str.replace(/\u001b[[0-9;]*m/g, '');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function unescapeHtml(str) {
|
|
138
|
+
let result = str;
|
|
139
|
+
// run twice to handle double-escaped entities
|
|
140
|
+
for (let i = 0; i < 2; i++) {
|
|
141
|
+
result = result
|
|
142
|
+
.replace(/&/g, '&')
|
|
143
|
+
.replace(/</g, '<')
|
|
144
|
+
.replace(/>/g, '>')
|
|
145
|
+
.replace(/"/g, '"')
|
|
146
|
+
.replace(/'/g, "'");
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function showOverlay(message, loc) {
|
|
152
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
153
|
+
const clean = unescapeHtml(stripAnsi(message));
|
|
154
|
+
const d = document.createElement('div');
|
|
155
|
+
d.id = '__grimoire_overlay__';
|
|
156
|
+
d.style.cssText = [
|
|
157
|
+
'position:fixed;inset:0;z-index:99999',
|
|
158
|
+
'background:rgba(15,15,20,.94)',
|
|
159
|
+
'color:#ff8a8a',
|
|
160
|
+
'font:13px/1.6 monospace',
|
|
161
|
+
'padding:40px',
|
|
162
|
+
'overflow:auto',
|
|
163
|
+
'white-space:pre-wrap',
|
|
164
|
+
].join(';');
|
|
165
|
+
|
|
166
|
+
d.textContent = '[sigil] compile error\\n\\n' + clean;
|
|
167
|
+
|
|
168
|
+
if (loc) {
|
|
169
|
+
const link = document.createElement('a');
|
|
170
|
+
link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
|
|
171
|
+
'&line=' + loc.line + '&col=' + loc.col;
|
|
172
|
+
link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
|
|
173
|
+
link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
|
|
174
|
+
link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
|
|
175
|
+
d.appendChild(link);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
d.title = 'Click anywhere to dismiss';
|
|
179
|
+
d.onclick = (ev) => { if (ev.target === d) d.remove(); };
|
|
180
|
+
document.body.appendChild(d);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
connect();
|
|
184
|
+
})();
|
|
185
|
+
`;
|
package/src/dev/loader.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
4
|
-
import { disposeModuleEffects } from "./effect-registry";
|
|
5
|
-
import type { DevGraph } from "./graph";
|
|
6
|
-
import { normalizePath } from "./paths";
|
|
7
|
-
|
|
8
|
-
export function makeDevLoader(graph: DevGraph, projectRoot: string) {
|
|
9
|
-
const cacheDir = join(projectRoot, ".grimoire/dev-cache");
|
|
10
|
-
mkdirSync(cacheDir, { recursive: true });
|
|
11
|
-
|
|
12
|
-
return async function devLoadModule(filePath: string): Promise<any> {
|
|
13
|
-
const key = normalizePath(filePath);
|
|
14
|
-
disposeModuleEffects(key);
|
|
15
|
-
const v = graph.get(key)?.version ?? 0;
|
|
16
|
-
|
|
17
|
-
if (v === 0) {
|
|
18
|
-
// first load — let Bun handle it normally via SSR plugin
|
|
19
|
-
return import(filePath);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// versioned load — read the source, let the SSR plugin transform it,
|
|
23
|
-
// write transformed output to a unique cache path, import that
|
|
24
|
-
// We can't re-trigger the plugin, so we invoke the compile pipeline directly
|
|
25
|
-
const source = await Bun.file(filePath).text();
|
|
26
|
-
const { transformSync } = await import("@babel/core");
|
|
27
|
-
const sigilPlugin = (await import("@sigil-dev/compiler/babel")).default;
|
|
28
|
-
const { createHash } = await import("node:crypto");
|
|
29
|
-
const hash = createHash("md5").update(filePath).digest("hex").slice(0, 8);
|
|
30
|
-
|
|
31
|
-
const result = transformSync(source, {
|
|
32
|
-
configFile: false,
|
|
33
|
-
babelrc: false,
|
|
34
|
-
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
35
|
-
plugins: [[sigilPlugin, { mode: "ssr", hash }]],
|
|
36
|
-
filename: filePath,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const transpiler = new Bun.Transpiler({ loader: "ts", target: "bun" });
|
|
40
|
-
const compiled = transpiler.transformSync(result?.code ?? "");
|
|
41
|
-
|
|
42
|
-
const cachePath = join(cacheDir, `${hash}_v${v}.js`);
|
|
43
|
-
writeFileSync(cachePath, compiled);
|
|
44
|
-
|
|
45
|
-
return import(pathToFileURL(cachePath).href);
|
|
46
|
-
};
|
|
47
|
-
}
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { disposeModuleEffects } from "./effect-registry";
|
|
5
|
+
import type { DevGraph } from "./graph";
|
|
6
|
+
import { normalizePath } from "./paths";
|
|
7
|
+
|
|
8
|
+
export function makeDevLoader(graph: DevGraph, projectRoot: string) {
|
|
9
|
+
const cacheDir = join(projectRoot, ".grimoire/dev-cache");
|
|
10
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
11
|
+
|
|
12
|
+
return async function devLoadModule(filePath: string): Promise<any> {
|
|
13
|
+
const key = normalizePath(filePath);
|
|
14
|
+
disposeModuleEffects(key);
|
|
15
|
+
const v = graph.get(key)?.version ?? 0;
|
|
16
|
+
|
|
17
|
+
if (v === 0) {
|
|
18
|
+
// first load — let Bun handle it normally via SSR plugin
|
|
19
|
+
return import(filePath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// versioned load — read the source, let the SSR plugin transform it,
|
|
23
|
+
// write transformed output to a unique cache path, import that
|
|
24
|
+
// We can't re-trigger the plugin, so we invoke the compile pipeline directly
|
|
25
|
+
const source = await Bun.file(filePath).text();
|
|
26
|
+
const { transformSync } = await import("@babel/core");
|
|
27
|
+
const sigilPlugin = (await import("@sigil-dev/compiler/babel")).default;
|
|
28
|
+
const { createHash } = await import("node:crypto");
|
|
29
|
+
const hash = createHash("md5").update(filePath).digest("hex").slice(0, 8);
|
|
30
|
+
|
|
31
|
+
const result = transformSync(source, {
|
|
32
|
+
configFile: false,
|
|
33
|
+
babelrc: false,
|
|
34
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
35
|
+
plugins: [[sigilPlugin, { mode: "ssr", hash }]],
|
|
36
|
+
filename: filePath,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "bun" });
|
|
40
|
+
const compiled = transpiler.transformSync(result?.code ?? "");
|
|
41
|
+
|
|
42
|
+
const cachePath = join(cacheDir, `${hash}_v${v}.js`);
|
|
43
|
+
writeFileSync(cachePath, compiled);
|
|
44
|
+
|
|
45
|
+
return import(pathToFileURL(cachePath).href);
|
|
46
|
+
};
|
|
47
|
+
}
|
package/src/dev/paths.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
|
|
3
|
-
export function normalizePath(p: string) {
|
|
4
|
-
let n = resolve(p).replace(/\\/g, "/");
|
|
5
|
-
if (process.platform === "win32") n = n.toLowerCase();
|
|
6
|
-
return n;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function keyFromUrl(pathname: string, prefix: string) {
|
|
10
|
-
// __grimoire__/m/src/lib/Example.tsx.js -> src/lib/Example.tsx
|
|
11
|
-
return decodeURIComponent(pathname.slice(prefix.length))
|
|
12
|
-
.replace(/\.js(\?.*)?$/, "")
|
|
13
|
-
.replace(/\\/g, "");
|
|
14
|
-
}
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
export function normalizePath(p: string) {
|
|
4
|
+
let n = resolve(p).replace(/\\/g, "/");
|
|
5
|
+
if (process.platform === "win32") n = n.toLowerCase();
|
|
6
|
+
return n;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function keyFromUrl(pathname: string, prefix: string) {
|
|
10
|
+
// __grimoire__/m/src/lib/Example.tsx.js -> src/lib/Example.tsx
|
|
11
|
+
return decodeURIComponent(pathname.slice(prefix.length))
|
|
12
|
+
.replace(/\.js(\?.*)?$/, "")
|
|
13
|
+
.replace(/\\/g, "");
|
|
14
|
+
}
|
|
@@ -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
|
+
}
|