@teyik0/furin 0.2.0-alpha.5 → 0.3.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/README.md +2 -3
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/entry-template.d.ts +2 -0
- package/dist/build/entry-template.ts +3 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +6944 -5067
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/types.d.ts +1 -0
- package/dist/cli/index.js +7874 -5971
- package/dist/client/boundaries.d.ts +4 -4
- package/dist/client/internal/runtime-types.d.ts +30 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +712 -338
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +5 -1
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +2 -2
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +108 -111
- package/dist/client.js +16 -17
- package/dist/config.d.ts +1 -1
- package/dist/config.js +2 -2
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +7 -6
- package/dist/furin.js +6547 -3187
- package/dist/isomorphic.d.ts +18 -0
- package/dist/plugin/binding-scope.d.ts +2 -0
- package/dist/plugin/dead-code-elimination.d.ts +3 -0
- package/dist/plugin/index.js +933 -423
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -0
- package/dist/rsc/build/environment.d.ts +2 -0
- package/dist/rsc/build/index.d.ts +15 -0
- package/dist/rsc/client-codec.d.ts +1 -0
- package/dist/rsc/codec.d.ts +4 -0
- package/dist/rsc/flight-drain.d.ts +11 -0
- package/dist/rsc/render-error.d.ts +17 -0
- package/dist/rsc/server-codec.d.ts +2 -0
- package/dist/rsc/server-codec.js +24 -0
- package/dist/rsc/shared.d.ts +30 -0
- package/dist/rsc/symbols.d.ts +2 -0
- package/dist/rsc/transport-benchmark.d.ts +13 -0
- package/dist/rsc/version.d.ts +7 -0
- package/dist/rsc-client.d.ts +4 -0
- package/dist/rsc-client.js +166 -0
- package/dist/rsc-server.d.ts +11 -0
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +353 -0
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +2 -3
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +9 -0
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/store-view.d.ts +7 -7
- package/dist/server/dev-page-plugin.d.ts +4 -0
- package/dist/server/devtools/hub.d.ts +11 -0
- package/dist/server/devtools/instrumentation.d.ts +22 -0
- package/dist/server/devtools/instrumentation.production.d.ts +44 -0
- package/dist/server/devtools/plugin.d.ts +3 -0
- package/dist/server/devtools/request-context.d.ts +6 -0
- package/dist/server/internal.d.ts +2 -0
- package/dist/server/internal.js +3 -3
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/render/assemble.d.ts +4 -0
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -2
- package/dist/server/render/loaders.d.ts +8 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-route.d.ts +8 -0
- package/dist/server/render/route-frame-transport.d.ts +2 -0
- package/dist/server/render/shell.ts +0 -1
- package/dist/server/render/ssr.d.ts +2 -3
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/index.d.ts +2 -1
- package/dist/server/router/index.js +23 -23
- package/dist/server/router/index.ts +1 -1
- package/dist/server/router/patterns.d.ts +1 -1
- package/dist/server/router/plugin.d.ts +4 -7
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +12 -7
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/runtime-env.js +2 -2
- package/dist/server/sync/adapter.d.ts +45 -26
- package/dist/server/sync/config.d.ts +5 -9
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +2868 -0
- package/dist/server/sync/notifier.d.ts +18 -0
- package/dist/server/sync/plugin.d.ts +53 -54
- package/dist/server/sync/postgres/index.d.ts +19 -0
- package/dist/server/sync/postgres/index.js +252 -0
- package/dist/server/sync/postgres/migrate.d.ts +2 -0
- package/dist/server/sync/postgres/migrate.js +16 -0
- package/dist/server/sync/postgres/migration.sql +66 -0
- package/dist/server/sync/redis/index.d.ts +30 -0
- package/dist/server/sync/redis/index.js +336 -0
- package/dist/server/sync/redis/scripts.d.ts +4 -0
- package/dist/server/sync/response.d.ts +10 -1
- package/dist/server/sync/runtime.d.ts +6 -0
- package/dist/server/sync/sqlite/index.d.ts +20 -0
- package/dist/server/sync/sqlite/index.js +250 -0
- package/dist/server/sync/stream.d.ts +12 -11
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/utils/index.d.ts +1 -1
- package/package.json +54 -12
- package/src/adapter/bun.ts +73 -16
- package/src/adapter/package.ts +3 -1
- package/src/adapter/static.ts +94 -48
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +22 -24
- package/src/build/entry-template.ts +3 -0
- package/src/build/hydrate.ts +33 -8
- package/src/build/index.ts +12 -0
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +7 -8
- package/src/build/scan-server.ts +3 -2
- package/src/build/ssg-cache.ts +7 -2
- package/src/build/types.ts +1 -0
- package/src/cli/index.ts +53 -22
- package/src/client/boundaries.tsx +42 -18
- package/src/client/default-screens.tsx +4 -4
- package/src/client/internal/runtime-types.ts +31 -0
- package/src/client/link.tsx +35 -37
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +66 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +74 -52
- package/src/client/router/search/index.ts +5 -7
- package/src/client/router/search-store.ts +5 -1
- package/src/client/router/spa-response.ts +3 -4
- package/src/client/router/sync-catch-up.ts +9 -8
- package/src/client/router/types.ts +2 -2
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +221 -174
- package/src/devtools/devtools-element.js +884 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +185 -96
- package/src/isomorphic.ts +66 -0
- package/src/plugin/binding-scope.ts +158 -0
- package/src/plugin/dead-code-elimination.ts +202 -0
- package/src/plugin/index.ts +14 -18
- package/src/plugin/transform-client.ts +237 -273
- package/src/plugin/transform-isomorphic.ts +464 -0
- package/src/rsc/build/environment.ts +30 -0
- package/src/rsc/build/index.ts +94 -0
- package/src/rsc/client-codec.ts +11 -0
- package/src/rsc/codec.ts +85 -0
- package/src/rsc/flight-drain.ts +68 -0
- package/src/rsc/react-server-dom-webpack.d.ts +23 -0
- package/src/rsc/render-error.ts +120 -0
- package/src/rsc/server-codec.ts +25 -0
- package/src/rsc/shared.tsx +113 -0
- package/src/rsc/symbols.ts +2 -0
- package/src/rsc/transport-benchmark.ts +73 -0
- package/src/rsc/version.ts +57 -0
- package/src/rsc-client.ts +12 -0
- package/src/rsc-server.tsx +91 -0
- package/src/rsc.tsx +75 -0
- package/src/server/auto-invalidate/runtime.ts +32 -6
- package/src/server/cache/dev-loader.ts +70 -10
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +33 -15
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +84 -9
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/store-view.ts +7 -7
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +8 -6
- package/src/server/devtools/hub.ts +64 -0
- package/src/server/devtools/instrumentation.production.ts +80 -0
- package/src/server/devtools/instrumentation.ts +72 -0
- package/src/server/devtools/plugin.ts +267 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +1 -1
- package/src/server/internal.ts +2 -0
- package/src/server/lang-detect.ts +14 -10
- package/src/server/render/assemble.ts +73 -10
- package/src/server/render/element.tsx +11 -8
- package/src/server/render/index.ts +6 -2
- package/src/server/render/isr.ts +154 -110
- package/src/server/render/loaders.ts +259 -45
- package/src/server/render/not-found.ts +10 -15
- package/src/server/render/ppr-route.ts +146 -0
- package/src/server/render/route-frame-transport.ts +62 -0
- package/src/server/render/shell.ts +0 -1
- package/src/server/render/ssg.ts +6 -5
- package/src/server/render/ssr.ts +155 -67
- package/src/server/render/template.ts +2 -1
- package/src/server/router/discovery.ts +39 -22
- package/src/server/router/hmr.ts +15 -13
- package/src/server/router/index.ts +1 -1
- package/src/server/router/patterns.ts +16 -15
- package/src/server/router/plugin.ts +151 -158
- package/src/server/router/schema-merge.ts +52 -0
- package/src/server/router/schemas.ts +55 -73
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +9 -27
- package/src/server/sync/fingerprint.ts +8 -3
- package/src/server/sync/index.ts +37 -13
- package/src/server/sync/notifier.ts +109 -0
- package/src/server/sync/plugin.ts +184 -89
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +15 -0
- package/src/server/sync/postgres/migration.sql +66 -0
- package/src/server/sync/redis/index.ts +342 -0
- package/src/server/sync/redis/scripts.ts +74 -0
- package/src/server/sync/response.ts +141 -16
- package/src/server/sync/runtime.ts +27 -0
- package/src/server/sync/sql.d.ts +4 -0
- package/src/server/sync/sqlite/index.ts +337 -0
- package/src/server/sync/sqlite/migration.sql +70 -0
- package/src/server/sync/stream.ts +137 -81
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +4 -4
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/parser.ts +5 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +5 -3
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/server/dev-inspector.ts +0 -60
- package/src/server/sync/memory-adapter.ts +0 -196
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
parse,
|
|
9
|
-
type SourceLang,
|
|
2
|
+
import type {
|
|
3
|
+
CallExpression,
|
|
4
|
+
ImportDeclaration,
|
|
5
|
+
ObjectExpression,
|
|
6
|
+
ObjectProperty,
|
|
7
|
+
Program,
|
|
10
8
|
} from "yuku-parser";
|
|
9
|
+
import { walk } from "yuku-parser";
|
|
11
10
|
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
12
|
-
import {
|
|
11
|
+
import { parseSource } from "../shared/parser.ts";
|
|
12
|
+
import type { AstNode } from "../shared/utils/ast-walk.ts";
|
|
13
|
+
import { hasShadowingDeclaration } from "./binding-scope.ts";
|
|
14
|
+
import { deadCodeElimination } from "./dead-code-elimination.ts";
|
|
15
|
+
import { transformIsomorphicFunctions } from "./transform-isomorphic.ts";
|
|
13
16
|
|
|
14
17
|
// loader: data fetching (runs on server only)
|
|
15
18
|
// query / params: Elysia TypeBox schemas — validated server-side, not used in browser
|
|
16
|
-
const SERVER_ONLY_PROPERTIES = new Set([
|
|
19
|
+
const SERVER_ONLY_PROPERTIES = new Set([
|
|
20
|
+
"loader",
|
|
21
|
+
"requestLoader",
|
|
22
|
+
"query",
|
|
23
|
+
"params",
|
|
24
|
+
"staticParams",
|
|
25
|
+
]);
|
|
26
|
+
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
17
27
|
|
|
18
28
|
interface TransformResult {
|
|
19
29
|
code: string;
|
|
@@ -21,59 +31,220 @@ interface TransformResult {
|
|
|
21
31
|
removedServerCode: boolean;
|
|
22
32
|
}
|
|
23
33
|
|
|
34
|
+
interface RouteTransformBindings {
|
|
35
|
+
createRouteNames: Set<string>;
|
|
36
|
+
routeNames: Set<string>;
|
|
37
|
+
}
|
|
38
|
+
|
|
24
39
|
// ---------------------------------------------------------------------------
|
|
25
|
-
// Check if a CallExpression is createRoute() or
|
|
40
|
+
// Check if a CallExpression is createRoute() or route.page()
|
|
26
41
|
// ---------------------------------------------------------------------------
|
|
27
|
-
function
|
|
28
|
-
return
|
|
42
|
+
function isFurinClientModule(source: unknown): boolean {
|
|
43
|
+
return typeof source === "string" && FURIN_CLIENT_MODULES.has(source);
|
|
29
44
|
}
|
|
30
45
|
|
|
31
|
-
function
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
46
|
+
function isRelativeModule(source: unknown): boolean {
|
|
47
|
+
return typeof source === "string" && source.startsWith(".");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function importedName(spec: AstNode): string | null {
|
|
51
|
+
const { imported } = spec;
|
|
52
|
+
if (!imported || typeof imported !== "object") {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const node = imported as AstNode;
|
|
56
|
+
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
57
|
+
return node.name;
|
|
58
|
+
}
|
|
59
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
60
|
+
return node.value;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function localName(spec: AstNode): string | null {
|
|
66
|
+
const { local } = spec;
|
|
67
|
+
if (!local || typeof local !== "object") {
|
|
68
|
+
return null;
|
|
35
69
|
}
|
|
70
|
+
const node = local as AstNode;
|
|
71
|
+
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function addImportedBinding(
|
|
75
|
+
source: unknown,
|
|
76
|
+
spec: AstNode,
|
|
77
|
+
bindings: RouteTransformBindings
|
|
78
|
+
): void {
|
|
79
|
+
if (spec.type !== "ImportSpecifier" || spec.importKind === "type") {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const imported = importedName(spec);
|
|
83
|
+
const local = localName(spec);
|
|
84
|
+
if (!local) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (isFurinClientModule(source) && imported === "createRoute") {
|
|
88
|
+
bindings.createRouteNames.add(local);
|
|
89
|
+
}
|
|
90
|
+
if (isRelativeModule(source) && imported === "route") {
|
|
91
|
+
bindings.routeNames.add(local);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function collectImportedBindings(program: Program): RouteTransformBindings {
|
|
96
|
+
const bindings = {
|
|
97
|
+
createRouteNames: new Set<string>(),
|
|
98
|
+
routeNames: new Set<string>(),
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
for (const stmt of program.body) {
|
|
102
|
+
if (stmt.type !== "ImportDeclaration") {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const decl = stmt as unknown as ImportDeclaration;
|
|
106
|
+
if (decl.importKind === "type") {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const source = decl.source.value;
|
|
110
|
+
|
|
111
|
+
for (const spec of decl.specifiers as unknown as AstNode[]) {
|
|
112
|
+
addImportedBinding(source, spec, bindings);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return bindings;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function isCreateRouteCall(
|
|
120
|
+
node: CallExpression,
|
|
121
|
+
bindings: RouteTransformBindings,
|
|
122
|
+
ancestors: AstNode[]
|
|
123
|
+
): boolean {
|
|
124
|
+
return (
|
|
125
|
+
node.callee.type === "Identifier" &&
|
|
126
|
+
bindings.createRouteNames.has(node.callee.name) &&
|
|
127
|
+
!hasShadowingDeclaration(node.callee.name, ancestors)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isCreateRouteExpression(
|
|
132
|
+
node: unknown,
|
|
133
|
+
bindings: RouteTransformBindings,
|
|
134
|
+
ancestors: AstNode[]
|
|
135
|
+
): boolean {
|
|
136
|
+
if (!node || typeof node !== "object") {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const expression = unwrapTSExpression(node as { type: string }) as AstNode;
|
|
140
|
+
return (
|
|
141
|
+
expression.type === "CallExpression" &&
|
|
142
|
+
isCreateRouteCall(expression as unknown as CallExpression, bindings, ancestors)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function collectLocalRouteBindings(program: Program, bindings: RouteTransformBindings): void {
|
|
147
|
+
walk(program, {
|
|
148
|
+
VariableDeclarator(node, context) {
|
|
149
|
+
if (node.type !== "VariableDeclarator") {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const { id, init } = node;
|
|
153
|
+
if (!id || typeof id !== "object") {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const identifier = id as unknown as AstNode;
|
|
157
|
+
if (identifier.type !== "Identifier" || typeof identifier.name !== "string") {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (isCreateRouteExpression(init, bindings, context.ancestors() as AstNode[])) {
|
|
161
|
+
bindings.routeNames.add(identifier.name);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function collectRouteTransformBindings(program: Program): RouteTransformBindings {
|
|
168
|
+
const bindings = collectImportedBindings(program);
|
|
169
|
+
collectLocalRouteBindings(program, bindings);
|
|
170
|
+
return bindings;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function isRoutePageCall(
|
|
174
|
+
node: CallExpression,
|
|
175
|
+
bindings: RouteTransformBindings,
|
|
176
|
+
ancestors: AstNode[]
|
|
177
|
+
): boolean {
|
|
178
|
+
const { callee } = node;
|
|
36
179
|
if (
|
|
37
180
|
callee.type === "MemberExpression" &&
|
|
181
|
+
!callee.computed &&
|
|
38
182
|
callee.property.type === "Identifier" &&
|
|
39
183
|
callee.property.name === "page"
|
|
40
184
|
) {
|
|
41
|
-
|
|
185
|
+
const object = unwrapTSExpression(callee.object);
|
|
186
|
+
if (
|
|
187
|
+
object.type === "Identifier" &&
|
|
188
|
+
bindings.routeNames.has(object.name) &&
|
|
189
|
+
!hasShadowingDeclaration(object.name, ancestors)
|
|
190
|
+
) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
return isCreateRouteExpression(object, bindings, ancestors);
|
|
42
194
|
}
|
|
43
195
|
return false;
|
|
44
196
|
}
|
|
45
197
|
|
|
46
|
-
function isTargetCall(
|
|
47
|
-
|
|
198
|
+
function isTargetCall(
|
|
199
|
+
node: CallExpression,
|
|
200
|
+
bindings: RouteTransformBindings,
|
|
201
|
+
ancestors: AstNode[]
|
|
202
|
+
): boolean {
|
|
203
|
+
return isCreateRouteCall(node, bindings, ancestors) || isRoutePageCall(node, bindings, ancestors);
|
|
48
204
|
}
|
|
49
205
|
|
|
50
206
|
function isObjectExpressionNode(node: { type: string }): node is ObjectExpression {
|
|
51
207
|
return node.type === "ObjectExpression";
|
|
52
208
|
}
|
|
53
209
|
|
|
210
|
+
function staticPropertyName(property: ObjectProperty): string | undefined {
|
|
211
|
+
const { key } = property;
|
|
212
|
+
if (key.type === "Identifier" && !property.computed && typeof key.name === "string") {
|
|
213
|
+
return key.name;
|
|
214
|
+
}
|
|
215
|
+
if (key.type === "Literal" && typeof key.value === "string") {
|
|
216
|
+
return key.value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function assertRoutePropertiesAreStaticallySafe(obj: ObjectExpression): void {
|
|
221
|
+
for (const property of obj.properties) {
|
|
222
|
+
if (property.type !== "Property") {
|
|
223
|
+
throw new Error(
|
|
224
|
+
"[furin] Route configuration spreads are not allowed because server-only properties cannot be stripped safely."
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
if (property.computed && staticPropertyName(property) === undefined) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
"[furin] A dynamic computed property in route configuration cannot be stripped safely."
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
54
235
|
// ---------------------------------------------------------------------------
|
|
55
236
|
// Remove server-only properties from an ObjectExpression using MagicString.
|
|
56
237
|
// Returns true if any property was removed.
|
|
57
238
|
// ---------------------------------------------------------------------------
|
|
58
239
|
function removeServerProperties(s: MagicString, source: string, obj: ObjectExpression): boolean {
|
|
240
|
+
assertRoutePropertiesAreStaticallySafe(obj);
|
|
241
|
+
|
|
59
242
|
const toRemove = obj.properties.filter((p): p is ObjectProperty => {
|
|
60
|
-
// Skip spread elements — they have no key.
|
|
61
243
|
if (p.type !== "Property") {
|
|
62
244
|
return false;
|
|
63
245
|
}
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
if (p.computed) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
if (key.type === "Identifier" && typeof key.name === "string") {
|
|
70
|
-
return SERVER_ONLY_PROPERTIES.has(key.name);
|
|
71
|
-
}
|
|
72
|
-
// Quoted string key: { "loader": fn }
|
|
73
|
-
if (key.type === "Literal" && typeof key.value === "string") {
|
|
74
|
-
return SERVER_ONLY_PROPERTIES.has(key.value);
|
|
75
|
-
}
|
|
76
|
-
return false;
|
|
246
|
+
const name = staticPropertyName(p);
|
|
247
|
+
return name !== undefined && SERVER_ONLY_PROPERTIES.has(name);
|
|
77
248
|
});
|
|
78
249
|
if (toRemove.length === 0) {
|
|
79
250
|
return false;
|
|
@@ -92,10 +263,10 @@ function removeServerProperties(s: MagicString, source: string, obj: ObjectExpre
|
|
|
92
263
|
source[removeEnd] === "\t")
|
|
93
264
|
) {
|
|
94
265
|
if (source[removeEnd] === ",") {
|
|
95
|
-
removeEnd
|
|
266
|
+
removeEnd += 1;
|
|
96
267
|
break;
|
|
97
268
|
}
|
|
98
|
-
removeEnd
|
|
269
|
+
removeEnd += 1;
|
|
99
270
|
}
|
|
100
271
|
|
|
101
272
|
// Also remove leading whitespace (indentation before the property)
|
|
@@ -104,13 +275,13 @@ function removeServerProperties(s: MagicString, source: string, obj: ObjectExpre
|
|
|
104
275
|
removeStart > 0 &&
|
|
105
276
|
(source[removeStart - 1] === " " || source[removeStart - 1] === "\t")
|
|
106
277
|
) {
|
|
107
|
-
removeStart
|
|
278
|
+
removeStart -= 1;
|
|
108
279
|
}
|
|
109
280
|
// If there's a newline before the leading whitespace, consume it too
|
|
110
281
|
if (removeStart > 0 && source[removeStart - 1] === "\n") {
|
|
111
|
-
removeStart
|
|
282
|
+
removeStart -= 1;
|
|
112
283
|
if (removeStart > 0 && source[removeStart - 1] === "\r") {
|
|
113
|
-
removeStart
|
|
284
|
+
removeStart -= 1;
|
|
114
285
|
}
|
|
115
286
|
}
|
|
116
287
|
|
|
@@ -121,243 +292,32 @@ function removeServerProperties(s: MagicString, source: string, obj: ObjectExpre
|
|
|
121
292
|
}
|
|
122
293
|
|
|
123
294
|
// TypeScript-specific node types that introduce a type-only scope. Any
|
|
124
|
-
// Identifier / JSXIdentifier beneath one of these nodes is a TypeScript type
|
|
125
|
-
// reference, not a runtime value reference, and must not count toward DCE refs.
|
|
126
|
-
const TYPE_SCOPE_NODES = new Set([
|
|
127
|
-
"TSTypeAnnotation", // `: Type` on variables, params, return types
|
|
128
|
-
"TSTypeParameterDeclaration", // `<T>` in generic type-param declarations
|
|
129
|
-
"TSTypeParameterInstantiation", // `<T>` in generic instantiation positions
|
|
130
|
-
"TSInterfaceDeclaration", // `interface Foo { … }` — entirely type-level
|
|
131
|
-
"TSTypeAliasDeclaration", // `type Foo = …` — entirely type-level
|
|
132
|
-
"TSTypePredicate", // `x is Type` in return-type position
|
|
133
|
-
"TSClassImplements", // `class C implements Iface` — the implements clause is type-only
|
|
134
|
-
]);
|
|
135
|
-
|
|
136
|
-
// Walk `node` and add every Identifier / JSXIdentifier found inside it to
|
|
137
|
-
// `excluded`. Called for nodes that are entirely in type position (e.g. a
|
|
138
|
-
// TSTypeAnnotation) so their descendant identifiers are ignored by DCE.
|
|
139
|
-
function markTypeDescendants(node: unknown, excluded: Set<unknown>): void {
|
|
140
|
-
if (!node || typeof node !== "object") {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (Array.isArray(node)) {
|
|
144
|
-
for (const item of node) {
|
|
145
|
-
markTypeDescendants(item, excluded);
|
|
146
|
-
}
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const n = node as AstNode;
|
|
150
|
-
if (n.type === "Identifier" || n.type === "JSXIdentifier") {
|
|
151
|
-
excluded.add(n);
|
|
152
|
-
}
|
|
153
|
-
for (const key of Object.keys(n)) {
|
|
154
|
-
if (key === "type" || key === "start" || key === "end") {
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
markTypeDescendants(n[key], excluded);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Excludes identifiers that appear in TypeScript type-only positions from the
|
|
162
|
-
// `excluded` set. Extracted from the Pass-1 walk callback to keep cognitive
|
|
163
|
-
// complexity within the allowed budget.
|
|
164
|
-
function excludeTypePositionIdentifiers(node: AstNode, excluded: Set<unknown>): void {
|
|
165
|
-
if (TYPE_SCOPE_NODES.has(node.type)) {
|
|
166
|
-
markTypeDescendants(node, excluded);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
// TSAsExpression (`x as T`), TSSatisfiesExpression (`x satisfies T`), and
|
|
170
|
-
// TSTypeAssertion (`<T>x`) are mixed: the expression child is runtime; only
|
|
171
|
-
// the typeAnnotation child is type-only.
|
|
172
|
-
if (
|
|
173
|
-
node.type === "TSAsExpression" ||
|
|
174
|
-
node.type === "TSSatisfiesExpression" ||
|
|
175
|
-
node.type === "TSTypeAssertion"
|
|
176
|
-
) {
|
|
177
|
-
markTypeDescendants(node.typeAnnotation as unknown, excluded);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
295
|
// ---------------------------------------------------------------------------
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
//
|
|
185
|
-
// • Static (non-computed) object property keys — `{ loader: fn }`
|
|
186
|
-
// • Static (non-computed) member access properties — `obj.prop`
|
|
187
|
-
// • JSX attribute names — `<div className=...>`
|
|
188
|
-
// • Right-hand side of a JSXMemberExpression chain — `<UI.Button>` (Button)
|
|
189
|
-
// • TypeScript type positions — `: MyType`, `<T>`, etc.
|
|
190
|
-
//
|
|
191
|
-
// Computed keys like `{ [someVar]: v }` are left in — they ARE references.
|
|
192
|
-
// JSX tag positions ARE references — `<Link>` requires the `Link` binding
|
|
193
|
-
// to be in scope, so omitting JSXIdentifier (the default after the parser
|
|
194
|
-
// switch from Bun.Transpiler to yuku-parser) silently drops imports that
|
|
195
|
-
// are only used as JSX tags.
|
|
296
|
+
// Remove server-only properties from createRoute() / route.page()
|
|
297
|
+
// calls found anywhere in the AST.
|
|
196
298
|
// ---------------------------------------------------------------------------
|
|
197
|
-
function
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const excluded = new Set<unknown>();
|
|
299
|
+
function removeServerExports(s: MagicString, source: string, program: Program): boolean {
|
|
300
|
+
let removedServerCode = false;
|
|
301
|
+
const bindings = collectRouteTransformBindings(program);
|
|
201
302
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
// Pass 1 — mark non-reference identifier positions.
|
|
207
|
-
// Only exclude *static* keys (computed=false); computed keys like
|
|
208
|
-
// `{ [someVar]: v }` are genuine identifier references.
|
|
209
|
-
walkAST(stmt, (node) => {
|
|
210
|
-
if (node.type === "Property" && !node.computed) {
|
|
211
|
-
excluded.add(node.key);
|
|
212
|
-
}
|
|
213
|
-
if (node.type === "MemberExpression" && !node.computed) {
|
|
214
|
-
excluded.add(node.property);
|
|
215
|
-
}
|
|
216
|
-
// `<div className="...">` — the attribute *name* is a key, not a ref.
|
|
217
|
-
if (node.type === "JSXAttribute") {
|
|
218
|
-
excluded.add(node.name);
|
|
219
|
-
}
|
|
220
|
-
// `<UI.Button>` — `UI` is the live binding, `.Button` is a property
|
|
221
|
-
// lookup on it. Exclude the property side of every JSXMemberExpression
|
|
222
|
-
// so a same-named import (e.g. `import { Button } from ...`) doesn't
|
|
223
|
-
// get falsely kept alive by every `<Foo.Button>` in the file.
|
|
224
|
-
if (node.type === "JSXMemberExpression") {
|
|
225
|
-
excluded.add(node.property);
|
|
303
|
+
walk(program, {
|
|
304
|
+
CallExpression(call, context) {
|
|
305
|
+
if (!isTargetCall(call, bindings, context.ancestors() as AstNode[])) {
|
|
306
|
+
return;
|
|
226
307
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (const stmt of program.body) {
|
|
232
|
-
if (stmt.type === "ImportDeclaration") {
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
// Pass 2 — collect genuine identifier references.
|
|
236
|
-
walkAST(stmt, (node) => {
|
|
237
|
-
if (excluded.has(node)) {
|
|
308
|
+
// Unwrap `createRoute({...} as Config)` / `route.page({...} satisfies Opts)` etc.
|
|
309
|
+
const [firstArg] = call.arguments;
|
|
310
|
+
if (!firstArg) {
|
|
238
311
|
return;
|
|
239
312
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
) {
|
|
244
|
-
refs.add((node as AstNode & { name: string }).name);
|
|
313
|
+
const arg = unwrapTSExpression(firstArg);
|
|
314
|
+
if (!isObjectExpressionNode(arg)) {
|
|
315
|
+
return;
|
|
245
316
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return refs;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// ---------------------------------------------------------------------------
|
|
253
|
-
// Import pruning helpers
|
|
254
|
-
// ---------------------------------------------------------------------------
|
|
255
|
-
function removeEntireImport(s: MagicString, code: string, decl: ImportDeclaration): void {
|
|
256
|
-
let removeEnd = decl.end;
|
|
257
|
-
while (removeEnd < code.length && (code[removeEnd] === "\n" || code[removeEnd] === "\r")) {
|
|
258
|
-
removeEnd++;
|
|
259
|
-
}
|
|
260
|
-
s.remove(decl.start, removeEnd);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function removeUnusedSpecifiers(
|
|
264
|
-
s: MagicString,
|
|
265
|
-
code: string,
|
|
266
|
-
decl: ImportDeclaration,
|
|
267
|
-
refs: Set<string>
|
|
268
|
-
): void {
|
|
269
|
-
const removedSpecs = decl.specifiers.filter((spec) => !refs.has(spec.local.name));
|
|
270
|
-
for (const spec of removedSpecs) {
|
|
271
|
-
let removeStart = spec.start;
|
|
272
|
-
let removeEnd = spec.end;
|
|
273
|
-
while (removeEnd < code.length && (code[removeEnd] === "," || code[removeEnd] === " ")) {
|
|
274
|
-
removeEnd++;
|
|
275
|
-
}
|
|
276
|
-
// react-doctor-disable-next-line react-doctor/js-set-map-lookups
|
|
277
|
-
if (!code.slice(spec.end, removeEnd).includes(",")) {
|
|
278
|
-
while (removeStart > 0 && (code[removeStart - 1] === " " || code[removeStart - 1] === ",")) {
|
|
279
|
-
removeStart--;
|
|
317
|
+
if (removeServerProperties(s, source, arg)) {
|
|
318
|
+
removedServerCode = true;
|
|
280
319
|
}
|
|
281
|
-
}
|
|
282
|
-
s.remove(removeStart, removeEnd);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// ---------------------------------------------------------------------------
|
|
287
|
-
// Dead code elimination: remove import specifiers that are no longer
|
|
288
|
-
// referenced after server property removal.
|
|
289
|
-
//
|
|
290
|
-
// A fresh MagicString is created from s.toString() so that the AST offsets
|
|
291
|
-
// produced by re-parsing the *current* output agree with the string positions
|
|
292
|
-
// operated on by MagicString.remove() — the original MagicString always uses
|
|
293
|
-
// original-source positions, which diverge from output positions whenever
|
|
294
|
-
// earlier passes have removed content.
|
|
295
|
-
// ---------------------------------------------------------------------------
|
|
296
|
-
export function deadCodeElimination(s: MagicString, lang: SourceLang): MagicString {
|
|
297
|
-
const code = s.toString();
|
|
298
|
-
const { program, diagnostics } = parse(code, { sourceType: "module", lang });
|
|
299
|
-
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
300
|
-
if (firstError) {
|
|
301
|
-
console.error("[furin] DCE: failed to parse transformed output:", firstError.message);
|
|
302
|
-
return s;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const fresh = new MagicString(code);
|
|
306
|
-
const refs = collectReferencedNames(program);
|
|
307
|
-
|
|
308
|
-
const body = program.body;
|
|
309
|
-
for (let i = body.length - 1; i >= 0; i--) {
|
|
310
|
-
const stmt = body[i];
|
|
311
|
-
if (!stmt) {
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
if (stmt.type !== "ImportDeclaration") {
|
|
315
|
-
continue;
|
|
316
|
-
}
|
|
317
|
-
const decl = stmt as unknown as ImportDeclaration;
|
|
318
|
-
if (!decl.specifiers || decl.specifiers.length === 0) {
|
|
319
|
-
continue;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const usedCount = decl.specifiers.filter((spec) => refs.has(spec.local.name)).length;
|
|
323
|
-
|
|
324
|
-
if (usedCount === 0) {
|
|
325
|
-
removeEntireImport(fresh, code, decl);
|
|
326
|
-
} else if (usedCount < decl.specifiers.length) {
|
|
327
|
-
removeUnusedSpecifiers(fresh, code, decl, refs);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
return fresh;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// ---------------------------------------------------------------------------
|
|
335
|
-
// Remove server-only properties from createRoute() / page() / route.page()
|
|
336
|
-
// calls found anywhere in the AST.
|
|
337
|
-
// ---------------------------------------------------------------------------
|
|
338
|
-
function removeServerExports(s: MagicString, source: string, program: Program): boolean {
|
|
339
|
-
let removedServerCode = false;
|
|
340
|
-
|
|
341
|
-
walkAST(program, (node) => {
|
|
342
|
-
if (node.type !== "CallExpression") {
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
const call = node as unknown as CallExpression;
|
|
346
|
-
if (!isTargetCall(call)) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
// Unwrap `createRoute({...} as Config)` / `page({...} satisfies Opts)` etc.
|
|
350
|
-
const firstArg = call.arguments[0];
|
|
351
|
-
if (!firstArg) {
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
const arg = unwrapTSExpression(firstArg);
|
|
355
|
-
if (!isObjectExpressionNode(arg)) {
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
if (removeServerProperties(s, source, arg)) {
|
|
359
|
-
removedServerCode = true;
|
|
360
|
-
}
|
|
320
|
+
},
|
|
361
321
|
});
|
|
362
322
|
|
|
363
323
|
return removedServerCode;
|
|
@@ -371,28 +331,32 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
371
331
|
return { code, map: null, removedServerCode: false };
|
|
372
332
|
}
|
|
373
333
|
|
|
334
|
+
const isomorphicResult = transformIsomorphicFunctions(code, filename, "client");
|
|
335
|
+
const clientSource = isomorphicResult.code;
|
|
336
|
+
|
|
374
337
|
// Pass 1 — yuku-parser: parse TS/TSX/JS/JSX directly to ESTree AST with
|
|
375
338
|
// span offsets calibrated against `code` itself (no transpile step).
|
|
376
|
-
const { program, diagnostics } =
|
|
339
|
+
const { program, diagnostics } = parseSource(clientSource, lang);
|
|
377
340
|
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
378
341
|
if (firstError) {
|
|
379
342
|
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
380
343
|
}
|
|
381
344
|
|
|
382
345
|
// Pass 2 — MagicString: surgically remove server-only properties.
|
|
383
|
-
let s = new MagicString(
|
|
384
|
-
const
|
|
346
|
+
let s = new MagicString(clientSource);
|
|
347
|
+
const removedRouteCode = removeServerExports(s, clientSource, program);
|
|
348
|
+
const removedServerCode = isomorphicResult.transformed || removedRouteCode;
|
|
385
349
|
|
|
386
350
|
// Pass 3 — DCE: prune imports that are no longer referenced.
|
|
387
351
|
// deadCodeElimination returns a fresh MagicString keyed on the current
|
|
388
352
|
// output so its internal AST offsets remain consistent.
|
|
389
353
|
if (removedServerCode) {
|
|
390
|
-
s = deadCodeElimination(s, lang);
|
|
354
|
+
s = deadCodeElimination(s, code, lang);
|
|
391
355
|
}
|
|
392
356
|
|
|
393
357
|
return {
|
|
394
358
|
code: s.toString(),
|
|
395
|
-
map: s.generateMap({
|
|
359
|
+
map: s.generateMap({ includeContent: true, source: filename }),
|
|
396
360
|
removedServerCode,
|
|
397
361
|
};
|
|
398
362
|
}
|