@teyik0/furin 0.3.0-alpha.1 → 0.4.0-alpha.2
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/bun.d.ts +2 -18
- package/dist/adapter/package.d.ts +2 -2
- package/dist/adapter/runtime-build.d.ts +33 -0
- package/dist/adapter/vercel.d.ts +3 -0
- package/dist/build/client.d.ts +2 -2
- package/dist/build/compile-entry.d.ts +4 -3
- package/dist/build/compile-entry.ts +24 -69
- package/dist/build/entry-template.d.ts +8 -2
- package/dist/build/entry-template.ts +48 -11
- package/dist/build/index.d.ts +2 -2
- package/dist/build/index.js +11720 -7840
- package/dist/build/route-types.d.ts +2 -22
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +14 -1
- package/dist/build/types.d.ts +26 -3
- package/dist/build/virtual-entry.d.ts +8 -0
- package/dist/cache.d.ts +5 -0
- package/dist/cache.js +97 -0
- package/dist/cli/index.js +11815 -7793
- package/dist/cli/preview.d.ts +7 -0
- package/dist/client/boundaries.d.ts +3 -0
- package/dist/client/browser-events-runtime.d.ts +2 -0
- package/dist/client/browser-events.d.ts +2 -0
- package/dist/client/define-route.d.ts +36 -0
- package/dist/client/dev-error-overlay.d.ts +1 -0
- package/dist/client/dev-error-overlay.ts +201 -0
- package/dist/client/document.d.ts +27 -0
- package/dist/client/hmr.d.ts +12 -0
- package/dist/client/internal/runtime-types.d.ts +3 -1
- package/dist/client/link.js +1392 -940
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +9 -0
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/search/index.d.ts +3 -7
- package/dist/client/router/sync-catch-up.d.ts +1 -1
- package/dist/client/router/types.d.ts +40 -34
- package/dist/client.d.ts +4 -159
- package/dist/client.js +197 -21
- package/dist/config.d.ts +25 -8
- package/dist/config.js +15 -7
- package/dist/define-route.d.ts +1358 -0
- package/dist/devtools/protocol.d.ts +2 -2
- package/dist/furin.d.ts +18 -34
- package/dist/furin.js +16038 -9395
- package/dist/plugin/index.js +1401 -223
- package/dist/plugin/route-config-autofix.d.ts +31 -0
- package/dist/plugin/routes.d.ts +28 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +1 -0
- package/dist/rsc/build/index.d.ts +6 -2
- package/dist/rsc.js +3 -3
- package/dist/server/browser-events/plugin.d.ts +11 -0
- package/dist/server/browser-events/types.d.ts +9 -0
- package/dist/server/cache/invalidation.d.ts +1 -2
- package/dist/server/cache/isr.d.ts +1 -0
- package/dist/server/cache/purger.d.ts +7 -0
- package/dist/server/cache/runtime-cache.d.ts +21 -0
- package/dist/server/dev/browser-events.d.ts +4 -0
- package/dist/server/dev/diagnostics.d.ts +39 -0
- package/dist/server/dev/error.d.ts +17 -0
- package/dist/server/dev/graph.d.ts +86 -0
- package/dist/server/dev/plugin.d.ts +9 -0
- package/dist/server/dev/request-security.d.ts +1 -0
- package/dist/server/dev/rewrite-module-specifiers.d.ts +7 -0
- package/dist/server/dev/runtime.production.d.ts +5 -0
- package/dist/server/dev/symbolicate.d.ts +11 -0
- package/dist/server/dev/transform-failure.d.ts +4 -0
- package/dist/server/dev-page-plugin.d.ts +7 -5
- package/dist/server/dev-page-plugin.production.d.ts +1 -0
- package/dist/server/devtools/hub.d.ts +3 -2
- package/dist/server/devtools/instrumentation.d.ts +2 -2
- package/dist/server/devtools/instrumentation.production.d.ts +2 -2
- package/dist/server/devtools/plugin.d.ts +2 -2
- package/dist/server/external-prerender.d.ts +2 -0
- package/dist/server/instance.d.ts +2 -2
- package/dist/server/internal.d.ts +17 -2
- package/dist/server/internal.js +436 -1
- package/dist/server/logger.d.ts +4 -0
- package/dist/server/render/assemble.d.ts +0 -9
- package/dist/server/render/document.d.ts +4 -0
- package/dist/server/render/element.d.ts +3 -1
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +1 -2
- package/dist/server/render/isr.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +1 -1
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-document.d.ts +21 -0
- package/dist/server/render/ppr-request.d.ts +26 -0
- package/dist/server/render/ppr-route.d.ts +4 -2
- package/dist/server/render/shell.ts +0 -12
- package/dist/server/render/ssg.d.ts +3 -1
- package/dist/server/render/ssr.d.ts +23 -11
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +24 -0
- package/dist/server/router/hmr.d.ts +16 -8
- package/dist/server/router/hmr.production.d.ts +2 -0
- package/dist/server/router/patterns.d.ts +7 -0
- package/dist/server/router/plugin.d.ts +6 -3
- package/dist/server/router/schema-merge.d.ts +2 -2
- package/dist/server/router/schemas.d.ts +5 -5
- package/dist/server/router/source-version.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -0
- package/dist/server/sync/adapter.d.ts +13 -0
- package/dist/server/sync/config.d.ts +5 -5
- package/dist/server/sync/index.d.ts +6 -6
- package/dist/server/sync/index.js +1115 -985
- package/dist/server/sync/notifier.d.ts +1 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +101 -6
- package/dist/server/sync/postgres/migrate.js +3 -2
- package/dist/server/sync/stream.d.ts +4 -13
- package/dist/shared/await.d.ts +4 -5
- package/dist/shared/browser-events.d.ts +22 -0
- package/dist/shared/compact-json.d.ts +8 -0
- package/dist/shared/deferred-ndjson.d.ts +6 -5
- package/dist/shared/dev-diagnostics.d.ts +36 -0
- package/dist/shared/elysia-contract.d.ts +41 -0
- package/dist/shared/prefix.d.ts +2 -0
- package/dist/shared/route-map.d.ts +5 -0
- package/dist/shared/utils/index.d.ts +1 -3
- package/package.json +55 -43
- package/src/adapter/README.md +45 -0
- package/src/adapter/bun.ts +62 -265
- package/src/adapter/package.ts +34 -22
- package/src/adapter/runtime-build.ts +375 -0
- package/src/adapter/static.ts +83 -5
- package/src/adapter/vercel.ts +857 -0
- package/src/build/client.ts +58 -16
- package/src/build/compile-entry.ts +24 -69
- package/src/build/entry-template.ts +48 -11
- package/src/build/hydrate.ts +147 -37
- package/src/build/index.ts +21 -7
- package/src/build/production-instrumentation.ts +56 -4
- package/src/build/route-types.ts +19 -114
- package/src/build/shared.ts +3 -2
- package/src/build/ssg-cache.ts +99 -16
- package/src/build/types.ts +32 -3
- package/src/build/virtual-entry.ts +27 -0
- package/src/cache.ts +14 -0
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +62 -10
- package/src/cli/preview.ts +90 -0
- package/src/client/boundaries.tsx +19 -2
- package/src/client/browser-events-runtime.ts +204 -0
- package/src/client/browser-events.ts +22 -0
- package/src/client/define-route.ts +38 -0
- package/src/client/dev-error-overlay.ts +201 -0
- package/src/client/document.tsx +162 -0
- package/src/client/hmr.ts +63 -0
- package/src/client/internal/runtime-types.ts +3 -1
- package/src/client/link.tsx +21 -6
- package/src/client/router/boundary-tree.tsx +4 -4
- package/src/client/router/index.ts +1 -0
- package/src/client/router/link-utils.ts +26 -0
- package/src/client/router/provider.tsx +190 -65
- package/src/client/router/search/index.ts +4 -12
- package/src/client/router/sync-catch-up.ts +3 -4
- package/src/client/router/types.ts +56 -30
- package/src/client.ts +14 -316
- package/src/config.ts +35 -10
- package/src/define-route.ts +972 -0
- package/src/devtools/devtools-element.js +67 -87
- package/src/devtools/protocol.ts +2 -2
- package/src/furin.ts +431 -101
- package/src/plugin/route-config-autofix.ts +1140 -0
- package/src/plugin/routes.ts +1061 -0
- package/src/plugin/transform-client.ts +776 -228
- package/src/plugin/transform-isomorphic.ts +1 -1
- package/src/react-dom-edge.d.ts +6 -0
- package/src/routes.ts +6 -0
- package/src/rsc/build/index.ts +16 -4
- package/src/server/auto-invalidate/registry.ts +2 -2
- package/src/server/auto-invalidate/runtime.ts +5 -2
- package/src/server/browser-events/plugin.ts +168 -0
- package/src/server/browser-events/types.ts +11 -0
- package/src/server/cache/dev-loader.ts +30 -29
- package/src/server/cache/invalidation.ts +13 -22
- package/src/server/cache/isr.ts +9 -0
- package/src/server/cache/purger.ts +41 -0
- package/src/server/cache/runtime-cache.ts +137 -0
- package/src/server/dev/browser-events.ts +54 -0
- package/src/server/dev/diagnostics.ts +297 -0
- package/src/server/dev/error.ts +148 -0
- package/src/server/dev/graph.ts +438 -0
- package/src/server/dev/plugin.ts +130 -0
- package/src/server/dev/request-security.ts +38 -0
- package/src/server/dev/rewrite-module-specifiers.ts +81 -0
- package/src/server/dev/runtime.production.ts +24 -0
- package/src/server/dev/symbolicate.ts +135 -0
- package/src/server/dev/transform-failure.ts +8 -0
- package/src/server/dev-page-plugin.production.ts +3 -0
- package/src/server/dev-page-plugin.ts +110 -52
- package/src/server/devtools/hub.ts +6 -5
- package/src/server/devtools/instrumentation.production.ts +3 -3
- package/src/server/devtools/instrumentation.ts +14 -8
- package/src/server/devtools/plugin.ts +12 -153
- package/src/server/external-prerender.ts +10 -0
- package/src/server/instance.ts +5 -5
- package/src/server/internal.ts +42 -2
- package/src/server/logger.ts +21 -0
- package/src/server/render/assemble.ts +4 -45
- package/src/server/render/document.tsx +34 -0
- package/src/server/render/element.tsx +31 -15
- package/src/server/render/index.ts +1 -2
- package/src/server/render/isr.ts +70 -24
- package/src/server/render/loaders.ts +43 -8
- package/src/server/render/not-found.ts +77 -29
- package/src/server/render/ppr-document.ts +239 -0
- package/src/server/render/ppr-request.ts +104 -0
- package/src/server/render/ppr-route.ts +153 -32
- package/src/server/render/shell.ts +0 -12
- package/src/server/render/ssg.ts +39 -21
- package/src/server/render/ssr.ts +429 -143
- package/src/server/render/template.ts +128 -2
- package/src/server/router/defined-route.ts +78 -0
- package/src/server/router/discovery.ts +129 -29
- package/src/server/router/hmr.production.ts +7 -0
- package/src/server/router/hmr.ts +177 -67
- package/src/server/router/patterns.ts +45 -13
- package/src/server/router/plugin.ts +184 -78
- package/src/server/router/schema-merge.ts +4 -14
- package/src/server/router/schemas.ts +11 -8
- package/src/server/router/source-version.ts +9 -0
- package/src/server/router/types.ts +2 -0
- package/src/server/sync/adapter.ts +13 -0
- package/src/server/sync/config.ts +12 -12
- package/src/server/sync/index.ts +8 -8
- package/src/server/sync/notifier.ts +1 -0
- package/src/server/sync/plugin.ts +4 -1
- package/src/server/sync/postgres/index.ts +111 -5
- package/src/server/sync/postgres/migrate.ts +3 -2
- package/src/server/sync/response.ts +14 -10
- package/src/server/sync/runtime.ts +12 -0
- package/src/server/sync/stream.ts +78 -113
- package/src/shared/await.tsx +4 -5
- package/src/shared/browser-events.ts +28 -0
- package/src/shared/compact-json.ts +77 -0
- package/src/shared/deferred-ndjson.ts +20 -6
- package/src/shared/dev-diagnostics.ts +44 -0
- package/src/shared/elysia-contract.ts +63 -0
- package/src/shared/prefix.ts +8 -0
- package/src/shared/route-map.ts +46 -0
- package/src/shared/utils/index.ts +2 -20
- package/dist/build/server-routes-entry.d.ts +0 -13
- package/dist/build/server-routes-entry.ts +0 -30
- package/dist/server/router/index.d.ts +0 -7
- package/dist/server/router/index.js +0 -27
- package/dist/server/router/index.ts +0 -28
- package/src/build/server-routes-entry.ts +0 -30
- package/src/server/router/index.ts +0 -28
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
ImportDeclaration,
|
|
5
|
-
ObjectExpression,
|
|
6
|
-
ObjectProperty,
|
|
7
|
-
Program,
|
|
8
|
-
} from "yuku-parser";
|
|
9
|
-
import { walk } from "yuku-parser";
|
|
2
|
+
import { walk } from "yuku-ast";
|
|
3
|
+
import type { ImportDeclaration, Program } from "yuku-parser";
|
|
10
4
|
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
11
5
|
import { parseSource } from "../shared/parser.ts";
|
|
12
6
|
import type { AstNode } from "../shared/utils/ast-walk.ts";
|
|
@@ -14,16 +8,20 @@ import { hasShadowingDeclaration } from "./binding-scope.ts";
|
|
|
14
8
|
import { deadCodeElimination } from "./dead-code-elimination.ts";
|
|
15
9
|
import { transformIsomorphicFunctions } from "./transform-isomorphic.ts";
|
|
16
10
|
|
|
17
|
-
// loader: data fetching (runs on server only)
|
|
18
|
-
// query / params: Elysia TypeBox schemas — validated server-side, not used in browser
|
|
19
|
-
const SERVER_ONLY_PROPERTIES = new Set([
|
|
20
|
-
"loader",
|
|
21
|
-
"requestLoader",
|
|
22
|
-
"query",
|
|
23
|
-
"params",
|
|
24
|
-
"staticParams",
|
|
25
|
-
]);
|
|
26
11
|
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
12
|
+
const FURIN_SERVER_MODULES = new Set(["@teyik0/furin", "furin"]);
|
|
13
|
+
const REACT_COMPONENT_WRAPPERS = new Set(["forwardRef", "memo"]);
|
|
14
|
+
const SERVER_ONLY_METHODS = new Set(["config", "head", "loader", "requestLoader", "staticParams"]);
|
|
15
|
+
const REACT_HOOK_NAME_RE = /^use[A-Z0-9]/;
|
|
16
|
+
const HMR_DATA_SIGNATURE = "furin.hmr.data-signature";
|
|
17
|
+
const TYPESCRIPT_RUNTIME_WRAPPERS = new Map([
|
|
18
|
+
["TSAsExpression", ["expression"]],
|
|
19
|
+
["TSInstantiationExpression", ["expression"]],
|
|
20
|
+
["TSNonNullExpression", ["expression"]],
|
|
21
|
+
["TSParameterProperty", ["decorators", "parameter"]],
|
|
22
|
+
["TSSatisfiesExpression", ["expression"]],
|
|
23
|
+
["TSTypeAssertion", ["expression"]],
|
|
24
|
+
]);
|
|
27
25
|
|
|
28
26
|
interface TransformResult {
|
|
29
27
|
code: string;
|
|
@@ -31,24 +29,15 @@ interface TransformResult {
|
|
|
31
29
|
removedServerCode: boolean;
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
// Check if a CallExpression is createRoute() or route.page()
|
|
41
|
-
// ---------------------------------------------------------------------------
|
|
42
|
-
function isFurinClientModule(source: unknown): boolean {
|
|
43
|
-
return typeof source === "string" && FURIN_CLIENT_MODULES.has(source);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isRelativeModule(source: unknown): boolean {
|
|
47
|
-
return typeof source === "string" && source.startsWith(".");
|
|
32
|
+
function isFurinRouteModule(source: unknown): source is string {
|
|
33
|
+
return (
|
|
34
|
+
typeof source === "string" &&
|
|
35
|
+
(FURIN_SERVER_MODULES.has(source) || FURIN_CLIENT_MODULES.has(source))
|
|
36
|
+
);
|
|
48
37
|
}
|
|
49
38
|
|
|
50
|
-
function importedName(
|
|
51
|
-
const { imported } =
|
|
39
|
+
function importedName(specifier: AstNode): string | null {
|
|
40
|
+
const { imported } = specifier;
|
|
52
41
|
if (!imported || typeof imported !== "object") {
|
|
53
42
|
return null;
|
|
54
43
|
}
|
|
@@ -62,8 +51,8 @@ function importedName(spec: AstNode): string | null {
|
|
|
62
51
|
return null;
|
|
63
52
|
}
|
|
64
53
|
|
|
65
|
-
function localName(
|
|
66
|
-
const { local } =
|
|
54
|
+
function localName(specifier: AstNode): string | null {
|
|
55
|
+
const { local } = specifier;
|
|
67
56
|
if (!local || typeof local !== "object") {
|
|
68
57
|
return null;
|
|
69
58
|
}
|
|
@@ -71,261 +60,768 @@ function localName(spec: AstNode): string | null {
|
|
|
71
60
|
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
72
61
|
}
|
|
73
62
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
63
|
+
function collectDefineRouteBindings(program: Program): Set<string> {
|
|
64
|
+
const bindings = new Set<string>();
|
|
65
|
+
for (const statement of program.body) {
|
|
66
|
+
if (statement.type !== "ImportDeclaration") {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const declaration = statement as unknown as ImportDeclaration;
|
|
70
|
+
if (declaration.importKind === "type") {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const source = declaration.source.value;
|
|
74
|
+
if (!isFurinRouteModule(source)) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
78
|
+
if (specifier.type !== "ImportSpecifier" || specifier.importKind === "type") {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const imported = importedName(specifier);
|
|
82
|
+
if (imported !== "defineRoute" && imported !== "defineRootRoute") {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const local = localName(specifier);
|
|
86
|
+
if (local) {
|
|
87
|
+
bindings.add(local);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
92
90
|
}
|
|
91
|
+
return bindings;
|
|
93
92
|
}
|
|
94
93
|
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
for (const
|
|
102
|
-
if (
|
|
94
|
+
function rewriteClientImports(
|
|
95
|
+
source: MagicString,
|
|
96
|
+
program: Program,
|
|
97
|
+
bindings: Set<string>
|
|
98
|
+
): boolean {
|
|
99
|
+
let rewritten = false;
|
|
100
|
+
for (const statement of program.body) {
|
|
101
|
+
if (statement.type !== "ImportDeclaration") {
|
|
103
102
|
continue;
|
|
104
103
|
}
|
|
105
|
-
const
|
|
106
|
-
|
|
104
|
+
const declaration = statement as unknown as ImportDeclaration;
|
|
105
|
+
const moduleName = declaration.source.value;
|
|
106
|
+
if (typeof moduleName !== "string" || !FURIN_SERVER_MODULES.has(moduleName)) {
|
|
107
107
|
continue;
|
|
108
108
|
}
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
const importsClientValue = (declaration.specifiers as unknown as AstNode[]).some(
|
|
110
|
+
(specifier) => {
|
|
111
|
+
const local = localName(specifier);
|
|
112
|
+
const imported = importedName(specifier);
|
|
113
|
+
return (
|
|
114
|
+
(local !== null && bindings.has(local)) ||
|
|
115
|
+
imported === "HeadContent" ||
|
|
116
|
+
imported === "Scripts"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
if (!importsClientValue) {
|
|
121
|
+
continue;
|
|
113
122
|
}
|
|
123
|
+
const clientModule = moduleName === "furin" ? "furin/client" : "@teyik0/furin/client";
|
|
124
|
+
source.update(declaration.source.start + 1, declaration.source.end - 1, clientModule);
|
|
125
|
+
rewritten = true;
|
|
114
126
|
}
|
|
115
|
-
|
|
116
|
-
return bindings;
|
|
127
|
+
return rewritten;
|
|
117
128
|
}
|
|
118
129
|
|
|
119
|
-
function
|
|
120
|
-
node
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return (
|
|
125
|
-
node.callee.type === "Identifier" &&
|
|
126
|
-
bindings.createRouteNames.has(node.callee.name) &&
|
|
127
|
-
!hasShadowingDeclaration(node.callee.name, ancestors)
|
|
128
|
-
);
|
|
130
|
+
function asAstNode(node: unknown): AstNode | null {
|
|
131
|
+
if (!node || typeof node !== "object" || !("type" in node)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return unwrapTSExpression(node as { type: string }) as AstNode;
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
function
|
|
137
|
+
function chainRootIsDefineRoute(
|
|
132
138
|
node: unknown,
|
|
133
|
-
bindings:
|
|
139
|
+
bindings: Set<string>,
|
|
134
140
|
ancestors: AstNode[]
|
|
135
141
|
): boolean {
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
let current = asAstNode(node);
|
|
143
|
+
while (current) {
|
|
144
|
+
if (current.type === "MemberExpression") {
|
|
145
|
+
current = asAstNode(current.object);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (current.type === "CallExpression") {
|
|
149
|
+
current = asAstNode(current.callee);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
return (
|
|
153
|
+
current.type === "Identifier" &&
|
|
154
|
+
typeof current.name === "string" &&
|
|
155
|
+
bindings.has(current.name) &&
|
|
156
|
+
!hasShadowingDeclaration(current.name, ancestors)
|
|
157
|
+
);
|
|
138
158
|
}
|
|
139
|
-
|
|
140
|
-
return (
|
|
141
|
-
expression.type === "CallExpression" &&
|
|
142
|
-
isCreateRouteCall(expression as unknown as CallExpression, bindings, ancestors)
|
|
143
|
-
);
|
|
159
|
+
return false;
|
|
144
160
|
}
|
|
145
161
|
|
|
146
|
-
function
|
|
162
|
+
function removeChainedServerCalls(
|
|
163
|
+
source: MagicString,
|
|
164
|
+
program: Program,
|
|
165
|
+
bindings: Set<string>
|
|
166
|
+
): boolean {
|
|
167
|
+
let transformed = rewriteClientImports(source, program, bindings);
|
|
168
|
+
|
|
147
169
|
walk(program, {
|
|
148
|
-
|
|
149
|
-
|
|
170
|
+
CallExpression(call, context) {
|
|
171
|
+
const callee = asAstNode(call.callee);
|
|
172
|
+
if (
|
|
173
|
+
callee?.type !== "MemberExpression" ||
|
|
174
|
+
callee.computed === true ||
|
|
175
|
+
!callee.property ||
|
|
176
|
+
typeof callee.property !== "object"
|
|
177
|
+
) {
|
|
150
178
|
return;
|
|
151
179
|
}
|
|
152
|
-
const
|
|
153
|
-
if (
|
|
180
|
+
const property = callee.property as AstNode;
|
|
181
|
+
if (
|
|
182
|
+
property.type !== "Identifier" ||
|
|
183
|
+
typeof property.name !== "string" ||
|
|
184
|
+
!SERVER_ONLY_METHODS.has(property.name) ||
|
|
185
|
+
!chainRootIsDefineRoute(callee.object, bindings, context.ancestors() as AstNode[])
|
|
186
|
+
) {
|
|
154
187
|
return;
|
|
155
188
|
}
|
|
156
|
-
const
|
|
157
|
-
if (
|
|
189
|
+
const object = asAstNode(callee.object);
|
|
190
|
+
if (!object) {
|
|
158
191
|
return;
|
|
159
192
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
193
|
+
source.remove(object.end, call.end);
|
|
194
|
+
transformed = true;
|
|
163
195
|
},
|
|
164
196
|
});
|
|
197
|
+
return transformed;
|
|
165
198
|
}
|
|
166
199
|
|
|
167
|
-
function
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
200
|
+
function collectBindingNames(pattern: unknown, names: string[]): void {
|
|
201
|
+
const node = asAstNode(pattern);
|
|
202
|
+
if (!node) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
206
|
+
names.push(node.name);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (node.type === "AssignmentPattern") {
|
|
210
|
+
collectBindingNames(node.left, names);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (node.type === "RestElement") {
|
|
214
|
+
collectBindingNames(node.argument, names);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (node.type === "ArrayPattern" && Array.isArray(node.elements)) {
|
|
218
|
+
for (const element of node.elements) {
|
|
219
|
+
collectBindingNames(element, names);
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (node.type === "ObjectPattern" && Array.isArray(node.properties)) {
|
|
224
|
+
for (const property of node.properties) {
|
|
225
|
+
const propertyNode = asAstNode(property);
|
|
226
|
+
collectBindingNames(
|
|
227
|
+
propertyNode?.type === "Property" ? propertyNode.value : propertyNode?.argument,
|
|
228
|
+
names
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
171
232
|
}
|
|
172
233
|
|
|
173
|
-
function
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
) {
|
|
191
|
-
|
|
234
|
+
function collectDeclarationBindings(
|
|
235
|
+
declaration: AstNode | null,
|
|
236
|
+
declarations: Map<string, AstNode>
|
|
237
|
+
): void {
|
|
238
|
+
if (declaration?.type === "FunctionDeclaration" || declaration?.type === "ClassDeclaration") {
|
|
239
|
+
const identifier = asAstNode(declaration.id);
|
|
240
|
+
if (identifier?.type === "Identifier" && typeof identifier.name === "string") {
|
|
241
|
+
declarations.set(identifier.name, declaration);
|
|
242
|
+
}
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (declaration?.type !== "VariableDeclaration" || !Array.isArray(declaration.declarations)) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
for (const item of declaration.declarations as AstNode[]) {
|
|
249
|
+
const names: string[] = [];
|
|
250
|
+
collectBindingNames(item.id, names);
|
|
251
|
+
for (const name of names) {
|
|
252
|
+
declarations.set(name, item);
|
|
192
253
|
}
|
|
193
|
-
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function collectModuleBindings(program: Program): {
|
|
258
|
+
declarations: Map<string, AstNode>;
|
|
259
|
+
imports: Set<string>;
|
|
260
|
+
} {
|
|
261
|
+
const declarations = new Map<string, AstNode>();
|
|
262
|
+
const imports = new Set<string>();
|
|
263
|
+
for (const statement of program.body as unknown as AstNode[]) {
|
|
264
|
+
if (statement.type === "ImportDeclaration" && Array.isArray(statement.specifiers)) {
|
|
265
|
+
for (const specifier of statement.specifiers as AstNode[]) {
|
|
266
|
+
const local = localName(specifier);
|
|
267
|
+
if (local) {
|
|
268
|
+
imports.add(local);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const declaration =
|
|
274
|
+
statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
|
|
275
|
+
? asAstNode(statement.declaration)
|
|
276
|
+
: statement;
|
|
277
|
+
collectDeclarationBindings(declaration, declarations);
|
|
278
|
+
}
|
|
279
|
+
return { declarations, imports };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function isBindingIdentifier(identifier: AstNode, pattern: unknown): boolean {
|
|
283
|
+
const node = asAstNode(pattern);
|
|
284
|
+
if (!node) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (node.type === "Identifier") {
|
|
288
|
+
return node.start === identifier.start && node.end === identifier.end;
|
|
289
|
+
}
|
|
290
|
+
if (node.type === "AssignmentPattern") {
|
|
291
|
+
return isBindingIdentifier(identifier, node.left);
|
|
292
|
+
}
|
|
293
|
+
if (node.type === "RestElement") {
|
|
294
|
+
return isBindingIdentifier(identifier, node.argument);
|
|
295
|
+
}
|
|
296
|
+
if (node.type === "ArrayPattern" && Array.isArray(node.elements)) {
|
|
297
|
+
return node.elements.some((element) => isBindingIdentifier(identifier, element));
|
|
298
|
+
}
|
|
299
|
+
if (node.type === "ObjectPattern" && Array.isArray(node.properties)) {
|
|
300
|
+
return node.properties.some((property) => {
|
|
301
|
+
const propertyNode = asAstNode(property);
|
|
302
|
+
return isBindingIdentifier(
|
|
303
|
+
identifier,
|
|
304
|
+
propertyNode?.type === "Property" ? propertyNode.value : propertyNode?.argument
|
|
305
|
+
);
|
|
306
|
+
});
|
|
194
307
|
}
|
|
195
308
|
return false;
|
|
196
309
|
}
|
|
197
310
|
|
|
198
|
-
function
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return
|
|
311
|
+
function nodeContains(container: unknown, node: AstNode): boolean {
|
|
312
|
+
if (Array.isArray(container)) {
|
|
313
|
+
return container.some((entry) => nodeContains(entry, node));
|
|
314
|
+
}
|
|
315
|
+
const containerNode = asAstNode(container);
|
|
316
|
+
return Boolean(
|
|
317
|
+
containerNode && containerNode.start <= node.start && node.end <= containerNode.end
|
|
318
|
+
);
|
|
204
319
|
}
|
|
205
320
|
|
|
206
|
-
function
|
|
207
|
-
return
|
|
321
|
+
function isTypePosition(node: AstNode, ancestors: AstNode[]): boolean {
|
|
322
|
+
return ancestors.some((ancestor) => {
|
|
323
|
+
if (!ancestor.type.startsWith("TS")) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
const runtimeChildren = TYPESCRIPT_RUNTIME_WRAPPERS.get(ancestor.type);
|
|
327
|
+
if (runtimeChildren) {
|
|
328
|
+
return !runtimeChildren.some((key) => nodeContains(ancestor[key], node));
|
|
329
|
+
}
|
|
330
|
+
return true;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function isNonReferenceKey(node: AstNode, parent: AstNode | undefined): boolean {
|
|
335
|
+
return Boolean(
|
|
336
|
+
parent &&
|
|
337
|
+
((parent.type === "MemberExpression" &&
|
|
338
|
+
parent.computed !== true &&
|
|
339
|
+
parent.property === node) ||
|
|
340
|
+
(parent.type === "Property" &&
|
|
341
|
+
parent.computed !== true &&
|
|
342
|
+
parent.shorthand !== true &&
|
|
343
|
+
parent.key === node) ||
|
|
344
|
+
((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") &&
|
|
345
|
+
parent.computed !== true &&
|
|
346
|
+
parent.key === node) ||
|
|
347
|
+
((parent.type === "LabeledStatement" ||
|
|
348
|
+
parent.type === "BreakStatement" ||
|
|
349
|
+
parent.type === "ContinueStatement") &&
|
|
350
|
+
parent.label === node))
|
|
351
|
+
);
|
|
208
352
|
}
|
|
209
353
|
|
|
210
|
-
function
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
return key.name;
|
|
354
|
+
function isReferenceIdentifier(node: AstNode, ancestors: AstNode[]): boolean {
|
|
355
|
+
if (isTypePosition(node, ancestors) || isNonReferenceKey(node, ancestors.at(-1))) {
|
|
356
|
+
return false;
|
|
214
357
|
}
|
|
215
|
-
|
|
216
|
-
|
|
358
|
+
for (const ancestor of ancestors) {
|
|
359
|
+
if (
|
|
360
|
+
(ancestor.type === "VariableDeclarator" && isBindingIdentifier(node, ancestor.id)) ||
|
|
361
|
+
((ancestor.type === "FunctionDeclaration" ||
|
|
362
|
+
ancestor.type === "FunctionExpression" ||
|
|
363
|
+
ancestor.type === "ArrowFunctionExpression") &&
|
|
364
|
+
(isBindingIdentifier(node, ancestor.id) ||
|
|
365
|
+
(Array.isArray(ancestor.params) &&
|
|
366
|
+
ancestor.params.some((parameter) => isBindingIdentifier(node, parameter))))) ||
|
|
367
|
+
(ancestor.type === "ClassDeclaration" && isBindingIdentifier(node, ancestor.id)) ||
|
|
368
|
+
(ancestor.type === "CatchClause" && isBindingIdentifier(node, ancestor.param))
|
|
369
|
+
) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
217
372
|
}
|
|
373
|
+
return true;
|
|
218
374
|
}
|
|
219
375
|
|
|
220
|
-
function
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
376
|
+
function walkWithAncestors(
|
|
377
|
+
value: unknown,
|
|
378
|
+
ancestors: AstNode[],
|
|
379
|
+
visitor: (node: AstNode, ancestors: AstNode[]) => void
|
|
380
|
+
): void {
|
|
381
|
+
if (Array.isArray(value)) {
|
|
382
|
+
for (const entry of value) {
|
|
383
|
+
walkWithAncestors(entry, ancestors, visitor);
|
|
226
384
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const node = asAstNode(value);
|
|
388
|
+
if (!node) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
visitor(node, ancestors);
|
|
392
|
+
const nextAncestors = [...ancestors, node];
|
|
393
|
+
for (const [key, child] of Object.entries(node)) {
|
|
394
|
+
if (key !== "type" && key !== "start" && key !== "end" && key !== "comments") {
|
|
395
|
+
walkWithAncestors(child, nextAncestors, visitor);
|
|
231
396
|
}
|
|
232
397
|
}
|
|
233
398
|
}
|
|
234
399
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
400
|
+
interface HmrDependencyState {
|
|
401
|
+
dependencies: Map<number, AstNode>;
|
|
402
|
+
hasUnresolvedImport: boolean;
|
|
403
|
+
importTrackedDependencies: Set<number>;
|
|
404
|
+
moduleBindings: ReturnType<typeof collectModuleBindings>;
|
|
405
|
+
}
|
|
241
406
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
407
|
+
function collectDependencyIdentifier(
|
|
408
|
+
child: AstNode,
|
|
409
|
+
ancestors: AstNode[],
|
|
410
|
+
trackImports: boolean,
|
|
411
|
+
state: HmrDependencyState
|
|
412
|
+
): void {
|
|
413
|
+
if (
|
|
414
|
+
child.type !== "Identifier" ||
|
|
415
|
+
typeof child.name !== "string" ||
|
|
416
|
+
!isReferenceIdentifier(child, ancestors) ||
|
|
417
|
+
hasShadowingDeclaration(child.name, ancestors)
|
|
418
|
+
) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const declaration = state.moduleBindings.declarations.get(child.name);
|
|
422
|
+
if (!declaration) {
|
|
423
|
+
if (trackImports && state.moduleBindings.imports.has(child.name)) {
|
|
424
|
+
state.hasUnresolvedImport = true;
|
|
245
425
|
}
|
|
246
|
-
|
|
247
|
-
return name !== undefined && SERVER_ONLY_PROPERTIES.has(name);
|
|
248
|
-
});
|
|
249
|
-
if (toRemove.length === 0) {
|
|
250
|
-
return false;
|
|
426
|
+
return;
|
|
251
427
|
}
|
|
428
|
+
const alreadyTracked = state.dependencies.has(declaration.start);
|
|
429
|
+
if (!alreadyTracked) {
|
|
430
|
+
state.dependencies.set(declaration.start, declaration);
|
|
431
|
+
}
|
|
432
|
+
if (trackImports) {
|
|
433
|
+
if (state.importTrackedDependencies.has(declaration.start)) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
state.importTrackedDependencies.add(declaration.start);
|
|
437
|
+
} else if (alreadyTracked) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
collectDependencies(declaration, trackImports, state);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function collectDependencies(
|
|
444
|
+
node: AstNode,
|
|
445
|
+
trackImports: boolean,
|
|
446
|
+
state: HmrDependencyState
|
|
447
|
+
): void {
|
|
448
|
+
walkWithAncestors(node, [], (child, ancestors) => {
|
|
449
|
+
collectDependencyIdentifier(child, ancestors, trackImports, state);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function createHmrDataSignature(code: string, program: Program, bindings: Set<string>): string {
|
|
454
|
+
const serverStages: Array<{ source: string; start: number }> = [];
|
|
455
|
+
const dependencyState: HmrDependencyState = {
|
|
456
|
+
dependencies: new Map(),
|
|
457
|
+
hasUnresolvedImport: false,
|
|
458
|
+
importTrackedDependencies: new Set(),
|
|
459
|
+
moduleBindings: collectModuleBindings(program),
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
walk(program, {
|
|
463
|
+
CallExpression(call, context) {
|
|
464
|
+
const callee = asAstNode(call.callee);
|
|
465
|
+
if (
|
|
466
|
+
callee?.type !== "MemberExpression" ||
|
|
467
|
+
callee.computed === true ||
|
|
468
|
+
!callee.property ||
|
|
469
|
+
typeof callee.property !== "object"
|
|
470
|
+
) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const property = callee.property as AstNode;
|
|
474
|
+
if (
|
|
475
|
+
property.type !== "Identifier" ||
|
|
476
|
+
typeof property.name !== "string" ||
|
|
477
|
+
!SERVER_ONLY_METHODS.has(property.name) ||
|
|
478
|
+
!chainRootIsDefineRoute(callee.object, bindings, context.ancestors() as AstNode[])
|
|
479
|
+
) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const object = asAstNode(callee.object);
|
|
483
|
+
if (!object) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
serverStages.push({
|
|
487
|
+
source: code.slice(object.end, call.end),
|
|
488
|
+
start: call.start,
|
|
489
|
+
});
|
|
490
|
+
if (Array.isArray(call.arguments)) {
|
|
491
|
+
for (const argument of call.arguments) {
|
|
492
|
+
const argumentNode = asAstNode(argument);
|
|
493
|
+
if (argumentNode) {
|
|
494
|
+
collectDependencies(argumentNode, property.name !== "config", dependencyState);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
});
|
|
252
500
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
501
|
+
const dataSource = [
|
|
502
|
+
...serverStages,
|
|
503
|
+
...[...dependencyState.dependencies.values()].map((dependency) => ({
|
|
504
|
+
source: code.slice(dependency.start, dependency.end),
|
|
505
|
+
start: dependency.start,
|
|
506
|
+
})),
|
|
507
|
+
]
|
|
508
|
+
.sort((left, right) => left.start - right.start)
|
|
509
|
+
.map((entry) => entry.source)
|
|
510
|
+
.join("\n");
|
|
511
|
+
const hash = new Bun.CryptoHasher("sha256").update(dataSource).digest("hex");
|
|
512
|
+
return dependencyState.hasUnresolvedImport ? `external:${hash}` : hash;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function calledHookName(call: AstNode): string | null {
|
|
516
|
+
const callee = asAstNode(call.callee);
|
|
517
|
+
if (callee?.type === "Identifier" && typeof callee.name === "string") {
|
|
518
|
+
return REACT_HOOK_NAME_RE.test(callee.name) ? callee.name : null;
|
|
519
|
+
}
|
|
520
|
+
if (
|
|
521
|
+
callee?.type === "MemberExpression" &&
|
|
522
|
+
callee.computed !== true &&
|
|
523
|
+
callee.property &&
|
|
524
|
+
typeof callee.property === "object"
|
|
525
|
+
) {
|
|
526
|
+
const property = callee.property as AstNode;
|
|
527
|
+
if (
|
|
528
|
+
property.type === "Identifier" &&
|
|
529
|
+
typeof property.name === "string" &&
|
|
530
|
+
REACT_HOOK_NAME_RE.test(property.name)
|
|
264
531
|
) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
532
|
+
return property.name;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function routeComponentExpression(program: Program, bindings: Set<string>): AstNode | null {
|
|
539
|
+
let component: AstNode | null = null;
|
|
540
|
+
walk(program, {
|
|
541
|
+
CallExpression(call, context) {
|
|
542
|
+
const callee = asAstNode(call.callee);
|
|
543
|
+
if (
|
|
544
|
+
callee?.type !== "MemberExpression" ||
|
|
545
|
+
callee.computed === true ||
|
|
546
|
+
!callee.property ||
|
|
547
|
+
typeof callee.property !== "object"
|
|
548
|
+
) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
const property = callee.property as AstNode;
|
|
552
|
+
const ancestors = context.ancestors() as AstNode[];
|
|
553
|
+
const routeDeclarator = ancestors.find((ancestor) => {
|
|
554
|
+
if (ancestor.type !== "VariableDeclarator") {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
const identifier = asAstNode(ancestor.id);
|
|
558
|
+
return identifier?.type === "Identifier" && identifier.name === "route";
|
|
559
|
+
});
|
|
560
|
+
if (
|
|
561
|
+
!(
|
|
562
|
+
routeDeclarator &&
|
|
563
|
+
ancestors.some((ancestor) => ancestor.type === "ExportNamedDeclaration")
|
|
564
|
+
) ||
|
|
565
|
+
property.type !== "Identifier" ||
|
|
566
|
+
(property.name !== "page" && property.name !== "layout") ||
|
|
567
|
+
!chainRootIsDefineRoute(callee.object, bindings, ancestors)
|
|
568
|
+
) {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const args = call.arguments;
|
|
572
|
+
component = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
573
|
+
context.stop();
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
return component;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function namedFunction(declaration: AstNode, name: string): AstNode | null {
|
|
580
|
+
if (declaration.type === "FunctionDeclaration") {
|
|
581
|
+
const identifier = asAstNode(declaration.id);
|
|
582
|
+
return identifier?.type === "Identifier" && identifier.name === name ? declaration : null;
|
|
583
|
+
}
|
|
584
|
+
if (declaration.type === "VariableDeclaration" && Array.isArray(declaration.declarations)) {
|
|
585
|
+
for (const item of declaration.declarations as AstNode[]) {
|
|
586
|
+
const identifier = asAstNode(item.id);
|
|
587
|
+
const initializer = asAstNode(item.init);
|
|
588
|
+
if (
|
|
589
|
+
identifier?.type === "Identifier" &&
|
|
590
|
+
identifier.name === name &&
|
|
591
|
+
(initializer?.type === "ArrowFunctionExpression" ||
|
|
592
|
+
initializer?.type === "FunctionExpression")
|
|
593
|
+
) {
|
|
594
|
+
return initializer;
|
|
268
595
|
}
|
|
269
|
-
removeEnd += 1;
|
|
270
596
|
}
|
|
597
|
+
}
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
271
600
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
601
|
+
function localFunction(program: Program, name: string): AstNode | null {
|
|
602
|
+
for (const statement of program.body as unknown as AstNode[]) {
|
|
603
|
+
const declaration =
|
|
604
|
+
statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
|
|
605
|
+
? asAstNode(statement.declaration)
|
|
606
|
+
: statement;
|
|
607
|
+
const component = declaration ? namedFunction(declaration, name) : null;
|
|
608
|
+
if (component) {
|
|
609
|
+
return component;
|
|
279
610
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
611
|
+
}
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
interface ImportedRouteComponent {
|
|
616
|
+
end: number;
|
|
617
|
+
expression: string;
|
|
618
|
+
start: number;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function isImportedBinding(program: Program, name: string): boolean {
|
|
622
|
+
return program.body.some(
|
|
623
|
+
(statement) =>
|
|
624
|
+
statement.type === "ImportDeclaration" &&
|
|
625
|
+
statement.importKind !== "type" &&
|
|
626
|
+
(statement.specifiers as unknown as AstNode[]).some(
|
|
627
|
+
(specifier) => specifier.importKind !== "type" && localName(specifier) === name
|
|
628
|
+
)
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
interface ReactWrapperBindings {
|
|
633
|
+
members: Set<string>;
|
|
634
|
+
namespaces: Set<string>;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function collectReactWrapperBindings(program: Program): ReactWrapperBindings {
|
|
638
|
+
const bindings: ReactWrapperBindings = {
|
|
639
|
+
members: new Set<string>(),
|
|
640
|
+
namespaces: new Set<string>(),
|
|
641
|
+
};
|
|
642
|
+
for (const statement of program.body) {
|
|
643
|
+
if (statement.type !== "ImportDeclaration" || statement.source.value !== "react") {
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
for (const specifier of statement.specifiers as unknown as AstNode[]) {
|
|
647
|
+
const local = localName(specifier);
|
|
648
|
+
if (!local || specifier.importKind === "type") {
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
const imported = importedName(specifier);
|
|
652
|
+
if (
|
|
653
|
+
specifier.type === "ImportSpecifier" &&
|
|
654
|
+
imported &&
|
|
655
|
+
REACT_COMPONENT_WRAPPERS.has(imported)
|
|
656
|
+
) {
|
|
657
|
+
bindings.members.add(local);
|
|
658
|
+
} else if (
|
|
659
|
+
specifier.type === "ImportDefaultSpecifier" ||
|
|
660
|
+
specifier.type === "ImportNamespaceSpecifier" ||
|
|
661
|
+
(specifier.type === "ImportSpecifier" && imported === "default")
|
|
662
|
+
) {
|
|
663
|
+
bindings.namespaces.add(local);
|
|
285
664
|
}
|
|
286
665
|
}
|
|
666
|
+
}
|
|
667
|
+
return bindings;
|
|
668
|
+
}
|
|
287
669
|
|
|
288
|
-
|
|
670
|
+
function wrappedComponentFunction(
|
|
671
|
+
program: Program,
|
|
672
|
+
expression: AstNode,
|
|
673
|
+
bindings: ReactWrapperBindings
|
|
674
|
+
): AstNode | null {
|
|
675
|
+
if (expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression") {
|
|
676
|
+
return expression;
|
|
677
|
+
}
|
|
678
|
+
if (expression.type === "Identifier" && typeof expression.name === "string") {
|
|
679
|
+
return localFunction(program, expression.name);
|
|
289
680
|
}
|
|
681
|
+
if (expression.type !== "CallExpression") {
|
|
682
|
+
return null;
|
|
683
|
+
}
|
|
684
|
+
const callee = asAstNode(expression.callee);
|
|
685
|
+
const isNamedWrapper =
|
|
686
|
+
callee?.type === "Identifier" &&
|
|
687
|
+
typeof callee.name === "string" &&
|
|
688
|
+
bindings.members.has(callee.name);
|
|
689
|
+
const object = callee?.type === "MemberExpression" ? asAstNode(callee.object) : null;
|
|
690
|
+
const property = callee?.type === "MemberExpression" ? asAstNode(callee.property) : null;
|
|
691
|
+
const isNamespaceWrapper =
|
|
692
|
+
callee?.type === "MemberExpression" &&
|
|
693
|
+
callee.computed !== true &&
|
|
694
|
+
object?.type === "Identifier" &&
|
|
695
|
+
typeof object.name === "string" &&
|
|
696
|
+
bindings.namespaces.has(object.name) &&
|
|
697
|
+
property?.type === "Identifier" &&
|
|
698
|
+
typeof property.name === "string" &&
|
|
699
|
+
REACT_COMPONENT_WRAPPERS.has(property.name);
|
|
700
|
+
if (!(isNamedWrapper || isNamespaceWrapper)) {
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
const args = expression.arguments;
|
|
704
|
+
const wrapped = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
705
|
+
return wrapped ? wrappedComponentFunction(program, wrapped, bindings) : null;
|
|
706
|
+
}
|
|
290
707
|
|
|
291
|
-
|
|
708
|
+
function importedMemberBinding(program: Program, expression: AstNode): string | null {
|
|
709
|
+
let object = expression.type === "MemberExpression" ? asAstNode(expression.object) : null;
|
|
710
|
+
while (object?.type === "MemberExpression") {
|
|
711
|
+
object = asAstNode(object.object);
|
|
712
|
+
}
|
|
713
|
+
return object?.type === "Identifier" &&
|
|
714
|
+
typeof object.name === "string" &&
|
|
715
|
+
isImportedBinding(program, object.name)
|
|
716
|
+
? object.name
|
|
717
|
+
: null;
|
|
292
718
|
}
|
|
293
719
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
// calls found anywhere in the AST.
|
|
298
|
-
// ---------------------------------------------------------------------------
|
|
299
|
-
function removeServerExports(s: MagicString, source: string, program: Program): boolean {
|
|
300
|
-
let removedServerCode = false;
|
|
301
|
-
const bindings = collectRouteTransformBindings(program);
|
|
720
|
+
function sourceText(code: string, node: AstNode | null): string {
|
|
721
|
+
return node ? code.slice(node.start, node.end) : "";
|
|
722
|
+
}
|
|
302
723
|
|
|
724
|
+
function unusedIdentifier(program: Program, preferred: string): string {
|
|
725
|
+
const identifiers = new Set<string>();
|
|
303
726
|
walk(program, {
|
|
727
|
+
Identifier(node) {
|
|
728
|
+
identifiers.add(node.name);
|
|
729
|
+
},
|
|
730
|
+
});
|
|
731
|
+
if (!identifiers.has(preferred)) {
|
|
732
|
+
return preferred;
|
|
733
|
+
}
|
|
734
|
+
let suffix = 1;
|
|
735
|
+
while (identifiers.has(`${preferred}_${suffix}`)) {
|
|
736
|
+
suffix += 1;
|
|
737
|
+
}
|
|
738
|
+
return `${preferred}_${suffix}`;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function hookCallsiteSignature(
|
|
742
|
+
code: string,
|
|
743
|
+
call: AstNode,
|
|
744
|
+
hookName: string,
|
|
745
|
+
parent: AstNode | null
|
|
746
|
+
): string {
|
|
747
|
+
let key = parent?.type === "VariableDeclarator" ? sourceText(code, asAstNode(parent.id)) : "";
|
|
748
|
+
const args = call.arguments;
|
|
749
|
+
const stateArgument = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
750
|
+
const reducerArgument = Array.isArray(args) ? asAstNode(args[1]) : null;
|
|
751
|
+
if (hookName === "useState" && stateArgument) {
|
|
752
|
+
key += `(${sourceText(code, stateArgument)})`;
|
|
753
|
+
} else if (hookName === "useReducer" && reducerArgument) {
|
|
754
|
+
key += `(${sourceText(code, reducerArgument)})`;
|
|
755
|
+
}
|
|
756
|
+
return `${hookName}{${key}}`;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function collectFunctionHookSignature(code: string, component: AstNode): string[] {
|
|
760
|
+
const hooks: string[] = [];
|
|
761
|
+
walk(component as never, {
|
|
304
762
|
CallExpression(call, context) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
const arg = unwrapTSExpression(firstArg);
|
|
314
|
-
if (!isObjectExpressionNode(arg)) {
|
|
315
|
-
return;
|
|
763
|
+
const callNode = call as unknown as AstNode;
|
|
764
|
+
const name = calledHookName(callNode);
|
|
765
|
+
if (name) {
|
|
766
|
+
hooks.push(
|
|
767
|
+
hookCallsiteSignature(code, callNode, name, asAstNode(context.parent as unknown))
|
|
768
|
+
);
|
|
316
769
|
}
|
|
317
|
-
|
|
318
|
-
|
|
770
|
+
},
|
|
771
|
+
Function(node, context) {
|
|
772
|
+
if ((node as unknown as AstNode) !== component) {
|
|
773
|
+
context.skip();
|
|
319
774
|
}
|
|
320
775
|
},
|
|
321
776
|
});
|
|
777
|
+
return hooks;
|
|
778
|
+
}
|
|
322
779
|
|
|
323
|
-
|
|
780
|
+
function collectClientHookSignature(
|
|
781
|
+
code: string,
|
|
782
|
+
filename: string
|
|
783
|
+
): string[] | ImportedRouteComponent | "external" | null {
|
|
784
|
+
const lang = detectLangFromPath(filename);
|
|
785
|
+
const { program } = parseSource(code, lang);
|
|
786
|
+
const componentExpression = routeComponentExpression(
|
|
787
|
+
program,
|
|
788
|
+
collectDefineRouteBindings(program)
|
|
789
|
+
);
|
|
790
|
+
if (!componentExpression) {
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
const wrappedComponent = wrappedComponentFunction(
|
|
794
|
+
program,
|
|
795
|
+
componentExpression,
|
|
796
|
+
collectReactWrapperBindings(program)
|
|
797
|
+
);
|
|
798
|
+
if (wrappedComponent) {
|
|
799
|
+
return collectFunctionHookSignature(code, wrappedComponent);
|
|
800
|
+
}
|
|
801
|
+
if (componentExpression.type !== "Identifier" || typeof componentExpression.name !== "string") {
|
|
802
|
+
if (importedMemberBinding(program, componentExpression)) {
|
|
803
|
+
return {
|
|
804
|
+
end: componentExpression.end,
|
|
805
|
+
expression: sourceText(code, componentExpression),
|
|
806
|
+
start: componentExpression.start,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
return collectFunctionHookSignature(code, componentExpression);
|
|
810
|
+
}
|
|
811
|
+
const component = localFunction(program, componentExpression.name);
|
|
812
|
+
if (component) {
|
|
813
|
+
return collectFunctionHookSignature(code, component);
|
|
814
|
+
}
|
|
815
|
+
return isImportedBinding(program, componentExpression.name)
|
|
816
|
+
? {
|
|
817
|
+
end: componentExpression.end,
|
|
818
|
+
expression: componentExpression.name,
|
|
819
|
+
start: componentExpression.start,
|
|
820
|
+
}
|
|
821
|
+
: "external";
|
|
324
822
|
}
|
|
325
823
|
|
|
326
824
|
export function transformForClient(code: string, filename: string): TransformResult {
|
|
327
|
-
// .d.ts files have no runtime code — yuku rejects parsing them as modules.
|
|
328
|
-
// Treat as a passthrough so callers don't need to pre-filter.
|
|
329
825
|
const lang = detectLangFromPath(filename);
|
|
330
826
|
if (lang === "dts") {
|
|
331
827
|
return { code, map: null, removedServerCode: false };
|
|
@@ -333,30 +829,82 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
333
829
|
|
|
334
830
|
const isomorphicResult = transformIsomorphicFunctions(code, filename, "client");
|
|
335
831
|
const clientSource = isomorphicResult.code;
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
// span offsets calibrated against `code` itself (no transpile step).
|
|
339
|
-
const { program, diagnostics } = parseSource(clientSource, lang);
|
|
340
|
-
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
832
|
+
const { diagnostics, program } = parseSource(clientSource, lang);
|
|
833
|
+
const firstError = diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
341
834
|
if (firstError) {
|
|
342
835
|
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
343
836
|
}
|
|
344
837
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
const removedRouteCode =
|
|
838
|
+
let source = new MagicString(clientSource);
|
|
839
|
+
const routeBindings = collectDefineRouteBindings(program);
|
|
840
|
+
const removedRouteCode = removeChainedServerCalls(source, program, routeBindings);
|
|
348
841
|
const removedServerCode = isomorphicResult.transformed || removedRouteCode;
|
|
349
|
-
|
|
350
|
-
// Pass 3 — DCE: prune imports that are no longer referenced.
|
|
351
|
-
// deadCodeElimination returns a fresh MagicString keyed on the current
|
|
352
|
-
// output so its internal AST offsets remain consistent.
|
|
353
842
|
if (removedServerCode) {
|
|
354
|
-
|
|
843
|
+
source = deadCodeElimination(source, code, lang);
|
|
844
|
+
}
|
|
845
|
+
const transformedCode = source.toString();
|
|
846
|
+
let hookSignature =
|
|
847
|
+
routeBindings.size > 0 ? collectClientHookSignature(transformedCode, filename) : null;
|
|
848
|
+
if (hookSignature && typeof hookSignature === "object" && !Array.isArray(hookSignature)) {
|
|
849
|
+
const createElementBinding = unusedIdentifier(program, "__furinCreateElement");
|
|
850
|
+
source = new MagicString(transformedCode);
|
|
851
|
+
source.prepend(`import { createElement as ${createElementBinding} } from "react";\n`);
|
|
852
|
+
source.update(
|
|
853
|
+
hookSignature.start,
|
|
854
|
+
hookSignature.end,
|
|
855
|
+
`(import.meta.hot ? (props) => ${createElementBinding}(${hookSignature.expression}, props) : ${hookSignature.expression})`
|
|
856
|
+
);
|
|
857
|
+
hookSignature = [];
|
|
858
|
+
}
|
|
859
|
+
if (routeBindings.size > 0 && hookSignature !== null) {
|
|
860
|
+
const originalParse = parseSource(code, lang);
|
|
861
|
+
const originalError = originalParse.diagnostics.find(
|
|
862
|
+
(diagnostic) => diagnostic.severity === "error"
|
|
863
|
+
);
|
|
864
|
+
if (originalError) {
|
|
865
|
+
throw new Error(`Failed to parse ${filename}: ${originalError.message}`);
|
|
866
|
+
}
|
|
867
|
+
const hmrDataSignature = createHmrDataSignature(
|
|
868
|
+
code,
|
|
869
|
+
originalParse.program,
|
|
870
|
+
collectDefineRouteBindings(originalParse.program)
|
|
871
|
+
);
|
|
872
|
+
const signatureValue = Array.isArray(hookSignature)
|
|
873
|
+
? JSON.stringify(hookSignature)
|
|
874
|
+
: 'route.component[Symbol.for("furin.hmr.hook-signature")] ?? [String(route.component)]';
|
|
875
|
+
source.append(`
|
|
876
|
+
if (import.meta.hot && route?.component) {
|
|
877
|
+
const previousDataSignature = ${JSON.stringify(hmrDataSignature)};
|
|
878
|
+
Object.defineProperty(route, Symbol.for(${JSON.stringify(HMR_DATA_SIGNATURE)}), {
|
|
879
|
+
configurable: true,
|
|
880
|
+
value: previousDataSignature,
|
|
881
|
+
});
|
|
882
|
+
Object.defineProperty(route.component, Symbol.for("furin.hmr.hook-signature"), {
|
|
883
|
+
configurable: true,
|
|
884
|
+
value: ${signatureValue},
|
|
885
|
+
});
|
|
886
|
+
import.meta.hot.accept((updatedModule) => {
|
|
887
|
+
const updatedRoute = updatedModule?.route;
|
|
888
|
+
if (updatedRoute?.component) {
|
|
889
|
+
const updatedDataSignature = Reflect.get(
|
|
890
|
+
updatedRoute,
|
|
891
|
+
Symbol.for(${JSON.stringify(HMR_DATA_SIGNATURE)})
|
|
892
|
+
);
|
|
893
|
+
const dataChanged =
|
|
894
|
+
previousDataSignature.startsWith("external:") ||
|
|
895
|
+
typeof updatedDataSignature !== "string" ||
|
|
896
|
+
updatedDataSignature.startsWith("external:") ||
|
|
897
|
+
updatedDataSignature !== previousDataSignature;
|
|
898
|
+
window.__FURIN_HMR_UPDATE__?.(${JSON.stringify(filename)}, updatedRoute.component, dataChanged);
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
`);
|
|
355
903
|
}
|
|
356
904
|
|
|
357
905
|
return {
|
|
358
|
-
code:
|
|
359
|
-
map:
|
|
906
|
+
code: source.toString(),
|
|
907
|
+
map: source.generateMap({ includeContent: true, source: filename }),
|
|
360
908
|
removedServerCode,
|
|
361
909
|
};
|
|
362
910
|
}
|