@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.3
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/LICENSE +21 -21
- package/README.md +33 -33
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/index.js +1724 -687
- package/dist/build/route-types.d.ts +3 -1
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/build/shared.d.ts +1 -0
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/cli/index.js +1673 -658
- package/dist/client/link.js +883 -89
- package/dist/client/router/index.d.ts +4 -0
- package/dist/client/router/link-utils.d.ts +3 -1
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/types.d.ts +14 -1
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +44 -0
- package/dist/furin.d.ts +9 -1
- package/dist/furin.js +1578 -258
- package/dist/server/auto-invalidate/runtime.d.ts +1 -1
- package/dist/server/dev-page-plugin.d.ts +3 -0
- package/dist/server/render/assemble.d.ts +2 -0
- package/dist/server/render/isr.d.ts +2 -1
- package/dist/server/render/ssg.d.ts +3 -2
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/router/hmr.d.ts +4 -3
- package/dist/server/router/index.d.ts +1 -1
- package/dist/server/router/index.js +1 -2
- package/dist/server/router/index.ts +1 -2
- package/dist/server/router/plugin.d.ts +2 -1
- package/dist/server/router/schemas.d.ts +25 -17
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +369 -0
- package/dist/server/sync/stream.d.ts +47 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/package.json +10 -5
- package/src/adapter/README.md +13 -13
- package/src/adapter/static.ts +23 -6
- package/src/build/client.ts +152 -149
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -331
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -115
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -78
- package/src/build/ssg-cache.ts +32 -31
- package/src/build/types.ts +89 -89
- package/src/client/link.tsx +13 -4
- package/src/client/router/context.ts +2 -0
- package/src/client/router/index.ts +9 -0
- package/src/client/router/link-utils.ts +184 -180
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +804 -723
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/types.ts +276 -263
- package/src/client/sync.ts +98 -0
- package/src/client.ts +369 -355
- package/src/furin.ts +465 -437
- package/src/server/auto-invalidate/runtime.ts +134 -133
- package/src/server/dev-page-plugin.ts +454 -449
- package/src/server/render/assemble.ts +160 -133
- package/src/server/render/isr.ts +323 -295
- package/src/server/render/not-found.ts +2 -0
- package/src/server/render/ssg.ts +15 -4
- package/src/server/render/ssr.ts +585 -545
- package/src/server/router/hmr.ts +14 -10
- package/src/server/router/index.ts +1 -2
- package/src/server/router/patterns.ts +5 -0
- package/src/server/router/plugin.ts +384 -401
- package/src/server/router/schemas.ts +172 -57
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/index.ts +14 -0
- package/src/server/sync/plugin.ts +108 -0
- package/src/server/sync/stream.ts +144 -0
- package/src/shared/search-params.ts +182 -0
|
@@ -1,449 +1,454 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bun.plugin that loads page modules into a virtual namespace so they stay
|
|
3
|
-
* out of `--hot`'s file watcher.
|
|
4
|
-
*
|
|
5
|
-
* `--hot` re-evaluates every real source file in the module graph when any
|
|
6
|
-
* tracked file changes. By resolving page imports to a virtual namespace
|
|
7
|
-
* (`furin-dev-page`), the actual `.tsx` files are never registered as
|
|
8
|
-
* watched sources — edits trigger only the client-side dev bundler (React
|
|
9
|
-
* Fast Refresh) without a full server restart.
|
|
10
|
-
*
|
|
11
|
-
* ## Cache-busting (prevents hydration mismatches)
|
|
12
|
-
*
|
|
13
|
-
* Bun caches every resolved module by its `(namespace, path)` identity.
|
|
14
|
-
* Without cache-busting, the first import of `pages/index.tsx` would be
|
|
15
|
-
* permanently frozen: after a file edit the server would keep returning the
|
|
16
|
-
* stale module while the client received fresh HMR code, causing a React
|
|
17
|
-
* hydration mismatch.
|
|
18
|
-
*
|
|
19
|
-
* Solution: the caller appends `?furin-server&t=<timestamp>` to the import
|
|
20
|
-
* specifier (a new timestamp on every request). `onResolve` preserves the
|
|
21
|
-
* `?t=...` fragment in the resolved path so each request gets a unique
|
|
22
|
-
* `(namespace, path)` key and Bun always calls `onLoad` fresh.
|
|
23
|
-
*
|
|
24
|
-
* ## Relative import rewriting
|
|
25
|
-
*
|
|
26
|
-
* Modules in a virtual namespace cannot resolve relative specifiers on their
|
|
27
|
-
* own (there is no real directory context). We rewrite every `./` / `../`
|
|
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
|
-
import { dirname, resolve } from "node:path";
|
|
55
|
-
import { fileURLToPath } from "node:url";
|
|
56
|
-
import { invalidateDevLoaderCacheBySource } from "./cache/dev-loader.ts";
|
|
57
|
-
|
|
58
|
-
// Matches ?furin-server with an optional &t=<ms> cache-buster.
|
|
59
|
-
const FURIN_SERVER_FILTER = /\?furin-server(?:&t=\d+)?$/;
|
|
60
|
-
const ANY_FILTER = /.*/;
|
|
61
|
-
const WORKSPACE_SOURCE_FILTER =
|
|
62
|
-
/^(?!.*(
|
|
63
|
-
const T_PARAM_RE = /&t=(\d+)/;
|
|
64
|
-
const STRIP_FURIN_SERVER_RE = /\?furin-server.*$/;
|
|
65
|
-
const STRIP_T_PARAM_RE = /\?t=\d+$/;
|
|
66
|
-
|
|
67
|
-
let _pluginRegistered = false;
|
|
68
|
-
|
|
69
|
-
type SourceLoader = "js" | "jsx" | "ts" | "tsx";
|
|
70
|
-
|
|
71
|
-
// ── Singleton package resolution ───────────────────────────────────────────────
|
|
72
|
-
//
|
|
73
|
-
// React (and react-dom) must be singletons: every module in the render graph
|
|
74
|
-
// must import the EXACT same object instance so that ReactSharedInternals.H
|
|
75
|
-
// (the hooks dispatcher) is shared between react-dom/server and the page.
|
|
76
|
-
//
|
|
77
|
-
// We resolve the absolute on-disk path once at module-evaluation time and
|
|
78
|
-
// rewrite every explicit import in the transpiled source to use that path.
|
|
79
|
-
// The auto-injected jsx-dev-runtime import (added by Bun.Transpiler AFTER the
|
|
80
|
-
// source is transformed) is handled by injectJsxHelperImports below.
|
|
81
|
-
|
|
82
|
-
/** React packages that must share a single instance across the module graph. */
|
|
83
|
-
const SINGLETON_PKGS = [
|
|
84
|
-
"react",
|
|
85
|
-
"react/jsx-runtime",
|
|
86
|
-
"react/jsx-dev-runtime",
|
|
87
|
-
"react-dom",
|
|
88
|
-
"react-dom/server",
|
|
89
|
-
"react-dom/client",
|
|
90
|
-
] as const;
|
|
91
|
-
|
|
92
|
-
/** package specifier → absolute file path, resolved once at plugin registration time. */
|
|
93
|
-
const SINGLETON_PATHS = new Map<string, string>();
|
|
94
|
-
for (const pkg of SINGLETON_PKGS) {
|
|
95
|
-
try {
|
|
96
|
-
SINGLETON_PATHS.set(pkg, fileURLToPath(import.meta.resolve(pkg)));
|
|
97
|
-
} catch {
|
|
98
|
-
// Package not installed — skip (e.g. react-dom/client in a server-only env)
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* the
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* import
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
// Skip
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
result =
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
})
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
* • `
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Bun.plugin that loads page modules into a virtual namespace so they stay
|
|
3
|
+
* out of `--hot`'s file watcher.
|
|
4
|
+
*
|
|
5
|
+
* `--hot` re-evaluates every real source file in the module graph when any
|
|
6
|
+
* tracked file changes. By resolving page imports to a virtual namespace
|
|
7
|
+
* (`furin-dev-page`), the actual `.tsx` files are never registered as
|
|
8
|
+
* watched sources — edits trigger only the client-side dev bundler (React
|
|
9
|
+
* Fast Refresh) without a full server restart.
|
|
10
|
+
*
|
|
11
|
+
* ## Cache-busting (prevents hydration mismatches)
|
|
12
|
+
*
|
|
13
|
+
* Bun caches every resolved module by its `(namespace, path)` identity.
|
|
14
|
+
* Without cache-busting, the first import of `pages/index.tsx` would be
|
|
15
|
+
* permanently frozen: after a file edit the server would keep returning the
|
|
16
|
+
* stale module while the client received fresh HMR code, causing a React
|
|
17
|
+
* hydration mismatch.
|
|
18
|
+
*
|
|
19
|
+
* Solution: the caller appends `?furin-server&t=<timestamp>` to the import
|
|
20
|
+
* specifier (a new timestamp on every request). `onResolve` preserves the
|
|
21
|
+
* `?t=...` fragment in the resolved path so each request gets a unique
|
|
22
|
+
* `(namespace, path)` key and Bun always calls `onLoad` fresh.
|
|
23
|
+
*
|
|
24
|
+
* ## Relative import rewriting
|
|
25
|
+
*
|
|
26
|
+
* Modules in a virtual namespace cannot resolve relative specifiers on their
|
|
27
|
+
* own (there is no real directory context). We rewrite every `./` / `../`
|
|
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
|
+
import { dirname, resolve } from "node:path";
|
|
55
|
+
import { fileURLToPath } from "node:url";
|
|
56
|
+
import { invalidateDevLoaderCacheBySource } from "./cache/dev-loader.ts";
|
|
57
|
+
|
|
58
|
+
// Matches ?furin-server with an optional &t=<ms> cache-buster.
|
|
59
|
+
const FURIN_SERVER_FILTER = /\?furin-server(?:&t=\d+)?$/;
|
|
60
|
+
const ANY_FILTER = /.*/;
|
|
61
|
+
export const WORKSPACE_SOURCE_FILTER =
|
|
62
|
+
/^(?!.*(?:[\\/]node_modules[\\/]|[\\/]\.bun[\\/]))(?!.*\.(?:test|spec)\.[jt]sx?$).*\.[jt]sx?$/;
|
|
63
|
+
const T_PARAM_RE = /&t=(\d+)/;
|
|
64
|
+
const STRIP_FURIN_SERVER_RE = /\?furin-server.*$/;
|
|
65
|
+
const STRIP_T_PARAM_RE = /\?t=\d+$/;
|
|
66
|
+
|
|
67
|
+
let _pluginRegistered = false;
|
|
68
|
+
|
|
69
|
+
type SourceLoader = "js" | "jsx" | "ts" | "tsx";
|
|
70
|
+
|
|
71
|
+
// ── Singleton package resolution ───────────────────────────────────────────────
|
|
72
|
+
//
|
|
73
|
+
// React (and react-dom) must be singletons: every module in the render graph
|
|
74
|
+
// must import the EXACT same object instance so that ReactSharedInternals.H
|
|
75
|
+
// (the hooks dispatcher) is shared between react-dom/server and the page.
|
|
76
|
+
//
|
|
77
|
+
// We resolve the absolute on-disk path once at module-evaluation time and
|
|
78
|
+
// rewrite every explicit import in the transpiled source to use that path.
|
|
79
|
+
// The auto-injected jsx-dev-runtime import (added by Bun.Transpiler AFTER the
|
|
80
|
+
// source is transformed) is handled by injectJsxHelperImports below.
|
|
81
|
+
|
|
82
|
+
/** React packages that must share a single instance across the module graph. */
|
|
83
|
+
const SINGLETON_PKGS = [
|
|
84
|
+
"react",
|
|
85
|
+
"react/jsx-runtime",
|
|
86
|
+
"react/jsx-dev-runtime",
|
|
87
|
+
"react-dom",
|
|
88
|
+
"react-dom/server",
|
|
89
|
+
"react-dom/client",
|
|
90
|
+
] as const;
|
|
91
|
+
|
|
92
|
+
/** package specifier → absolute file path, resolved once at plugin registration time. */
|
|
93
|
+
const SINGLETON_PATHS = new Map<string, string>();
|
|
94
|
+
for (const pkg of SINGLETON_PKGS) {
|
|
95
|
+
try {
|
|
96
|
+
SINGLETON_PATHS.set(pkg, toImportSpecifier(fileURLToPath(import.meta.resolve(pkg))));
|
|
97
|
+
} catch {
|
|
98
|
+
// Package not installed — skip (e.g. react-dom/client in a server-only env)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Converts a native absolute path to Bun's cross-platform import format. */
|
|
103
|
+
export function toImportSpecifier(filePath: string): string {
|
|
104
|
+
return filePath.replaceAll("\\", "/");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function escapeRegExp(s: string): string {
|
|
108
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Rewrites bare React/react-dom singleton imports in source text to their
|
|
113
|
+
* absolute resolved paths. Handles explicit static import / re-export forms:
|
|
114
|
+
*
|
|
115
|
+
* import { useState } from "react" → import { useState } from "/abs/…"
|
|
116
|
+
* import type { FC } from "react"
|
|
117
|
+
* export { x } from "react"
|
|
118
|
+
* import "react/jsx-dev-runtime"
|
|
119
|
+
*
|
|
120
|
+
* Does NOT touch dynamic `import("react")` or `require("react")`.
|
|
121
|
+
*
|
|
122
|
+
* @internal exported for testing
|
|
123
|
+
*/
|
|
124
|
+
export function rewriteSingletonImports(source: string): string {
|
|
125
|
+
let result = source;
|
|
126
|
+
for (const [pkg, absPath] of SINGLETON_PATHS) {
|
|
127
|
+
// react-doctor-disable-next-line react-doctor/js-hoist-regexp
|
|
128
|
+
const re = new RegExp(`((?:from|import(?:\\s+type)?)\\s+)["']${escapeRegExp(pkg)}["']`, "g");
|
|
129
|
+
result = result.replace(re, (_, g1: string) => `${g1}${JSON.stringify(absPath)}`);
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* `Bun.Transpiler.transformSync` converts JSX syntax to function calls using
|
|
136
|
+
* mangled helper names (e.g. `jsxDEV_7x81h0kn`, `Fragment_8vg9x3sq`) but does
|
|
137
|
+
* NOT emit the corresponding import statement — it expects Bun's full bundler
|
|
138
|
+
* pipeline to inject it later.
|
|
139
|
+
*
|
|
140
|
+
* When we return pre-transpiled JS from `onLoad`, that pipeline step never
|
|
141
|
+
* runs, leaving the helpers as undefined free variables. This function scans
|
|
142
|
+
* the transpiled source, detects which mangled helper names are used, and
|
|
143
|
+
* prepends the correct import statements using absolute on-disk paths from
|
|
144
|
+
* `SINGLETON_PATHS`.
|
|
145
|
+
*
|
|
146
|
+
* Helper → package mapping:
|
|
147
|
+
* jsxDEV_* → `{ jsxDEV as … }` from react/jsx-dev-runtime
|
|
148
|
+
* Fragment_* → `{ Fragment as … }` from react/jsx-dev-runtime
|
|
149
|
+
* jsx_* → `{ jsx as … }` from react/jsx-runtime (prod only)
|
|
150
|
+
* jsxs_* → `{ jsxs as … }` from react/jsx-runtime (prod only)
|
|
151
|
+
*
|
|
152
|
+
* @internal exported for testing
|
|
153
|
+
*/
|
|
154
|
+
export function injectJsxHelperImports(transpiled: string): string {
|
|
155
|
+
// Match mangled JSX helper names. Alternation order matters: try longer
|
|
156
|
+
// prefixes first so "jsxDEV_xxx" isn't partially matched as "jsx_xxx".
|
|
157
|
+
const JSX_HELPER_RE = /\b(jsxDEV|jsxs|jsx|Fragment)_([0-9a-z]{8})\b/g;
|
|
158
|
+
|
|
159
|
+
// Package → Map<varName, exportName>
|
|
160
|
+
const devRuntime = new Map<string, string>(); // react/jsx-dev-runtime
|
|
161
|
+
const prodRuntime = new Map<string, string>(); // react/jsx-runtime
|
|
162
|
+
|
|
163
|
+
for (const [, base, hash] of transpiled.matchAll(JSX_HELPER_RE)) {
|
|
164
|
+
if (!(base && hash)) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const varName = `${base}_${hash}`;
|
|
168
|
+
if (base === "jsxDEV" || base === "Fragment") {
|
|
169
|
+
devRuntime.set(varName, base);
|
|
170
|
+
} else {
|
|
171
|
+
prodRuntime.set(varName, base);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const lines: string[] = [];
|
|
176
|
+
|
|
177
|
+
if (devRuntime.size > 0) {
|
|
178
|
+
const absPath = SINGLETON_PATHS.get("react/jsx-dev-runtime");
|
|
179
|
+
if (absPath) {
|
|
180
|
+
const specifiers = [...devRuntime.entries()]
|
|
181
|
+
.map(([varName, exportName]) => `${exportName} as ${varName}`)
|
|
182
|
+
.join(", ");
|
|
183
|
+
lines.push(`import { ${specifiers} } from ${JSON.stringify(absPath)};`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (prodRuntime.size > 0) {
|
|
188
|
+
const absPath = SINGLETON_PATHS.get("react/jsx-runtime");
|
|
189
|
+
if (absPath) {
|
|
190
|
+
const specifiers = [...prodRuntime.entries()]
|
|
191
|
+
.map(([varName, exportName]) => `${exportName} as ${varName}`)
|
|
192
|
+
.join(", ");
|
|
193
|
+
lines.push(`import { ${specifiers} } from ${JSON.stringify(absPath)};`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (lines.length === 0) {
|
|
198
|
+
return transpiled;
|
|
199
|
+
}
|
|
200
|
+
return `${lines.join("\n")}\n${transpiled}`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Matches relative specifiers in all ESM import / re-export forms:
|
|
205
|
+
*
|
|
206
|
+
* import { foo } from "./bar" ← named import
|
|
207
|
+
* import foo from "../baz" ← default import
|
|
208
|
+
* import "./side-effect" ← side-effect import
|
|
209
|
+
* export { x } from "./mod" ← re-export
|
|
210
|
+
* export * from "./mod" ← namespace re-export
|
|
211
|
+
* import type { T } from "./types" ← type import (harmless to rewrite)
|
|
212
|
+
*/
|
|
213
|
+
const RELATIVE_SPECIFIER_RE = /(?:from|import)\s+["'](\.\.?\/[^"']+)["']/g;
|
|
214
|
+
|
|
215
|
+
/** @internal exported for testing */
|
|
216
|
+
export function rewriteRelativeImports(source: string, dir: string): string {
|
|
217
|
+
return source.replace(RELATIVE_SPECIFIER_RE, (match, relPath, offset) => {
|
|
218
|
+
// Skip matches that appear on a comment line (// ...)
|
|
219
|
+
const lineStart = source.lastIndexOf("\n", offset - 1) + 1;
|
|
220
|
+
const linePrefix = source.slice(lineStart, offset).trimStart();
|
|
221
|
+
if (linePrefix.startsWith("//") || linePrefix.startsWith("*")) {
|
|
222
|
+
return match;
|
|
223
|
+
}
|
|
224
|
+
const absPath = toImportSpecifier(resolve(dir, relPath));
|
|
225
|
+
const keyword = match.startsWith("import") ? "import" : "from";
|
|
226
|
+
return `${keyword} ${JSON.stringify(absPath)}`;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Rewrites all remaining bare specifiers in the transpiled JS to absolute
|
|
232
|
+
* on-disk paths resolved from `dir` (the page file's directory).
|
|
233
|
+
*
|
|
234
|
+
* Uses `Bun.Transpiler.scan()` on the original source to get the authoritative
|
|
235
|
+
* list of *actual* import specifiers (parsing correctly handles template
|
|
236
|
+
* literals, comments, etc.). For each bare specifier that is not already
|
|
237
|
+
* handled by `rewriteSingletonImports`, it calls `Bun.resolveSync` and
|
|
238
|
+
* replaces **every** import/re-export occurrence of the quoted specifier using
|
|
239
|
+
* a context-aware regex with the `g` flag — mirroring the approach used by
|
|
240
|
+
* `rewriteSingletonImports`. This correctly handles modules that both import
|
|
241
|
+
* and re-export from the same package without leaving any bare specifier
|
|
242
|
+
* behind. String literals that happen to contain the same text (inside
|
|
243
|
+
* template literals, JSX props, etc.) are not touched because the regex
|
|
244
|
+
* requires a preceding `from` or `import` keyword.
|
|
245
|
+
*
|
|
246
|
+
* @internal exported for testing
|
|
247
|
+
*/
|
|
248
|
+
export function rewriteBareImports(source: string, transpiled: string, dir: string): string {
|
|
249
|
+
const scanner = new Bun.Transpiler({ loader: "tsx" });
|
|
250
|
+
let { imports } = scanner.scan(source);
|
|
251
|
+
// Deduplicate: scan() may return the same specifier multiple times if it
|
|
252
|
+
// appears in both `import` and `export ... from` positions.
|
|
253
|
+
const seen = new Set<string>();
|
|
254
|
+
imports = imports.filter((imp) => {
|
|
255
|
+
if (seen.has(imp.path)) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
seen.add(imp.path);
|
|
259
|
+
return true;
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
let result = transpiled;
|
|
263
|
+
for (const imp of imports) {
|
|
264
|
+
const spec = imp.path;
|
|
265
|
+
// Skip relative paths (already rewritten by rewriteRelativeImports before transpilation)
|
|
266
|
+
// and already-absolute paths.
|
|
267
|
+
if (spec.startsWith(".") || spec.startsWith("/") || spec.startsWith("file:")) {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
// Skip React singletons — already handled by rewriteSingletonImports.
|
|
271
|
+
if (SINGLETON_PATHS.has(spec)) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
let resolved: string;
|
|
276
|
+
try {
|
|
277
|
+
resolved = toImportSpecifier(Bun.resolveSync(spec, dir));
|
|
278
|
+
} catch {
|
|
279
|
+
// Not resolvable from the file's directory — leave as a bare specifier
|
|
280
|
+
// so Bun falls back to CWD-relative resolution (adequate for most cases).
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Replace ALL import/re-export occurrences of the bare specifier using a
|
|
285
|
+
// context-aware regex with the `g` flag. This correctly handles modules
|
|
286
|
+
// that both import and re-export from the same package. The regex only
|
|
287
|
+
// matches when the specifier is preceded by `from` or `import` so string
|
|
288
|
+
// literals in non-import positions are left untouched.
|
|
289
|
+
// react-doctor-disable-next-line react-doctor/js-hoist-regexp
|
|
290
|
+
const re = new RegExp(`((?:from|import(?:\\s+type)?)\\s+)["']${escapeRegExp(spec)}["']`, "g");
|
|
291
|
+
result = result.replace(re, (_, g1: string) => `${g1}${JSON.stringify(resolved)}`);
|
|
292
|
+
}
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function getSourceLoader(filePath: string): SourceLoader | null {
|
|
297
|
+
if (filePath.endsWith(".tsx")) {
|
|
298
|
+
return "tsx";
|
|
299
|
+
}
|
|
300
|
+
if (filePath.endsWith(".ts")) {
|
|
301
|
+
return "ts";
|
|
302
|
+
}
|
|
303
|
+
if (filePath.endsWith(".jsx")) {
|
|
304
|
+
return "jsx";
|
|
305
|
+
}
|
|
306
|
+
if (filePath.endsWith(".js")) {
|
|
307
|
+
return "js";
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function shouldSkipWorkspaceTransform(filePath: string): boolean {
|
|
313
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
314
|
+
return normalized.includes("/.furin/");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function transformDevSource(
|
|
318
|
+
raw: string,
|
|
319
|
+
filePath: string,
|
|
320
|
+
options: { rewriteBareImports: boolean; rewriteRelativeImports: boolean }
|
|
321
|
+
): string {
|
|
322
|
+
const loader = getSourceLoader(filePath);
|
|
323
|
+
if (!loader) {
|
|
324
|
+
throw new Error(`[furin] Unsupported source loader for ${filePath}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const dir = dirname(filePath);
|
|
328
|
+
const sourceForTranspile = options.rewriteRelativeImports
|
|
329
|
+
? rewriteRelativeImports(raw, dir)
|
|
330
|
+
: raw;
|
|
331
|
+
const transpiler = new Bun.Transpiler({ loader });
|
|
332
|
+
const transpiled = transpiler.transformSync(sourceForTranspile, loader);
|
|
333
|
+
|
|
334
|
+
let result = transpiled;
|
|
335
|
+
if (options.rewriteBareImports) {
|
|
336
|
+
result = rewriteBareImports(raw, result, dir);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
result = rewriteSingletonImports(result);
|
|
340
|
+
return injectJsxHelperImports(result);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function registerDevPagePlugin(): void {
|
|
344
|
+
if (_pluginRegistered) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
_pluginRegistered = true;
|
|
348
|
+
Bun.plugin({
|
|
349
|
+
name: "furin-dev-page-loader",
|
|
350
|
+
setup(build) {
|
|
351
|
+
/**
|
|
352
|
+
* Strip `?furin-server` but keep `?t=<ms>` in the resolved path so
|
|
353
|
+
* each request gets a unique module identity, bypassing Bun's cache.
|
|
354
|
+
*/
|
|
355
|
+
build.onResolve({ filter: FURIN_SERVER_FILTER }, (args) => {
|
|
356
|
+
const tMatch = T_PARAM_RE.exec(args.path);
|
|
357
|
+
const filePath = args.path.replace(STRIP_FURIN_SERVER_RE, "");
|
|
358
|
+
const resolvedPath = tMatch ? `${filePath}?t=${tMatch[1]}` : filePath;
|
|
359
|
+
return { path: resolvedPath, namespace: "furin-dev-page" };
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Workspace source files (root.tsx, _route.tsx, local components, core
|
|
364
|
+
* packages, etc.) still load through Bun's normal file namespace in dev.
|
|
365
|
+
*
|
|
366
|
+
* After bun --hot re-evaluates part of that graph, those modules can
|
|
367
|
+
* re-import React through a different logical path than react-dom/server,
|
|
368
|
+
* which recreates the classic "dispatcher is null" invalid hook crash.
|
|
369
|
+
*
|
|
370
|
+
* Pre-transpile those files and rewrite only the React singleton imports
|
|
371
|
+
* so every SSR module observes the exact same React instance, while still
|
|
372
|
+
* keeping the files in Bun's watched graph for root.tsx HMR.
|
|
373
|
+
*/
|
|
374
|
+
build.onLoad({ filter: WORKSPACE_SOURCE_FILTER }, async (args) => {
|
|
375
|
+
const raw = await Bun.file(args.path).text();
|
|
376
|
+
const loader = getSourceLoader(args.path);
|
|
377
|
+
if (!loader) {
|
|
378
|
+
throw new Error(`[furin] Unsupported source loader for ${args.path}`);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Every re-evaluation of a workspace source in dev is either the
|
|
382
|
+
// initial load (cache empty → no-op) or a post-edit HMR re-read.
|
|
383
|
+
// Both are correct moments to drop dependent dev loader cache
|
|
384
|
+
// entries — they will be re-populated on the next request.
|
|
385
|
+
invalidateDevLoaderCacheBySource(args.path);
|
|
386
|
+
|
|
387
|
+
if (shouldSkipWorkspaceTransform(args.path)) {
|
|
388
|
+
return {
|
|
389
|
+
contents: raw,
|
|
390
|
+
loader,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const contents = transformDevSource(raw, args.path, {
|
|
395
|
+
rewriteBareImports: false,
|
|
396
|
+
rewriteRelativeImports: false,
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
contents,
|
|
401
|
+
loader: "js",
|
|
402
|
+
};
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Read the page file fresh from disk. The `?t=...` suffix guarantees
|
|
407
|
+
* this handler is called on every request; strip it before file I/O.
|
|
408
|
+
*
|
|
409
|
+
* ## Why we pre-transpile with Bun.Transpiler
|
|
410
|
+
*
|
|
411
|
+
* Bun's runtime plugin system (used by `bun --hot`) does NOT route
|
|
412
|
+
* sub-imports from virtual-namespace modules through `onResolve` hooks —
|
|
413
|
+
* the `namespace` filter on `onResolve` only works in the bundler context
|
|
414
|
+
* (`Bun.build()`), not in the runtime module loader. This means two
|
|
415
|
+
* classes of specifiers inside a `furin-dev-page` module cannot be
|
|
416
|
+
* intercepted after the fact:
|
|
417
|
+
*
|
|
418
|
+
* 1. **Relative imports** (`./root`, `../utils`) — no real directory
|
|
419
|
+
* context in the virtual namespace → Bun fails to resolve them.
|
|
420
|
+
* 2. **Auto-injected JSX runtime** — `import { jsx } from
|
|
421
|
+
* "react/jsx-dev-runtime"` added by Bun's TSX transpiler AFTER
|
|
422
|
+
* `onLoad` returns → cannot be rewritten in the source text.
|
|
423
|
+
*
|
|
424
|
+
* Solution: pre-transpile the source with `Bun.Transpiler` ourselves,
|
|
425
|
+
* then rewrite all specifiers in the resulting JS text before returning
|
|
426
|
+
* it with `loader: "js"` (which Bun loads as-is, with no further
|
|
427
|
+
* transpilation or automatic import injection):
|
|
428
|
+
*
|
|
429
|
+
* • `rewriteRelativeImports` → converts `./root` to `/abs/path/root`
|
|
430
|
+
* • `rewriteBareImports` → resolves ALL bare specifiers (including
|
|
431
|
+
* `@teyik0/furin/link`, `shiki`, etc.) to canonical absolute paths
|
|
432
|
+
* from the page file's directory, preventing "two React copies" after
|
|
433
|
+
* bun --hot re-evaluation.
|
|
434
|
+
* • `rewriteSingletonImports` → belt-and-suspenders for react singletons
|
|
435
|
+
* (covered by rewriteBareImports but kept for safety).
|
|
436
|
+
* • `injectJsxHelperImports` → adds the JSX helper imports that
|
|
437
|
+
* `Bun.Transpiler` emits as free variables without corresponding imports.
|
|
438
|
+
*/
|
|
439
|
+
build.onLoad({ namespace: "furin-dev-page", filter: ANY_FILTER }, async (args) => {
|
|
440
|
+
const filePath = args.path.replace(STRIP_T_PARAM_RE, "");
|
|
441
|
+
const raw = await Bun.file(filePath).text();
|
|
442
|
+
const contents = transformDevSource(raw, filePath, {
|
|
443
|
+
rewriteBareImports: true,
|
|
444
|
+
rewriteRelativeImports: true,
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
return {
|
|
448
|
+
contents,
|
|
449
|
+
loader: "js", // Already fully transpiled — skip Bun's own TSX step.
|
|
450
|
+
};
|
|
451
|
+
});
|
|
452
|
+
},
|
|
453
|
+
});
|
|
454
|
+
}
|