@teyik0/furin 0.5.0-alpha.2 → 0.7.0-alpha.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/dist/adapter/runtime-build.d.ts +3 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/entry-template.d.ts +5 -0
- package/dist/build/entry-template.ts +9 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +9589 -6685
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/request-keys.d.ts +4 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +9539 -6644
- package/dist/client/browser-events-runtime.d.ts +2 -1
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -1
- package/dist/client/dev-error-overlay.ts +1 -0
- package/dist/client/document.d.ts +3 -0
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +1011 -157
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +49 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1222 -57
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +231 -123
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +4 -2
- package/dist/furin.js +9717 -6796
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +811 -145
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-dev-page.d.ts +7 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +5 -2
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/browser-events/plugin.d.ts +3 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +6 -19
- package/dist/server/internal.d.ts +5 -0
- package/dist/server/internal.js +105 -57
- package/dist/server/production-server.js +104 -56
- package/dist/server/render/assemble.d.ts +6 -6
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +15 -5
- package/dist/server/render/mixed-cache.d.ts +6 -0
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/patterns.d.ts +3 -0
- package/dist/server/router/plugin.d.ts +3 -0
- package/dist/server/router/types.d.ts +4 -0
- package/dist/server/security/csp.d.ts +12 -0
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +580 -124
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +42 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +40 -16
- package/src/adapter/README.md +3 -0
- package/src/adapter/bun.ts +3 -0
- package/src/adapter/package.ts +16 -2
- package/src/adapter/runtime-build.ts +99 -14
- package/src/adapter/static.ts +17 -6
- package/src/adapter/vercel.ts +33 -23
- package/src/build/client.ts +23 -5
- package/src/build/entry-template.ts +9 -0
- package/src/build/hydrate.ts +18 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/request-keys.ts +93 -0
- package/src/build/ssg-cache.ts +9 -4
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +1 -1
- package/src/client/browser-events-runtime.ts +73 -16
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -2
- package/src/client/dev-error-overlay.ts +1 -0
- package/src/client/document.tsx +47 -20
- package/src/client/hmr.ts +4 -2
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +10 -6
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +404 -96
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +71 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +442 -155
- package/src/env.d.ts +2 -2
- package/src/furin.ts +213 -38
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/route-config-autofix.ts +2 -2
- package/src/plugin/routes.ts +19 -8
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-dev-page.ts +231 -0
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/rsc/version.ts +5 -0
- package/src/server/auto-invalidate/registry.ts +56 -24
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/browser-events/plugin.ts +112 -1
- package/src/server/cache/dev-loader.ts +1 -1
- package/src/server/cache/invalidation.ts +2 -0
- package/src/server/cache/isr-ssg.ts +1 -1
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/cache/ssg.ts +1 -1
- package/src/server/dev/diagnostics.ts +4 -2
- package/src/server/dev/error.ts +2 -1
- package/src/server/dev/graph.ts +70 -7
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/symbolicate.ts +2 -0
- package/src/server/dev-page-plugin.ts +160 -63
- package/src/server/evlog.ts +1 -1
- package/src/server/internal.ts +5 -0
- package/src/server/render/assemble.ts +15 -11
- package/src/server/render/document.tsx +13 -2
- package/src/server/render/element.tsx +11 -6
- package/src/server/render/isr.ts +71 -33
- package/src/server/render/loaders.ts +328 -70
- package/src/server/render/mixed-cache.ts +327 -0
- package/src/server/render/not-found.ts +14 -11
- package/src/server/render/ppr-document.ts +47 -11
- package/src/server/render/ppr-route.ts +54 -20
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +53 -19
- package/src/server/render/ssr.ts +105 -36
- package/src/server/render/static-params.ts +1 -1
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/discovery.ts +14 -0
- package/src/server/router/hmr.ts +109 -18
- package/src/server/router/patterns.ts +27 -0
- package/src/server/router/plugin.ts +94 -19
- package/src/server/router/source-version.ts +20 -2
- package/src/server/router/types.ts +4 -0
- package/src/server/security/csp.ts +44 -0
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +60 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { walk } from "yuku-ast";
|
|
5
|
+
import type { CallExpression, ImportDeclaration, Program } from "yuku-parser";
|
|
6
|
+
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
7
|
+
import { parseSource } from "../shared/parser.ts";
|
|
8
|
+
import type { AstNode } from "../shared/utils/ast-walk.ts";
|
|
9
|
+
import { hasShadowingDeclaration } from "./binding-scope.ts";
|
|
10
|
+
import type { IsomorphicEnvironment } from "./transform-isomorphic.ts";
|
|
11
|
+
|
|
12
|
+
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
13
|
+
const SERVER_LOAD =
|
|
14
|
+
'() => Promise.reject(new Error("[furin] clientModule() can only be loaded in the browser."))';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Stable key shared by the `clientModule()` transform and the client build's
|
|
18
|
+
* preload manifest. It is an opaque ASCII token so it survives minification
|
|
19
|
+
* unchanged and can be swapped for the module's chunk URLs after the build.
|
|
20
|
+
*/
|
|
21
|
+
export function clientModuleKey(modulePath: string): string {
|
|
22
|
+
const realPath = realpathSync(modulePath).replaceAll("\\", "/");
|
|
23
|
+
return `__FURIN_CLIENT_MODULE_${Bun.hash(realPath).toString(36)}__`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function collectClientModuleBindings(program: Program): Set<string> {
|
|
27
|
+
const bindings = new Set<string>();
|
|
28
|
+
for (const statement of program.body) {
|
|
29
|
+
if (statement.type !== "ImportDeclaration") {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const declaration = statement as unknown as ImportDeclaration;
|
|
33
|
+
if (
|
|
34
|
+
declaration.importKind === "type" ||
|
|
35
|
+
!FURIN_CLIENT_MODULES.has(String(declaration.source.value))
|
|
36
|
+
) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
40
|
+
const imported = specifier.imported as AstNode | undefined;
|
|
41
|
+
const local = specifier.local as AstNode | undefined;
|
|
42
|
+
if (
|
|
43
|
+
specifier.type === "ImportSpecifier" &&
|
|
44
|
+
specifier.importKind !== "type" &&
|
|
45
|
+
imported?.name === "clientModule" &&
|
|
46
|
+
typeof local?.name === "string"
|
|
47
|
+
) {
|
|
48
|
+
bindings.add(local.name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return bindings;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Returns the `import("…")` specifier of a `() => import("…")` loader. */
|
|
56
|
+
function dynamicImportSpecifier(loader: AstNode): string | null {
|
|
57
|
+
if (loader.type !== "ArrowFunctionExpression" || (loader.params as unknown[]).length > 0) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const body = loader.body as unknown as AstNode;
|
|
61
|
+
const source = body.type === "ImportExpression" ? (body.source as AstNode) : null;
|
|
62
|
+
return source?.type === "Literal" && typeof source.value === "string" ? source.value : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Rewrites `clientModule(() => import("./x"))` calls imported from
|
|
67
|
+
* `@teyik0/furin/client`:
|
|
68
|
+
* - client: keeps the loader and appends the module key, which the production
|
|
69
|
+
* client build replaces with the module's chunk URLs;
|
|
70
|
+
* - server: drops the `import()` so the module is never bundled server-side and
|
|
71
|
+
* passes the key, resolved against the preload manifest at render time.
|
|
72
|
+
*/
|
|
73
|
+
export function transformClientModules(
|
|
74
|
+
source: string,
|
|
75
|
+
filename: string,
|
|
76
|
+
environment: IsomorphicEnvironment
|
|
77
|
+
): string {
|
|
78
|
+
if (!source.includes("clientModule")) {
|
|
79
|
+
return source;
|
|
80
|
+
}
|
|
81
|
+
const { program } = parseSource(source, detectLangFromPath(filename));
|
|
82
|
+
const bindings = collectClientModuleBindings(program);
|
|
83
|
+
if (bindings.size === 0) {
|
|
84
|
+
return source;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const transformed = new MagicString(source);
|
|
88
|
+
walk(program, {
|
|
89
|
+
CallExpression(node, context) {
|
|
90
|
+
const call = node as unknown as CallExpression;
|
|
91
|
+
const [loader, ...rest] = call.arguments as unknown as AstNode[];
|
|
92
|
+
if (
|
|
93
|
+
call.callee.type !== "Identifier" ||
|
|
94
|
+
!bindings.has(call.callee.name) ||
|
|
95
|
+
hasShadowingDeclaration(call.callee.name, context.ancestors() as AstNode[]) ||
|
|
96
|
+
!loader ||
|
|
97
|
+
rest.length > 0
|
|
98
|
+
) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const unwrapped = unwrapTSExpression(loader) as AstNode;
|
|
102
|
+
const specifier = dynamicImportSpecifier(unwrapped);
|
|
103
|
+
if (specifier === null) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
let key: string;
|
|
107
|
+
try {
|
|
108
|
+
key = JSON.stringify(clientModuleKey(Bun.resolveSync(specifier, dirname(filename))));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`[furin] Cannot resolve clientModule import ${JSON.stringify(specifier)} in ${filename}`,
|
|
112
|
+
{ cause: error }
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (environment === "client") {
|
|
116
|
+
transformed.appendLeft(loader.end, `, ${key}`);
|
|
117
|
+
} else {
|
|
118
|
+
transformed.overwrite(loader.start, loader.end, `${SERVER_LOAD}, ${key}`);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
return transformed.toString();
|
|
123
|
+
}
|
|
@@ -6,6 +6,7 @@ import { parseSource } from "../shared/parser.ts";
|
|
|
6
6
|
import type { AstNode } from "../shared/utils/ast-walk.ts";
|
|
7
7
|
import { hasShadowingDeclaration } from "./binding-scope.ts";
|
|
8
8
|
import { deadCodeElimination } from "./dead-code-elimination.ts";
|
|
9
|
+
import { hmrDependencySignature } from "./hmr-dependencies.ts";
|
|
9
10
|
import { transformIsomorphicFunctions } from "./transform-isomorphic.ts";
|
|
10
11
|
|
|
11
12
|
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
@@ -14,6 +15,7 @@ const REACT_COMPONENT_WRAPPERS = new Set(["forwardRef", "memo"]);
|
|
|
14
15
|
const SERVER_ONLY_METHODS = new Set(["config", "head", "loader", "requestLoader", "staticParams"]);
|
|
15
16
|
const REACT_HOOK_NAME_RE = /^use[A-Z0-9]/;
|
|
16
17
|
const HMR_DATA_SIGNATURE = "furin.hmr.data-signature";
|
|
18
|
+
const HMR_SOURCE_SIGNATURE = "furin.hmr.source-signature";
|
|
17
19
|
const TYPESCRIPT_RUNTIME_WRAPPERS = new Map([
|
|
18
20
|
["TSAsExpression", ["expression"]],
|
|
19
21
|
["TSInstantiationExpression", ["expression"]],
|
|
@@ -159,12 +161,79 @@ function chainRootIsDefineRoute(
|
|
|
159
161
|
return false;
|
|
160
162
|
}
|
|
161
163
|
|
|
164
|
+
function assertCompleteRouteChains(
|
|
165
|
+
program: Program,
|
|
166
|
+
bindings: Set<string>,
|
|
167
|
+
filename: string
|
|
168
|
+
): void {
|
|
169
|
+
const namespaces = new Set<string>();
|
|
170
|
+
for (const statement of program.body) {
|
|
171
|
+
if (statement.type !== "ImportDeclaration") {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const declaration = statement as unknown as ImportDeclaration;
|
|
175
|
+
if (!isFurinRouteModule(declaration.source.value) || declaration.importKind === "type") {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
179
|
+
const name = localName(specifier);
|
|
180
|
+
if (name && specifier.type === "ImportNamespaceSpecifier") {
|
|
181
|
+
namespaces.add(name);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
walk(program, {
|
|
186
|
+
MemberExpression(node, context) {
|
|
187
|
+
const object = asAstNode(node.object);
|
|
188
|
+
const property = asAstNode(node.property);
|
|
189
|
+
const method = property?.type === "Identifier" ? property.name : property?.value;
|
|
190
|
+
if (
|
|
191
|
+
object?.type === "Identifier" &&
|
|
192
|
+
typeof object.name === "string" &&
|
|
193
|
+
namespaces.has(object.name) &&
|
|
194
|
+
(method === "defineRoute" || method === "defineRootRoute") &&
|
|
195
|
+
!hasShadowingDeclaration(object.name, context.ancestors() as AstNode[])
|
|
196
|
+
) {
|
|
197
|
+
throw new Error(`[furin] ${filename}: route builders require a named import.`);
|
|
198
|
+
}
|
|
199
|
+
if (
|
|
200
|
+
node.computed &&
|
|
201
|
+
context.parent?.type === "CallExpression" &&
|
|
202
|
+
chainRootIsDefineRoute(node.object, bindings, context.ancestors() as AstNode[])
|
|
203
|
+
) {
|
|
204
|
+
throw new Error(`[furin] ${filename}: route builders require static builder methods.`);
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
VariableDeclarator(node, context) {
|
|
208
|
+
const initializer = asAstNode(node.init);
|
|
209
|
+
if (
|
|
210
|
+
!(
|
|
211
|
+
initializer &&
|
|
212
|
+
chainRootIsDefineRoute(initializer, bindings, context.ancestors() as AstNode[])
|
|
213
|
+
)
|
|
214
|
+
) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const callee = initializer.type === "CallExpression" ? asAstNode(initializer.callee) : null;
|
|
218
|
+
const method = callee?.type === "MemberExpression" ? asAstNode(callee.property) : null;
|
|
219
|
+
if (method?.type !== "Identifier" || (method.name !== "page" && method.name !== "layout")) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`[furin] ${filename}: route builders must use one fluent chain ending in .page() or .layout().`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
162
228
|
function removeChainedServerCalls(
|
|
163
229
|
source: MagicString,
|
|
164
230
|
program: Program,
|
|
165
|
-
bindings: Set<string
|
|
231
|
+
bindings: Set<string>,
|
|
232
|
+
configBindings: Set<string>,
|
|
233
|
+
filename: string
|
|
166
234
|
): boolean {
|
|
167
235
|
let transformed = rewriteClientImports(source, program, bindings);
|
|
236
|
+
const { declarations } = collectModuleBindings(program);
|
|
168
237
|
|
|
169
238
|
walk(program, {
|
|
170
239
|
CallExpression(call, context) {
|
|
@@ -190,6 +259,28 @@ function removeChainedServerCalls(
|
|
|
190
259
|
if (!object) {
|
|
191
260
|
return;
|
|
192
261
|
}
|
|
262
|
+
if (property.name === "config" && Array.isArray(call.arguments)) {
|
|
263
|
+
const config = asAstNode(call.arguments[0]);
|
|
264
|
+
if (config?.type === "ObjectExpression" && Array.isArray(config.properties)) {
|
|
265
|
+
collectConfigBindings(config, declarations, configBindings);
|
|
266
|
+
const remount = resolveRemountProperty(
|
|
267
|
+
config,
|
|
268
|
+
declarations,
|
|
269
|
+
configBindings,
|
|
270
|
+
new Set(),
|
|
271
|
+
filename
|
|
272
|
+
);
|
|
273
|
+
if (remount) {
|
|
274
|
+
source.update(
|
|
275
|
+
object.end,
|
|
276
|
+
call.end,
|
|
277
|
+
`.config({ ${source.original.slice(remount.start, remount.end)} })`
|
|
278
|
+
);
|
|
279
|
+
transformed = true;
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
193
284
|
source.remove(object.end, call.end);
|
|
194
285
|
transformed = true;
|
|
195
286
|
},
|
|
@@ -197,6 +288,128 @@ function removeChainedServerCalls(
|
|
|
197
288
|
return transformed;
|
|
198
289
|
}
|
|
199
290
|
|
|
291
|
+
function collectConfigBindings(
|
|
292
|
+
config: AstNode | null,
|
|
293
|
+
declarations: Map<string, AstNode>,
|
|
294
|
+
names: Set<string>
|
|
295
|
+
): void {
|
|
296
|
+
if (config?.type === "Identifier" && typeof config.name === "string" && !names.has(config.name)) {
|
|
297
|
+
names.add(config.name);
|
|
298
|
+
const declaration = declarations.get(config.name);
|
|
299
|
+
collectConfigBindings(asAstNode(declaration?.init), declarations, names);
|
|
300
|
+
} else if (config?.type === "ObjectExpression" && Array.isArray(config.properties)) {
|
|
301
|
+
for (const property of config.properties) {
|
|
302
|
+
const entry = asAstNode(property);
|
|
303
|
+
if (entry?.type === "SpreadElement") {
|
|
304
|
+
collectConfigBindings(asAstNode(entry.argument), declarations, names);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function resolveRemountProperty(
|
|
311
|
+
config: AstNode | null,
|
|
312
|
+
declarations: Map<string, AstNode>,
|
|
313
|
+
configBindings: Set<string>,
|
|
314
|
+
visiting: Set<string>,
|
|
315
|
+
filename: string
|
|
316
|
+
): AstNode | null {
|
|
317
|
+
if (
|
|
318
|
+
config?.type === "Identifier" &&
|
|
319
|
+
typeof config.name === "string" &&
|
|
320
|
+
!visiting.has(config.name)
|
|
321
|
+
) {
|
|
322
|
+
visiting.add(config.name);
|
|
323
|
+
configBindings.add(config.name);
|
|
324
|
+
const declaration = declarations.get(config.name);
|
|
325
|
+
const result = resolveRemountProperty(
|
|
326
|
+
asAstNode(declaration?.init),
|
|
327
|
+
declarations,
|
|
328
|
+
configBindings,
|
|
329
|
+
visiting,
|
|
330
|
+
filename
|
|
331
|
+
);
|
|
332
|
+
visiting.delete(config.name);
|
|
333
|
+
return result;
|
|
334
|
+
}
|
|
335
|
+
if (config?.type !== "ObjectExpression" || !Array.isArray(config.properties)) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`[furin] ${filename}: route config spreads must resolve to local object literals; declare remountDeps after dynamic spreads.`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
for (let index = config.properties.length - 1; index >= 0; index -= 1) {
|
|
341
|
+
const entry = asAstNode(config.properties[index]);
|
|
342
|
+
if (entry?.type === "SpreadElement") {
|
|
343
|
+
const remount = resolveRemountProperty(
|
|
344
|
+
asAstNode(entry.argument),
|
|
345
|
+
declarations,
|
|
346
|
+
configBindings,
|
|
347
|
+
visiting,
|
|
348
|
+
filename
|
|
349
|
+
);
|
|
350
|
+
if (remount) {
|
|
351
|
+
return remount;
|
|
352
|
+
}
|
|
353
|
+
} else if (entry?.type === "Property") {
|
|
354
|
+
const key = asAstNode(entry.key);
|
|
355
|
+
if (
|
|
356
|
+
(key?.type === "Identifier" && entry.computed !== true && key.name === "remountDeps") ||
|
|
357
|
+
(key?.type === "Literal" && key.value === "remountDeps")
|
|
358
|
+
) {
|
|
359
|
+
return entry;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function pruneUnusedConfigBindings(
|
|
367
|
+
source: MagicString,
|
|
368
|
+
filename: string,
|
|
369
|
+
names: Set<string>
|
|
370
|
+
): MagicString {
|
|
371
|
+
let result = source;
|
|
372
|
+
let pruned = true;
|
|
373
|
+
while (pruned) {
|
|
374
|
+
pruned = false;
|
|
375
|
+
const code = result.toString();
|
|
376
|
+
const { program } = parseSource(code, detectLangFromPath(filename));
|
|
377
|
+
const references = new Set<string>();
|
|
378
|
+
walkWithAncestors(program, [], (node, ancestors) => {
|
|
379
|
+
if (
|
|
380
|
+
node.type === "Identifier" &&
|
|
381
|
+
typeof node.name === "string" &&
|
|
382
|
+
isReferenceIdentifier(node, ancestors)
|
|
383
|
+
) {
|
|
384
|
+
references.add(node.name);
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
result = new MagicString(code);
|
|
388
|
+
for (const statement of program.body) {
|
|
389
|
+
if (statement.type !== "VariableDeclaration") {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const retained = statement.declarations.filter(
|
|
393
|
+
(declaration) =>
|
|
394
|
+
declaration.id.type !== "Identifier" ||
|
|
395
|
+
!names.has(declaration.id.name) ||
|
|
396
|
+
references.has(declaration.id.name)
|
|
397
|
+
);
|
|
398
|
+
if (retained.length !== statement.declarations.length) {
|
|
399
|
+
result.update(
|
|
400
|
+
statement.start,
|
|
401
|
+
statement.end,
|
|
402
|
+
retained.length
|
|
403
|
+
? `${statement.kind} ${retained.map((node) => code.slice(node.start, node.end)).join(", ")};`
|
|
404
|
+
: ""
|
|
405
|
+
);
|
|
406
|
+
pruned = true;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
}
|
|
412
|
+
|
|
200
413
|
function collectBindingNames(pattern: unknown, names: string[]): void {
|
|
201
414
|
const node = asAstNode(pattern);
|
|
202
415
|
if (!node) {
|
|
@@ -256,16 +469,16 @@ function collectDeclarationBindings(
|
|
|
256
469
|
|
|
257
470
|
function collectModuleBindings(program: Program): {
|
|
258
471
|
declarations: Map<string, AstNode>;
|
|
259
|
-
imports:
|
|
472
|
+
imports: Map<string, ImportDeclaration>;
|
|
260
473
|
} {
|
|
261
474
|
const declarations = new Map<string, AstNode>();
|
|
262
|
-
const imports = new
|
|
475
|
+
const imports = new Map<string, ImportDeclaration>();
|
|
263
476
|
for (const statement of program.body as unknown as AstNode[]) {
|
|
264
477
|
if (statement.type === "ImportDeclaration" && Array.isArray(statement.specifiers)) {
|
|
265
478
|
for (const specifier of statement.specifiers as AstNode[]) {
|
|
266
479
|
const local = localName(specifier);
|
|
267
480
|
if (local) {
|
|
268
|
-
imports.
|
|
481
|
+
imports.set(local, statement as unknown as ImportDeclaration);
|
|
269
482
|
}
|
|
270
483
|
}
|
|
271
484
|
continue;
|
|
@@ -399,7 +612,8 @@ function walkWithAncestors(
|
|
|
399
612
|
|
|
400
613
|
interface HmrDependencyState {
|
|
401
614
|
dependencies: Map<number, AstNode>;
|
|
402
|
-
|
|
615
|
+
dynamicImports: Set<string>;
|
|
616
|
+
imports: Set<ImportDeclaration>;
|
|
403
617
|
importTrackedDependencies: Set<number>;
|
|
404
618
|
moduleBindings: ReturnType<typeof collectModuleBindings>;
|
|
405
619
|
}
|
|
@@ -410,8 +624,16 @@ function collectDependencyIdentifier(
|
|
|
410
624
|
trackImports: boolean,
|
|
411
625
|
state: HmrDependencyState
|
|
412
626
|
): void {
|
|
627
|
+
const parent = ancestors.at(-1);
|
|
628
|
+
const jsxReference =
|
|
629
|
+
child.type === "JSXIdentifier" &&
|
|
630
|
+
((parent?.type === "JSXOpeningElement" &&
|
|
631
|
+
parent.name === child &&
|
|
632
|
+
typeof child.name === "string" &&
|
|
633
|
+
child.name[0] === child.name[0]?.toUpperCase()) ||
|
|
634
|
+
(parent?.type === "JSXMemberExpression" && parent.object === child));
|
|
413
635
|
if (
|
|
414
|
-
child.type
|
|
636
|
+
!(child.type === "Identifier" || jsxReference) ||
|
|
415
637
|
typeof child.name !== "string" ||
|
|
416
638
|
!isReferenceIdentifier(child, ancestors) ||
|
|
417
639
|
hasShadowingDeclaration(child.name, ancestors)
|
|
@@ -420,8 +642,9 @@ function collectDependencyIdentifier(
|
|
|
420
642
|
}
|
|
421
643
|
const declaration = state.moduleBindings.declarations.get(child.name);
|
|
422
644
|
if (!declaration) {
|
|
423
|
-
|
|
424
|
-
|
|
645
|
+
const imported = state.moduleBindings.imports.get(child.name);
|
|
646
|
+
if (trackImports && imported) {
|
|
647
|
+
state.imports.add(imported);
|
|
425
648
|
}
|
|
426
649
|
return;
|
|
427
650
|
}
|
|
@@ -447,14 +670,26 @@ function collectDependencies(
|
|
|
447
670
|
): void {
|
|
448
671
|
walkWithAncestors(node, [], (child, ancestors) => {
|
|
449
672
|
collectDependencyIdentifier(child, ancestors, trackImports, state);
|
|
673
|
+
if (trackImports && child.type === "ImportExpression") {
|
|
674
|
+
const specifier = asAstNode(child.source)?.value;
|
|
675
|
+
if (typeof specifier === "string") {
|
|
676
|
+
state.dynamicImports.add(specifier);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
450
679
|
});
|
|
451
680
|
}
|
|
452
681
|
|
|
453
|
-
function createHmrDataSignature(
|
|
682
|
+
function createHmrDataSignature(
|
|
683
|
+
code: string,
|
|
684
|
+
program: Program,
|
|
685
|
+
bindings: Set<string>,
|
|
686
|
+
filename: string
|
|
687
|
+
): string {
|
|
454
688
|
const serverStages: Array<{ source: string; start: number }> = [];
|
|
455
689
|
const dependencyState: HmrDependencyState = {
|
|
456
690
|
dependencies: new Map(),
|
|
457
|
-
|
|
691
|
+
dynamicImports: new Set(),
|
|
692
|
+
imports: new Set(),
|
|
458
693
|
importTrackedDependencies: new Set(),
|
|
459
694
|
moduleBindings: collectModuleBindings(program),
|
|
460
695
|
};
|
|
@@ -500,6 +735,10 @@ function createHmrDataSignature(code: string, program: Program, bindings: Set<st
|
|
|
500
735
|
|
|
501
736
|
const dataSource = [
|
|
502
737
|
...serverStages,
|
|
738
|
+
...[...dependencyState.imports].map((imported) => ({
|
|
739
|
+
source: code.slice(imported.start, imported.end),
|
|
740
|
+
start: imported.start,
|
|
741
|
+
})),
|
|
503
742
|
...[...dependencyState.dependencies.values()].map((dependency) => ({
|
|
504
743
|
source: code.slice(dependency.start, dependency.end),
|
|
505
744
|
start: dependency.start,
|
|
@@ -509,7 +748,27 @@ function createHmrDataSignature(code: string, program: Program, bindings: Set<st
|
|
|
509
748
|
.map((entry) => entry.source)
|
|
510
749
|
.join("\n");
|
|
511
750
|
const hash = new Bun.CryptoHasher("sha256").update(dataSource).digest("hex");
|
|
512
|
-
|
|
751
|
+
if (dependencyState.imports.size === 0 && dependencyState.dynamicImports.size === 0) {
|
|
752
|
+
return hash;
|
|
753
|
+
}
|
|
754
|
+
const dependencies = hmrDependencySignature(
|
|
755
|
+
new Set([
|
|
756
|
+
...[...dependencyState.imports].map((imported) => imported.source.value),
|
|
757
|
+
...dependencyState.dynamicImports,
|
|
758
|
+
]),
|
|
759
|
+
filename
|
|
760
|
+
);
|
|
761
|
+
return dependencies === undefined ? `external:${hash}` : `imports:${hash}:${dependencies}`;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/** Server stages and their imports share one signature across both HMR graphs. */
|
|
765
|
+
export function getHmrDataSignature(code: string, filename: string): string {
|
|
766
|
+
const { diagnostics, program } = parseSource(code, detectLangFromPath(filename));
|
|
767
|
+
const error = diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
768
|
+
if (error) {
|
|
769
|
+
throw new Error(`Failed to parse ${filename}: ${error.message}`);
|
|
770
|
+
}
|
|
771
|
+
return createHmrDataSignature(code, program, collectDefineRouteBindings(program), filename);
|
|
513
772
|
}
|
|
514
773
|
|
|
515
774
|
function calledHookName(call: AstNode): string | null {
|
|
@@ -837,9 +1096,20 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
837
1096
|
|
|
838
1097
|
let source = new MagicString(clientSource);
|
|
839
1098
|
const routeBindings = collectDefineRouteBindings(program);
|
|
840
|
-
|
|
1099
|
+
assertCompleteRouteChains(program, routeBindings, filename);
|
|
1100
|
+
const configBindings = new Set<string>();
|
|
1101
|
+
const removedRouteCode = removeChainedServerCalls(
|
|
1102
|
+
source,
|
|
1103
|
+
program,
|
|
1104
|
+
routeBindings,
|
|
1105
|
+
configBindings,
|
|
1106
|
+
filename
|
|
1107
|
+
);
|
|
841
1108
|
const removedServerCode = isomorphicResult.transformed || removedRouteCode;
|
|
842
1109
|
if (removedServerCode) {
|
|
1110
|
+
if (configBindings.size > 0) {
|
|
1111
|
+
source = pruneUnusedConfigBindings(source, filename, configBindings);
|
|
1112
|
+
}
|
|
843
1113
|
source = deadCodeElimination(source, code, lang);
|
|
844
1114
|
}
|
|
845
1115
|
const transformedCode = source.toString();
|
|
@@ -867,7 +1137,8 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
867
1137
|
const hmrDataSignature = createHmrDataSignature(
|
|
868
1138
|
code,
|
|
869
1139
|
originalParse.program,
|
|
870
|
-
collectDefineRouteBindings(originalParse.program)
|
|
1140
|
+
collectDefineRouteBindings(originalParse.program),
|
|
1141
|
+
filename
|
|
871
1142
|
);
|
|
872
1143
|
const signatureValue = Array.isArray(hookSignature)
|
|
873
1144
|
? JSON.stringify(hookSignature)
|
|
@@ -875,6 +1146,11 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
875
1146
|
source.append(`
|
|
876
1147
|
if (import.meta.hot && route?.component) {
|
|
877
1148
|
const previousDataSignature = ${JSON.stringify(hmrDataSignature)};
|
|
1149
|
+
const previousSourceSignature = ${JSON.stringify(Bun.hash(code).toString(16))};
|
|
1150
|
+
Object.defineProperty(route, Symbol.for(${JSON.stringify(HMR_SOURCE_SIGNATURE)}), {
|
|
1151
|
+
configurable: true,
|
|
1152
|
+
value: previousSourceSignature,
|
|
1153
|
+
});
|
|
878
1154
|
Object.defineProperty(route, Symbol.for(${JSON.stringify(HMR_DATA_SIGNATURE)}), {
|
|
879
1155
|
configurable: true,
|
|
880
1156
|
value: previousDataSignature,
|
|
@@ -890,10 +1166,14 @@ if (import.meta.hot && route?.component) {
|
|
|
890
1166
|
updatedRoute,
|
|
891
1167
|
Symbol.for(${JSON.stringify(HMR_DATA_SIGNATURE)})
|
|
892
1168
|
);
|
|
1169
|
+
// An unchanged source can be re-evaluated by Bun after an import edit
|
|
1170
|
+
// without rerunning this transform. Its embedded dependency hash is stale.
|
|
893
1171
|
const dataChanged =
|
|
894
1172
|
previousDataSignature.startsWith("external:") ||
|
|
895
1173
|
typeof updatedDataSignature !== "string" ||
|
|
896
1174
|
updatedDataSignature.startsWith("external:") ||
|
|
1175
|
+
(updatedDataSignature.startsWith("imports:") &&
|
|
1176
|
+
Reflect.get(updatedRoute, Symbol.for(${JSON.stringify(HMR_SOURCE_SIGNATURE)})) === previousSourceSignature) ||
|
|
897
1177
|
updatedDataSignature !== previousDataSignature;
|
|
898
1178
|
window.__FURIN_HMR_UPDATE__?.(${JSON.stringify(filename)}, updatedRoute.component, dataChanged);
|
|
899
1179
|
}
|