@tanstack/start-plugin-core 1.132.0-alpha.8 → 1.132.0
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/esm/constants.d.ts +2 -1
- package/dist/esm/constants.js +3 -2
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/create-server-fn-plugin/compiler.d.ts +64 -0
- package/dist/esm/create-server-fn-plugin/compiler.js +364 -0
- package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/handleCreateMiddleware.d.ts +5 -0
- package/dist/esm/{start-compiler-plugin/middleware.js → create-server-fn-plugin/handleCreateMiddleware.js} +11 -11
- package/dist/esm/create-server-fn-plugin/handleCreateMiddleware.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.d.ts +6 -0
- package/dist/esm/{start-compiler-plugin/serverFn.js → create-server-fn-plugin/handleCreateServerFn.js} +15 -17
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/plugin.d.ts +3 -0
- package/dist/esm/create-server-fn-plugin/plugin.js +128 -0
- package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -0
- package/dist/esm/dev-server-plugin/plugin.d.ts +4 -2
- package/dist/esm/dev-server-plugin/plugin.js +6 -2
- package/dist/esm/dev-server-plugin/plugin.js.map +1 -1
- package/dist/esm/plugin.d.ts +12 -6
- package/dist/esm/plugin.js +56 -68
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/resolve-entries.d.ts +0 -1
- package/dist/esm/resolve-entries.js +1 -1
- package/dist/esm/resolve-entries.js.map +1 -1
- package/dist/esm/schema.d.ts +375 -308
- package/dist/esm/schema.js +23 -11
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/start-compiler-plugin/compilers.js +17 -55
- package/dist/esm/start-compiler-plugin/compilers.js.map +1 -1
- package/dist/esm/start-compiler-plugin/constants.d.ts +1 -1
- package/dist/esm/start-compiler-plugin/constants.js +1 -6
- package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
- package/dist/esm/start-compiler-plugin/plugin.d.ts +1 -8
- package/dist/esm/start-compiler-plugin/plugin.js +6 -13
- package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/constants.d.ts +1 -0
- package/dist/esm/start-router-plugin/constants.js +5 -0
- package/dist/esm/start-router-plugin/constants.js.map +1 -0
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +3 -9
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/plugin.d.ts +3 -2
- package/dist/esm/start-router-plugin/plugin.js +191 -31
- package/dist/esm/start-router-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.d.ts +8 -0
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js +34 -0
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js.map +1 -0
- package/package.json +8 -8
- package/src/constants.ts +3 -2
- package/src/create-server-fn-plugin/compiler.ts +498 -0
- package/src/{start-compiler-plugin/middleware.ts → create-server-fn-plugin/handleCreateMiddleware.ts} +15 -12
- package/src/{start-compiler-plugin/serverFn.ts → create-server-fn-plugin/handleCreateServerFn.ts} +32 -39
- package/src/create-server-fn-plugin/plugin.ts +153 -0
- package/src/dev-server-plugin/plugin.ts +6 -3
- package/src/plugin.ts +78 -87
- package/src/resolve-entries.ts +1 -2
- package/src/schema.ts +31 -14
- package/src/start-compiler-plugin/compilers.ts +18 -57
- package/src/start-compiler-plugin/constants.ts +0 -5
- package/src/start-compiler-plugin/plugin.ts +7 -22
- package/src/start-router-plugin/constants.ts +1 -0
- package/src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts +3 -9
- package/src/start-router-plugin/plugin.ts +233 -45
- package/src/start-router-plugin/pruneServerOnlySubtrees.ts +51 -0
- package/dist/esm/debug.js +0 -5
- package/dist/esm/debug.js.map +0 -1
- package/dist/esm/start-compiler-plugin/middleware.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/middleware.js.map +0 -1
- package/dist/esm/start-compiler-plugin/serverFileRoute.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/serverFileRoute.js +0 -38
- package/dist/esm/start-compiler-plugin/serverFileRoute.js.map +0 -1
- package/dist/esm/start-compiler-plugin/serverFn.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/serverFn.js.map +0 -1
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.d.ts +0 -2
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js +0 -119
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js.map +0 -1
- package/dist/esm/start-router-plugin/route-tree-client-plugin.d.ts +0 -6
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js +0 -56
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js.map +0 -1
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.d.ts +0 -3
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js +0 -29
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js.map +0 -1
- package/src/start-compiler-plugin/serverFileRoute.ts +0 -59
- package/src/start-router-plugin/generator-plugins/server-routes-plugin.ts +0 -138
- package/src/start-router-plugin/route-tree-client-plugin.ts +0 -77
- package/src/start-router-plugin/virtual-route-tree-plugin.ts +0 -29
package/dist/esm/constants.d.ts
CHANGED
|
@@ -6,5 +6,6 @@ export type ViteEnvironmentNames = (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof
|
|
|
6
6
|
export declare const ENTRY_POINTS: {
|
|
7
7
|
readonly client: "virtual:tanstack-start-client-entry";
|
|
8
8
|
readonly server: "virtual:tanstack-start-server-request-entry";
|
|
9
|
-
readonly
|
|
9
|
+
readonly start: "#tanstack-start-entry";
|
|
10
|
+
readonly router: "#tanstack-router-entry";
|
|
10
11
|
};
|
package/dist/esm/constants.js
CHANGED
|
@@ -7,8 +7,9 @@ const VITE_ENVIRONMENT_NAMES = {
|
|
|
7
7
|
const ENTRY_POINTS = {
|
|
8
8
|
client: "virtual:tanstack-start-client-entry",
|
|
9
9
|
server: "virtual:tanstack-start-server-request-entry",
|
|
10
|
-
// the
|
|
11
|
-
|
|
10
|
+
// the start entry point must always be provided by the user
|
|
11
|
+
start: "#tanstack-start-entry",
|
|
12
|
+
router: "#tanstack-router-entry"
|
|
12
13
|
};
|
|
13
14
|
export {
|
|
14
15
|
ENTRY_POINTS,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sources":["../../src/constants.ts"],"sourcesContent":["export const VITE_ENVIRONMENT_NAMES = {\n // 'ssr' is chosen as the name for the server environment to ensure backwards compatibility\n // with vite plugins that are not compatible with the new vite environment API (e.g. tailwindcss)\n server: 'ssr',\n client: 'client',\n} as const\n\nexport type ViteEnvironmentNames =\n (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof VITE_ENVIRONMENT_NAMES]\n\n// for client and router:\n// if a user has a custom server/client entry point file, resolve.alias will point to this\n// otherwise it will be aliased to the default entry point in the respective framework plugin\nexport const ENTRY_POINTS = {\n client: 'virtual:tanstack-start-client-entry',\n server: 'virtual:tanstack-start-server-request-entry',\n // the
|
|
1
|
+
{"version":3,"file":"constants.js","sources":["../../src/constants.ts"],"sourcesContent":["export const VITE_ENVIRONMENT_NAMES = {\n // 'ssr' is chosen as the name for the server environment to ensure backwards compatibility\n // with vite plugins that are not compatible with the new vite environment API (e.g. tailwindcss)\n server: 'ssr',\n client: 'client',\n} as const\n\nexport type ViteEnvironmentNames =\n (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof VITE_ENVIRONMENT_NAMES]\n\n// for client and router:\n// if a user has a custom server/client entry point file, resolve.alias will point to this\n// otherwise it will be aliased to the default entry point in the respective framework plugin\nexport const ENTRY_POINTS = {\n client: 'virtual:tanstack-start-client-entry',\n server: 'virtual:tanstack-start-server-request-entry',\n // the start entry point must always be provided by the user\n start: '#tanstack-start-entry',\n router: '#tanstack-router-entry',\n} as const\n"],"names":[],"mappings":"AAAO,MAAM,yBAAyB;AAAA;AAAA;AAAA,EAGpC,QAAQ;AAAA,EACR,QAAQ;AACV;AAQO,MAAM,eAAe;AAAA,EAC1B,QAAQ;AAAA,EACR,QAAQ;AAAA;AAAA,EAER,OAAO;AAAA,EACP,QAAQ;AACV;"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { parseAst } from '@tanstack/router-utils';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
type Binding = {
|
|
4
|
+
type: 'import';
|
|
5
|
+
source: string;
|
|
6
|
+
importedName: string;
|
|
7
|
+
resolvedKind?: Kind;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'var';
|
|
10
|
+
init: t.Expression | null;
|
|
11
|
+
resolvedKind?: Kind;
|
|
12
|
+
};
|
|
13
|
+
type ExportEntry = {
|
|
14
|
+
tag: 'Normal';
|
|
15
|
+
name: string;
|
|
16
|
+
} | {
|
|
17
|
+
tag: 'Default';
|
|
18
|
+
name: string;
|
|
19
|
+
} | {
|
|
20
|
+
tag: 'Namespace';
|
|
21
|
+
name: string;
|
|
22
|
+
targetId: string;
|
|
23
|
+
};
|
|
24
|
+
type Kind = 'None' | `Root` | `Builder` | LookupKind;
|
|
25
|
+
type LookupKind = 'ServerFn' | 'Middleware';
|
|
26
|
+
export type LookupConfig = {
|
|
27
|
+
libName: string;
|
|
28
|
+
rootExport: string;
|
|
29
|
+
};
|
|
30
|
+
interface ModuleInfo {
|
|
31
|
+
id: string;
|
|
32
|
+
code: string;
|
|
33
|
+
ast: ReturnType<typeof parseAst>;
|
|
34
|
+
bindings: Map<string, Binding>;
|
|
35
|
+
exports: Map<string, ExportEntry>;
|
|
36
|
+
}
|
|
37
|
+
export declare class ServerFnCompiler {
|
|
38
|
+
private options;
|
|
39
|
+
private moduleCache;
|
|
40
|
+
private initialized;
|
|
41
|
+
constructor(options: {
|
|
42
|
+
env: 'client' | 'server';
|
|
43
|
+
lookupConfigurations: Array<LookupConfig>;
|
|
44
|
+
loadModule: (id: string) => Promise<void>;
|
|
45
|
+
resolveId: (id: string, importer?: string) => Promise<string | null>;
|
|
46
|
+
});
|
|
47
|
+
private init;
|
|
48
|
+
ingestModule({ code, id }: {
|
|
49
|
+
code: string;
|
|
50
|
+
id: string;
|
|
51
|
+
}): ModuleInfo;
|
|
52
|
+
invalidateModule(id: string): boolean;
|
|
53
|
+
compile({ code, id }: {
|
|
54
|
+
code: string;
|
|
55
|
+
id: string;
|
|
56
|
+
}): Promise<import('@tanstack/router-utils').GeneratorResult | null>;
|
|
57
|
+
private collectCandidates;
|
|
58
|
+
private resolveIdentifierKind;
|
|
59
|
+
private resolveBindingKind;
|
|
60
|
+
private resolveExprKind;
|
|
61
|
+
private resolveCalleeKind;
|
|
62
|
+
private getModuleInfo;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
import { parseAst, generateFromAst } from "@tanstack/router-utils";
|
|
3
|
+
import babel__default from "@babel/core";
|
|
4
|
+
import { findReferencedIdentifiers, deadCodeElimination } from "babel-dead-code-elimination";
|
|
5
|
+
import { handleCreateServerFn } from "./handleCreateServerFn.js";
|
|
6
|
+
import { handleCreateMiddleware } from "./handleCreateMiddleware.js";
|
|
7
|
+
const validLookupKinds = ["ServerFn", "Middleware"];
|
|
8
|
+
const candidateCallIdentifier = ["handler", "server", "client"];
|
|
9
|
+
class ServerFnCompiler {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
moduleCache = /* @__PURE__ */ new Map();
|
|
14
|
+
initialized = false;
|
|
15
|
+
async init(id) {
|
|
16
|
+
await Promise.all(
|
|
17
|
+
this.options.lookupConfigurations.map(async (config) => {
|
|
18
|
+
const libId = await this.options.resolveId(config.libName, id);
|
|
19
|
+
if (!libId) {
|
|
20
|
+
throw new Error(`could not resolve "${config.libName}"`);
|
|
21
|
+
}
|
|
22
|
+
let rootModule = this.moduleCache.get(libId);
|
|
23
|
+
if (!rootModule) {
|
|
24
|
+
rootModule = {
|
|
25
|
+
ast: null,
|
|
26
|
+
bindings: /* @__PURE__ */ new Map(),
|
|
27
|
+
exports: /* @__PURE__ */ new Map(),
|
|
28
|
+
code: "",
|
|
29
|
+
id: libId
|
|
30
|
+
};
|
|
31
|
+
this.moduleCache.set(libId, rootModule);
|
|
32
|
+
}
|
|
33
|
+
rootModule.exports.set(config.rootExport, {
|
|
34
|
+
tag: "Normal",
|
|
35
|
+
name: config.rootExport
|
|
36
|
+
});
|
|
37
|
+
rootModule.exports.set("*", {
|
|
38
|
+
tag: "Namespace",
|
|
39
|
+
name: config.rootExport,
|
|
40
|
+
targetId: libId
|
|
41
|
+
});
|
|
42
|
+
rootModule.bindings.set(config.rootExport, {
|
|
43
|
+
type: "var",
|
|
44
|
+
init: t.identifier(config.rootExport),
|
|
45
|
+
resolvedKind: `Root`
|
|
46
|
+
});
|
|
47
|
+
this.moduleCache.set(libId, rootModule);
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
this.initialized = true;
|
|
51
|
+
}
|
|
52
|
+
ingestModule({ code, id }) {
|
|
53
|
+
const ast = parseAst({ code });
|
|
54
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
55
|
+
const exports = /* @__PURE__ */ new Map();
|
|
56
|
+
for (const node of ast.program.body) {
|
|
57
|
+
if (t.isImportDeclaration(node)) {
|
|
58
|
+
const source = node.source.value;
|
|
59
|
+
for (const s of node.specifiers) {
|
|
60
|
+
if (t.isImportSpecifier(s)) {
|
|
61
|
+
const importedName = t.isIdentifier(s.imported) ? s.imported.name : s.imported.value;
|
|
62
|
+
bindings.set(s.local.name, { type: "import", source, importedName });
|
|
63
|
+
} else if (t.isImportDefaultSpecifier(s)) {
|
|
64
|
+
bindings.set(s.local.name, {
|
|
65
|
+
type: "import",
|
|
66
|
+
source,
|
|
67
|
+
importedName: "default"
|
|
68
|
+
});
|
|
69
|
+
} else if (t.isImportNamespaceSpecifier(s)) {
|
|
70
|
+
bindings.set(s.local.name, {
|
|
71
|
+
type: "import",
|
|
72
|
+
source,
|
|
73
|
+
importedName: "*"
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} else if (t.isVariableDeclaration(node)) {
|
|
78
|
+
for (const decl of node.declarations) {
|
|
79
|
+
if (t.isIdentifier(decl.id)) {
|
|
80
|
+
bindings.set(decl.id.name, {
|
|
81
|
+
type: "var",
|
|
82
|
+
init: decl.init ?? null
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} else if (t.isExportNamedDeclaration(node)) {
|
|
87
|
+
if (node.declaration) {
|
|
88
|
+
if (t.isVariableDeclaration(node.declaration)) {
|
|
89
|
+
for (const d of node.declaration.declarations) {
|
|
90
|
+
if (t.isIdentifier(d.id)) {
|
|
91
|
+
exports.set(d.id.name, { tag: "Normal", name: d.id.name });
|
|
92
|
+
bindings.set(d.id.name, { type: "var", init: d.init ?? null });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const sp of node.specifiers) {
|
|
98
|
+
if (t.isExportNamespaceSpecifier(sp)) {
|
|
99
|
+
exports.set(sp.exported.name, {
|
|
100
|
+
tag: "Namespace",
|
|
101
|
+
name: sp.exported.name,
|
|
102
|
+
targetId: node.source?.value || ""
|
|
103
|
+
});
|
|
104
|
+
} else if (t.isExportSpecifier(sp)) {
|
|
105
|
+
const local = sp.local.name;
|
|
106
|
+
const exported = t.isIdentifier(sp.exported) ? sp.exported.name : sp.exported.value;
|
|
107
|
+
exports.set(exported, { tag: "Normal", name: local });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} else if (t.isExportDefaultDeclaration(node)) {
|
|
111
|
+
const d = node.declaration;
|
|
112
|
+
if (t.isIdentifier(d)) {
|
|
113
|
+
exports.set("default", { tag: "Default", name: d.name });
|
|
114
|
+
} else {
|
|
115
|
+
const synth = "__default_export__";
|
|
116
|
+
bindings.set(synth, { type: "var", init: d });
|
|
117
|
+
exports.set("default", { tag: "Default", name: synth });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const info = { code, id, ast, bindings, exports };
|
|
122
|
+
this.moduleCache.set(id, info);
|
|
123
|
+
return info;
|
|
124
|
+
}
|
|
125
|
+
invalidateModule(id) {
|
|
126
|
+
return this.moduleCache.delete(id);
|
|
127
|
+
}
|
|
128
|
+
async compile({ code, id }) {
|
|
129
|
+
if (!this.initialized) {
|
|
130
|
+
await this.init(id);
|
|
131
|
+
}
|
|
132
|
+
const { bindings, ast } = this.ingestModule({ code, id });
|
|
133
|
+
const candidates = this.collectCandidates(bindings);
|
|
134
|
+
if (candidates.length === 0) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const toRewrite = [];
|
|
138
|
+
for (const handler of candidates) {
|
|
139
|
+
const kind = await this.resolveExprKind(handler, id);
|
|
140
|
+
if (validLookupKinds.includes(kind)) {
|
|
141
|
+
toRewrite.push({ callExpression: handler, kind });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (toRewrite.length === 0) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const pathsToRewrite = [];
|
|
148
|
+
babel__default.traverse(ast, {
|
|
149
|
+
CallExpression(path) {
|
|
150
|
+
const found = toRewrite.findIndex((h) => path.node === h.callExpression);
|
|
151
|
+
if (found !== -1) {
|
|
152
|
+
pathsToRewrite.push({ nodePath: path, kind: toRewrite[found].kind });
|
|
153
|
+
toRewrite.splice(found, 1);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
if (toRewrite.length > 0) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Internal error: could not find all paths to rewrite. please file an issue`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
const refIdents = findReferencedIdentifiers(ast);
|
|
163
|
+
pathsToRewrite.map((p) => {
|
|
164
|
+
if (p.kind === "ServerFn") {
|
|
165
|
+
handleCreateServerFn(p.nodePath, { env: this.options.env, code });
|
|
166
|
+
} else {
|
|
167
|
+
handleCreateMiddleware(p.nodePath, { env: this.options.env });
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
deadCodeElimination(ast, refIdents);
|
|
171
|
+
return generateFromAst(ast, {
|
|
172
|
+
sourceMaps: true,
|
|
173
|
+
sourceFileName: id,
|
|
174
|
+
filename: id
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
// collects all candidate CallExpressions at top-level
|
|
178
|
+
collectCandidates(bindings) {
|
|
179
|
+
const candidates = [];
|
|
180
|
+
for (const binding of bindings.values()) {
|
|
181
|
+
if (binding.type === "var") {
|
|
182
|
+
const handler = isCandidateCallExpression(binding.init);
|
|
183
|
+
if (handler) {
|
|
184
|
+
candidates.push(handler);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return candidates;
|
|
189
|
+
}
|
|
190
|
+
async resolveIdentifierKind(ident, id, visited = /* @__PURE__ */ new Set()) {
|
|
191
|
+
const info = await this.getModuleInfo(id);
|
|
192
|
+
const binding = info.bindings.get(ident);
|
|
193
|
+
if (!binding) {
|
|
194
|
+
return "None";
|
|
195
|
+
}
|
|
196
|
+
if (binding.resolvedKind) {
|
|
197
|
+
return binding.resolvedKind;
|
|
198
|
+
}
|
|
199
|
+
const vKey = `${id}:${ident}`;
|
|
200
|
+
if (visited.has(vKey)) {
|
|
201
|
+
return "None";
|
|
202
|
+
}
|
|
203
|
+
visited.add(vKey);
|
|
204
|
+
const resolvedKind = await this.resolveBindingKind(binding, id, visited);
|
|
205
|
+
binding.resolvedKind = resolvedKind;
|
|
206
|
+
return resolvedKind;
|
|
207
|
+
}
|
|
208
|
+
async resolveBindingKind(binding, fileId, visited = /* @__PURE__ */ new Set()) {
|
|
209
|
+
if (binding.resolvedKind) {
|
|
210
|
+
return binding.resolvedKind;
|
|
211
|
+
}
|
|
212
|
+
if (binding.type === "import") {
|
|
213
|
+
const target = await this.options.resolveId(binding.source, fileId);
|
|
214
|
+
if (!target) {
|
|
215
|
+
return "None";
|
|
216
|
+
}
|
|
217
|
+
const importedModule = await this.getModuleInfo(target);
|
|
218
|
+
const moduleExport = importedModule.exports.get(binding.importedName);
|
|
219
|
+
if (!moduleExport) {
|
|
220
|
+
return "None";
|
|
221
|
+
}
|
|
222
|
+
const importedBinding = importedModule.bindings.get(moduleExport.name);
|
|
223
|
+
if (!importedBinding) {
|
|
224
|
+
return "None";
|
|
225
|
+
}
|
|
226
|
+
if (importedBinding.resolvedKind) {
|
|
227
|
+
return importedBinding.resolvedKind;
|
|
228
|
+
}
|
|
229
|
+
const resolvedKind2 = await this.resolveBindingKind(
|
|
230
|
+
importedBinding,
|
|
231
|
+
importedModule.id,
|
|
232
|
+
visited
|
|
233
|
+
);
|
|
234
|
+
importedBinding.resolvedKind = resolvedKind2;
|
|
235
|
+
return resolvedKind2;
|
|
236
|
+
}
|
|
237
|
+
const resolvedKind = await this.resolveExprKind(
|
|
238
|
+
binding.init,
|
|
239
|
+
fileId,
|
|
240
|
+
visited
|
|
241
|
+
);
|
|
242
|
+
binding.resolvedKind = resolvedKind;
|
|
243
|
+
return resolvedKind;
|
|
244
|
+
}
|
|
245
|
+
async resolveExprKind(expr, fileId, visited = /* @__PURE__ */ new Set()) {
|
|
246
|
+
if (!expr) {
|
|
247
|
+
return "None";
|
|
248
|
+
}
|
|
249
|
+
let result = "None";
|
|
250
|
+
if (t.isCallExpression(expr)) {
|
|
251
|
+
if (!t.isExpression(expr.callee)) {
|
|
252
|
+
return "None";
|
|
253
|
+
}
|
|
254
|
+
const calleeKind = await this.resolveCalleeKind(
|
|
255
|
+
expr.callee,
|
|
256
|
+
fileId,
|
|
257
|
+
visited
|
|
258
|
+
);
|
|
259
|
+
if (calleeKind !== "None") {
|
|
260
|
+
if (calleeKind === `Root` || calleeKind === `Builder`) {
|
|
261
|
+
return `Builder`;
|
|
262
|
+
}
|
|
263
|
+
for (const kind of validLookupKinds) {
|
|
264
|
+
if (calleeKind === kind) {
|
|
265
|
+
return kind;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
} else if (t.isMemberExpression(expr) && t.isIdentifier(expr.property)) {
|
|
270
|
+
result = await this.resolveCalleeKind(expr.object, fileId, visited);
|
|
271
|
+
}
|
|
272
|
+
if (result === "None" && t.isIdentifier(expr)) {
|
|
273
|
+
result = await this.resolveIdentifierKind(expr.name, fileId, visited);
|
|
274
|
+
}
|
|
275
|
+
if (result === "None" && t.isTSAsExpression(expr)) {
|
|
276
|
+
result = await this.resolveExprKind(expr.expression, fileId, visited);
|
|
277
|
+
}
|
|
278
|
+
if (result === "None" && t.isTSNonNullExpression(expr)) {
|
|
279
|
+
result = await this.resolveExprKind(expr.expression, fileId, visited);
|
|
280
|
+
}
|
|
281
|
+
if (result === "None" && t.isParenthesizedExpression(expr)) {
|
|
282
|
+
result = await this.resolveExprKind(expr.expression, fileId, visited);
|
|
283
|
+
}
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
async resolveCalleeKind(callee, fileId, visited = /* @__PURE__ */ new Set()) {
|
|
287
|
+
if (t.isIdentifier(callee)) {
|
|
288
|
+
return this.resolveIdentifierKind(callee.name, fileId, visited);
|
|
289
|
+
}
|
|
290
|
+
if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
|
|
291
|
+
const prop = callee.property.name;
|
|
292
|
+
if (prop === "handler") {
|
|
293
|
+
const base = await this.resolveExprKind(callee.object, fileId, visited);
|
|
294
|
+
if (base === "Root" || base === "Builder") {
|
|
295
|
+
return "ServerFn";
|
|
296
|
+
}
|
|
297
|
+
return "None";
|
|
298
|
+
} else if (prop === "client" || prop === "server" || prop === "createMiddleware") {
|
|
299
|
+
const base = await this.resolveExprKind(callee.object, fileId, visited);
|
|
300
|
+
if (base === "Root" || base === "Builder" || base === "Middleware") {
|
|
301
|
+
return "Middleware";
|
|
302
|
+
}
|
|
303
|
+
return "None";
|
|
304
|
+
}
|
|
305
|
+
if (t.isIdentifier(callee.object)) {
|
|
306
|
+
const info = await this.getModuleInfo(fileId);
|
|
307
|
+
const binding = info.bindings.get(callee.object.name);
|
|
308
|
+
if (binding && binding.type === "import" && binding.importedName === "*") {
|
|
309
|
+
const targetModuleId = await this.options.resolveId(
|
|
310
|
+
binding.source,
|
|
311
|
+
fileId
|
|
312
|
+
);
|
|
313
|
+
if (targetModuleId) {
|
|
314
|
+
const targetModule = await this.getModuleInfo(targetModuleId);
|
|
315
|
+
const exportEntry = targetModule.exports.get(callee.property.name);
|
|
316
|
+
if (exportEntry) {
|
|
317
|
+
const exportedBinding = targetModule.bindings.get(
|
|
318
|
+
exportEntry.name
|
|
319
|
+
);
|
|
320
|
+
if (exportedBinding) {
|
|
321
|
+
return await this.resolveBindingKind(
|
|
322
|
+
exportedBinding,
|
|
323
|
+
targetModule.id,
|
|
324
|
+
visited
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
return "None";
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return this.resolveExprKind(callee.object, fileId, visited);
|
|
334
|
+
}
|
|
335
|
+
return this.resolveExprKind(callee, fileId, visited);
|
|
336
|
+
}
|
|
337
|
+
async getModuleInfo(id) {
|
|
338
|
+
let cached = this.moduleCache.get(id);
|
|
339
|
+
if (cached) {
|
|
340
|
+
return cached;
|
|
341
|
+
}
|
|
342
|
+
await this.options.loadModule(id);
|
|
343
|
+
cached = this.moduleCache.get(id);
|
|
344
|
+
if (!cached) {
|
|
345
|
+
throw new Error(`could not load module info for ${id}`);
|
|
346
|
+
}
|
|
347
|
+
return cached;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function isCandidateCallExpression(node) {
|
|
351
|
+
if (!t.isCallExpression(node)) return void 0;
|
|
352
|
+
const callee = node.callee;
|
|
353
|
+
if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) {
|
|
354
|
+
return void 0;
|
|
355
|
+
}
|
|
356
|
+
if (!candidateCallIdentifier.includes(callee.property.name)) {
|
|
357
|
+
return void 0;
|
|
358
|
+
}
|
|
359
|
+
return node;
|
|
360
|
+
}
|
|
361
|
+
export {
|
|
362
|
+
ServerFnCompiler
|
|
363
|
+
};
|
|
364
|
+
//# sourceMappingURL=compiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.js","sources":["../../../src/create-server-fn-plugin/compiler.ts"],"sourcesContent":["/* eslint-disable import/no-commonjs */\nimport * as t from '@babel/types'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport babel from '@babel/core'\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { handleCreateServerFn } from './handleCreateServerFn'\nimport { handleCreateMiddleware } from './handleCreateMiddleware'\n\ntype Binding =\n | {\n type: 'import'\n source: string\n importedName: string\n resolvedKind?: Kind\n }\n | {\n type: 'var'\n init: t.Expression | null\n resolvedKind?: Kind\n }\n\ntype ExportEntry =\n | { tag: 'Normal'; name: string }\n | { tag: 'Default'; name: string }\n | { tag: 'Namespace'; name: string; targetId: string } // for `export * as ns from './x'`\n\ntype Kind = 'None' | `Root` | `Builder` | LookupKind\n\ntype LookupKind = 'ServerFn' | 'Middleware'\n\nconst validLookupKinds: Array<LookupKind> = ['ServerFn', 'Middleware']\nconst candidateCallIdentifier = ['handler', 'server', 'client']\nexport type LookupConfig = {\n libName: string\n rootExport: string\n}\ninterface ModuleInfo {\n id: string\n code: string\n ast: ReturnType<typeof parseAst>\n bindings: Map<string, Binding>\n exports: Map<string, ExportEntry>\n}\n\nexport class ServerFnCompiler {\n private moduleCache = new Map<string, ModuleInfo>()\n private initialized = false\n constructor(\n private options: {\n env: 'client' | 'server'\n lookupConfigurations: Array<LookupConfig>\n loadModule: (id: string) => Promise<void>\n resolveId: (id: string, importer?: string) => Promise<string | null>\n },\n ) {}\n\n private async init(id: string) {\n await Promise.all(\n this.options.lookupConfigurations.map(async (config) => {\n const libId = await this.options.resolveId(config.libName, id)\n if (!libId) {\n throw new Error(`could not resolve \"${config.libName}\"`)\n }\n let rootModule = this.moduleCache.get(libId)\n if (!rootModule) {\n // insert root binding\n rootModule = {\n ast: null as any,\n bindings: new Map(),\n exports: new Map(),\n code: '',\n id: libId,\n }\n this.moduleCache.set(libId, rootModule)\n }\n\n rootModule.exports.set(config.rootExport, {\n tag: 'Normal',\n name: config.rootExport,\n })\n rootModule.exports.set('*', {\n tag: 'Namespace',\n name: config.rootExport,\n targetId: libId,\n })\n rootModule.bindings.set(config.rootExport, {\n type: 'var',\n init: t.identifier(config.rootExport),\n resolvedKind: `Root` satisfies Kind,\n })\n this.moduleCache.set(libId, rootModule)\n }),\n )\n\n this.initialized = true\n }\n\n public ingestModule({ code, id }: { code: string; id: string }) {\n const ast = parseAst({ code })\n\n const bindings = new Map<string, Binding>()\n const exports = new Map<string, ExportEntry>()\n\n // we are only interested in top-level bindings, hence we don't traverse the AST\n // instead we only iterate over the program body\n for (const node of ast.program.body) {\n if (t.isImportDeclaration(node)) {\n const source = node.source.value\n for (const s of node.specifiers) {\n if (t.isImportSpecifier(s)) {\n const importedName = t.isIdentifier(s.imported)\n ? s.imported.name\n : s.imported.value\n bindings.set(s.local.name, { type: 'import', source, importedName })\n } else if (t.isImportDefaultSpecifier(s)) {\n bindings.set(s.local.name, {\n type: 'import',\n source,\n importedName: 'default',\n })\n } else if (t.isImportNamespaceSpecifier(s)) {\n bindings.set(s.local.name, {\n type: 'import',\n source,\n importedName: '*',\n })\n }\n }\n } else if (t.isVariableDeclaration(node)) {\n for (const decl of node.declarations) {\n if (t.isIdentifier(decl.id)) {\n bindings.set(decl.id.name, {\n type: 'var',\n init: decl.init ?? null,\n })\n }\n }\n } else if (t.isExportNamedDeclaration(node)) {\n // export const foo = ...\n if (node.declaration) {\n if (t.isVariableDeclaration(node.declaration)) {\n for (const d of node.declaration.declarations) {\n if (t.isIdentifier(d.id)) {\n exports.set(d.id.name, { tag: 'Normal', name: d.id.name })\n bindings.set(d.id.name, { type: 'var', init: d.init ?? null })\n }\n }\n }\n }\n for (const sp of node.specifiers) {\n if (t.isExportNamespaceSpecifier(sp)) {\n exports.set(sp.exported.name, {\n tag: 'Namespace',\n name: sp.exported.name,\n targetId: node.source?.value || '',\n })\n }\n // export { local as exported }\n else if (t.isExportSpecifier(sp)) {\n const local = sp.local.name\n const exported = t.isIdentifier(sp.exported)\n ? sp.exported.name\n : sp.exported.value\n exports.set(exported, { tag: 'Normal', name: local })\n }\n }\n } else if (t.isExportDefaultDeclaration(node)) {\n const d = node.declaration\n if (t.isIdentifier(d)) {\n exports.set('default', { tag: 'Default', name: d.name })\n } else {\n const synth = '__default_export__'\n bindings.set(synth, { type: 'var', init: d as t.Expression })\n exports.set('default', { tag: 'Default', name: synth })\n }\n }\n }\n\n const info: ModuleInfo = { code, id, ast, bindings, exports }\n this.moduleCache.set(id, info)\n return info\n }\n\n public invalidateModule(id: string) {\n return this.moduleCache.delete(id)\n }\n\n public async compile({ code, id }: { code: string; id: string }) {\n if (!this.initialized) {\n await this.init(id)\n }\n const { bindings, ast } = this.ingestModule({ code, id })\n const candidates = this.collectCandidates(bindings)\n if (candidates.length === 0) {\n // this hook will only be invoked if there is `.handler(` | `.server(` | `.client(` in the code,\n // so not discovering a handler candidate is rather unlikely, but maybe possible?\n return null\n }\n\n // let's find out which of the candidates are actually server functions\n const toRewrite: Array<{\n callExpression: t.CallExpression\n kind: LookupKind\n }> = []\n for (const handler of candidates) {\n const kind = await this.resolveExprKind(handler, id)\n if (validLookupKinds.includes(kind as LookupKind)) {\n toRewrite.push({ callExpression: handler, kind: kind as LookupKind })\n }\n }\n if (toRewrite.length === 0) {\n return null\n }\n\n const pathsToRewrite: Array<{\n nodePath: babel.NodePath<t.CallExpression>\n kind: LookupKind\n }> = []\n babel.traverse(ast, {\n CallExpression(path) {\n const found = toRewrite.findIndex((h) => path.node === h.callExpression)\n if (found !== -1) {\n pathsToRewrite.push({ nodePath: path, kind: toRewrite[found]!.kind })\n // delete from toRewrite\n toRewrite.splice(found, 1)\n }\n },\n })\n\n if (toRewrite.length > 0) {\n throw new Error(\n `Internal error: could not find all paths to rewrite. please file an issue`,\n )\n }\n\n const refIdents = findReferencedIdentifiers(ast)\n\n pathsToRewrite.map((p) => {\n if (p.kind === 'ServerFn') {\n handleCreateServerFn(p.nodePath, { env: this.options.env, code })\n } else {\n handleCreateMiddleware(p.nodePath, { env: this.options.env })\n }\n })\n\n deadCodeElimination(ast, refIdents)\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: id,\n filename: id,\n })\n }\n\n // collects all candidate CallExpressions at top-level\n private collectCandidates(bindings: Map<string, Binding>) {\n const candidates: Array<t.CallExpression> = []\n\n for (const binding of bindings.values()) {\n if (binding.type === 'var') {\n const handler = isCandidateCallExpression(binding.init)\n if (handler) {\n candidates.push(handler)\n }\n }\n }\n return candidates\n }\n\n private async resolveIdentifierKind(\n ident: string,\n id: string,\n visited = new Set<string>(),\n ): Promise<Kind> {\n const info = await this.getModuleInfo(id)\n\n const binding = info.bindings.get(ident)\n if (!binding) {\n return 'None'\n }\n if (binding.resolvedKind) {\n return binding.resolvedKind\n }\n\n // TODO improve cycle detection? should we throw here instead of returning 'None'?\n // prevent cycles\n const vKey = `${id}:${ident}`\n if (visited.has(vKey)) {\n return 'None'\n }\n visited.add(vKey)\n\n const resolvedKind = await this.resolveBindingKind(binding, id, visited)\n binding.resolvedKind = resolvedKind\n return resolvedKind\n }\n\n private async resolveBindingKind(\n binding: Binding,\n fileId: string,\n visited = new Set<string>(),\n ): Promise<Kind> {\n if (binding.resolvedKind) {\n return binding.resolvedKind\n }\n if (binding.type === 'import') {\n const target = await this.options.resolveId(binding.source, fileId)\n if (!target) {\n return 'None'\n }\n\n const importedModule = await this.getModuleInfo(target)\n\n const moduleExport = importedModule.exports.get(binding.importedName)\n if (!moduleExport) {\n return 'None'\n }\n const importedBinding = importedModule.bindings.get(moduleExport.name)\n if (!importedBinding) {\n return 'None'\n }\n if (importedBinding.resolvedKind) {\n return importedBinding.resolvedKind\n }\n\n const resolvedKind = await this.resolveBindingKind(\n importedBinding,\n importedModule.id,\n visited,\n )\n importedBinding.resolvedKind = resolvedKind\n return resolvedKind\n }\n\n const resolvedKind = await this.resolveExprKind(\n binding.init,\n fileId,\n visited,\n )\n binding.resolvedKind = resolvedKind\n return resolvedKind\n }\n\n private async resolveExprKind(\n expr: t.Expression | null,\n fileId: string,\n visited = new Set<string>(),\n ): Promise<Kind> {\n if (!expr) {\n return 'None'\n }\n\n let result: Kind = 'None'\n\n if (t.isCallExpression(expr)) {\n if (!t.isExpression(expr.callee)) {\n return 'None'\n }\n const calleeKind = await this.resolveCalleeKind(\n expr.callee,\n fileId,\n visited,\n )\n if (calleeKind !== 'None') {\n if (calleeKind === `Root` || calleeKind === `Builder`) {\n return `Builder`\n }\n for (const kind of validLookupKinds) {\n if (calleeKind === kind) {\n return kind\n }\n }\n }\n } else if (t.isMemberExpression(expr) && t.isIdentifier(expr.property)) {\n result = await this.resolveCalleeKind(expr.object, fileId, visited)\n }\n\n if (result === 'None' && t.isIdentifier(expr)) {\n result = await this.resolveIdentifierKind(expr.name, fileId, visited)\n }\n\n if (result === 'None' && t.isTSAsExpression(expr)) {\n result = await this.resolveExprKind(expr.expression, fileId, visited)\n }\n if (result === 'None' && t.isTSNonNullExpression(expr)) {\n result = await this.resolveExprKind(expr.expression, fileId, visited)\n }\n if (result === 'None' && t.isParenthesizedExpression(expr)) {\n result = await this.resolveExprKind(expr.expression, fileId, visited)\n }\n\n return result\n }\n\n private async resolveCalleeKind(\n callee: t.Expression,\n fileId: string,\n visited = new Set<string>(),\n ): Promise<Kind> {\n if (t.isIdentifier(callee)) {\n return this.resolveIdentifierKind(callee.name, fileId, visited)\n }\n\n if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {\n const prop = callee.property.name\n\n if (prop === 'handler') {\n const base = await this.resolveExprKind(callee.object, fileId, visited)\n if (base === 'Root' || base === 'Builder') {\n return 'ServerFn'\n }\n return 'None'\n } else if (\n prop === 'client' ||\n prop === 'server' ||\n prop === 'createMiddleware'\n ) {\n const base = await this.resolveExprKind(callee.object, fileId, visited)\n if (base === 'Root' || base === 'Builder' || base === 'Middleware') {\n return 'Middleware'\n }\n return 'None'\n }\n // Check if the object is a namespace import\n if (t.isIdentifier(callee.object)) {\n const info = await this.getModuleInfo(fileId)\n const binding = info.bindings.get(callee.object.name)\n if (\n binding &&\n binding.type === 'import' &&\n binding.importedName === '*'\n ) {\n // resolve the property from the target module\n const targetModuleId = await this.options.resolveId(\n binding.source,\n fileId,\n )\n if (targetModuleId) {\n const targetModule = await this.getModuleInfo(targetModuleId)\n const exportEntry = targetModule.exports.get(callee.property.name)\n if (exportEntry) {\n const exportedBinding = targetModule.bindings.get(\n exportEntry.name,\n )\n if (exportedBinding) {\n return await this.resolveBindingKind(\n exportedBinding,\n targetModule.id,\n visited,\n )\n }\n }\n } else {\n return 'None'\n }\n }\n }\n return this.resolveExprKind(callee.object, fileId, visited)\n }\n\n // handle nested expressions\n return this.resolveExprKind(callee, fileId, visited)\n }\n\n private async getModuleInfo(id: string) {\n let cached = this.moduleCache.get(id)\n if (cached) {\n return cached\n }\n\n await this.options.loadModule(id)\n\n cached = this.moduleCache.get(id)\n if (!cached) {\n throw new Error(`could not load module info for ${id}`)\n }\n return cached\n }\n}\n\nfunction isCandidateCallExpression(\n node: t.Node | null | undefined,\n): undefined | t.CallExpression {\n if (!t.isCallExpression(node)) return undefined\n\n const callee = node.callee\n if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) {\n return undefined\n }\n if (!candidateCallIdentifier.includes(callee.property.name)) {\n return undefined\n }\n\n return node\n}\n"],"names":["babel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,SAAS;AAAA,gBAC5C,YAAY;AAAA,cAAA;AAEd,kBAAI,iBAAiB;AACnB,uBAAO,MAAM,KAAK;AAAA,kBAChB;AAAA,kBACA,aAAa;AAAA,kBACb;AAAA,gBAAA;AAAA,cAEJ;AAAA,YACF;AAAA,UACF,OAAO;AACL,mBAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF;AACA,aAAO,KAAK,gBAAgB,OAAO,QAAQ,QAAQ,OAAO;AAAA,IAC5D;AAGA,WAAO,KAAK,gBAAgB,QAAQ,QAAQ,OAAO;AAAA,EACrD;AAAA,EAEA,MAAc,cAAc,IAAY;AACtC,QAAI,SAAS,KAAK,YAAY,IAAI,EAAE;AACpC,QAAI,QAAQ;AACV,aAAO;AAAA,IACT;AAEA,UAAM,KAAK,QAAQ,WAAW,EAAE;AAEhC,aAAS,KAAK,YAAY,IAAI,EAAE;AAChC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,kCAAkC,EAAE,EAAE;AAAA,IACxD;AACA,WAAO;AAAA,EACT;AACF;AAEA,SAAS,0BACP,MAC8B;AAC9B,MAAI,CAAC,EAAE,iBAAiB,IAAI,EAAG,QAAO;AAEtC,QAAM,SAAS,KAAK;AACpB,MAAI,CAAC,EAAE,mBAAmB,MAAM,KAAK,CAAC,EAAE,aAAa,OAAO,QAAQ,GAAG;AACrE,WAAO;AAAA,EACT;AACA,MAAI,CAAC,wBAAwB,SAAS,OAAO,SAAS,IAAI,GAAG;AAC3D,WAAO;AAAA,EACT;AAEA,SAAO;AACT;"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as t from "@babel/types";
|
|
2
|
-
import { getRootCallExpression } from "
|
|
3
|
-
function
|
|
2
|
+
import { getRootCallExpression } from "../start-compiler-plugin/utils.js";
|
|
3
|
+
function handleCreateMiddleware(path, opts) {
|
|
4
4
|
const rootCallExpression = getRootCallExpression(path);
|
|
5
5
|
const callExpressionPaths = {
|
|
6
6
|
middleware: null,
|
|
7
|
-
|
|
7
|
+
inputValidator: null,
|
|
8
8
|
client: null,
|
|
9
9
|
server: null
|
|
10
10
|
};
|
|
@@ -19,17 +19,17 @@ function handleCreateMiddlewareCallExpression(path, opts) {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
|
-
if (callExpressionPaths.
|
|
23
|
-
const innerInputExpression = callExpressionPaths.
|
|
22
|
+
if (callExpressionPaths.inputValidator) {
|
|
23
|
+
const innerInputExpression = callExpressionPaths.inputValidator.node.arguments[0];
|
|
24
24
|
if (!innerInputExpression) {
|
|
25
25
|
throw new Error(
|
|
26
|
-
"createMiddleware().
|
|
26
|
+
"createMiddleware().inputValidator() must be called with a validator!"
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
29
|
if (opts.env === "client") {
|
|
30
|
-
if (t.isMemberExpression(callExpressionPaths.
|
|
31
|
-
callExpressionPaths.
|
|
32
|
-
callExpressionPaths.
|
|
30
|
+
if (t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)) {
|
|
31
|
+
callExpressionPaths.inputValidator.replaceWith(
|
|
32
|
+
callExpressionPaths.inputValidator.node.callee.object
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -46,6 +46,6 @@ function handleCreateMiddlewareCallExpression(path, opts) {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
export {
|
|
49
|
-
|
|
49
|
+
handleCreateMiddleware
|
|
50
50
|
};
|
|
51
|
-
//# sourceMappingURL=
|
|
51
|
+
//# sourceMappingURL=handleCreateMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleCreateMiddleware.js","sources":["../../../src/create-server-fn-plugin/handleCreateMiddleware.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { getRootCallExpression } from '../start-compiler-plugin/utils'\nimport type * as babel from '@babel/core'\n\nexport function handleCreateMiddleware(\n path: babel.NodePath<t.CallExpression>,\n opts: {\n env: 'client' | 'server'\n },\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n inputValidator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.inputValidator) {\n const innerInputExpression =\n callExpressionPaths.inputValidator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().inputValidator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (\n t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)\n ) {\n callExpressionPaths.inputValidator.replaceWith(\n callExpressionPaths.inputValidator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n"],"names":[],"mappings":";;AAIO,SAAS,uBACd,MACA,MAGA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,YAAY;AAAA,IACZ,gBAAgB;AAAA,IAChB,QAAQ;AAAA,IACR,QAAQ;AAAA,EAAA;AAGV,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SACpC;AAEH,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MAAI,oBAAoB,gBAAgB;AACtC,UAAM,uBACJ,oBAAoB,eAAe,KAAK,UAAU,CAAC;AAErD,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QAAI,KAAK,QAAQ,UAAU;AACzB,UACE,EAAE,mBAAmB,oBAAoB,eAAe,KAAK,MAAM,GACnE;AACA,4BAAoB,eAAe;AAAA,UACjC,oBAAoB,eAAe,KAAK,OAAO;AAAA,QAAA;AAAA,MAEnD;AAAA,IACF;AAAA,EACF;AAEA,QAAM,eAAe,oBAAoB,QAAQ;AAAA,IAC/C;AAAA,EAAA;AAGF,MACE,oBAAoB,UACpB,aAAa,QACb,KAAK,QAAQ,UACb;AAEA,QAAI,EAAE,mBAAmB,oBAAoB,OAAO,KAAK,MAAM,GAAG;AAChE,0BAAoB,OAAO;AAAA,QACzB,oBAAoB,OAAO,KAAK,OAAO;AAAA,MAAA;AAAA,IAE3C;AAAA,EACF;AACF;"}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import * as t from "@babel/types";
|
|
2
|
-
import { getRootCallExpression, codeFrameError } from "
|
|
3
|
-
function
|
|
4
|
-
const
|
|
5
|
-
const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {
|
|
6
|
-
return t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "validateClient" && t.isBooleanLiteral(prop.value) && prop.value.value === true;
|
|
7
|
-
});
|
|
2
|
+
import { getRootCallExpression, codeFrameError } from "../start-compiler-plugin/utils.js";
|
|
3
|
+
function handleCreateServerFn(path, opts) {
|
|
4
|
+
const validMethods = ["middleware", "inputValidator", "handler"];
|
|
8
5
|
const callExpressionPaths = {
|
|
9
6
|
middleware: null,
|
|
10
|
-
|
|
7
|
+
inputValidator: null,
|
|
11
8
|
handler: null
|
|
12
9
|
};
|
|
13
|
-
const validMethods = Object.keys(callExpressionPaths);
|
|
14
10
|
const rootCallExpression = getRootCallExpression(path);
|
|
15
11
|
if (!rootCallExpression.parentPath.isVariableDeclarator()) {
|
|
16
12
|
throw new Error("createServerFn must be assigned to a variable!");
|
|
@@ -27,17 +23,19 @@ function handleCreateServerFnCallExpression(path, opts) {
|
|
|
27
23
|
}
|
|
28
24
|
}
|
|
29
25
|
});
|
|
30
|
-
if (callExpressionPaths.
|
|
31
|
-
const innerInputExpression = callExpressionPaths.
|
|
26
|
+
if (callExpressionPaths.inputValidator) {
|
|
27
|
+
const innerInputExpression = callExpressionPaths.inputValidator.node.arguments[0];
|
|
32
28
|
if (!innerInputExpression) {
|
|
33
29
|
throw new Error(
|
|
34
|
-
"createServerFn().
|
|
30
|
+
"createServerFn().inputValidator() must be called with a validator!"
|
|
35
31
|
);
|
|
36
32
|
}
|
|
37
|
-
if (opts.env === "client"
|
|
38
|
-
callExpressionPaths.
|
|
39
|
-
callExpressionPaths.
|
|
40
|
-
|
|
33
|
+
if (opts.env === "client") {
|
|
34
|
+
if (t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)) {
|
|
35
|
+
callExpressionPaths.inputValidator.replaceWith(
|
|
36
|
+
callExpressionPaths.inputValidator.node.callee.object
|
|
37
|
+
);
|
|
38
|
+
}
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
const handlerFnPath = callExpressionPaths.handler?.get(
|
|
@@ -82,6 +80,6 @@ function handleCreateServerFnCallExpression(path, opts) {
|
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
82
|
export {
|
|
85
|
-
|
|
83
|
+
handleCreateServerFn
|
|
86
84
|
};
|
|
87
|
-
//# sourceMappingURL=
|
|
85
|
+
//# sourceMappingURL=handleCreateServerFn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleCreateServerFn.js","sources":["../../../src/create-server-fn-plugin/handleCreateServerFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n codeFrameError,\n getRootCallExpression,\n} from '../start-compiler-plugin/utils'\nimport type * as babel from '@babel/core'\n\nexport function handleCreateServerFn(\n path: babel.NodePath<t.CallExpression>,\n opts: {\n env: 'client' | 'server'\n code: string\n },\n) {\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const validMethods = ['middleware', 'inputValidator', 'handler'] as const\n type ValidMethods = (typeof validMethods)[number]\n const callExpressionPaths: Record<\n ValidMethods,\n babel.NodePath<t.CallExpression> | null\n > = {\n middleware: null,\n inputValidator: null,\n handler: null,\n }\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property.name as ValidMethods\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.inputValidator) {\n const innerInputExpression =\n callExpressionPaths.inputValidator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().inputValidator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (\n t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)\n ) {\n callExpressionPaths.inputValidator.replaceWith(\n callExpressionPaths.inputValidator.node.callee.object,\n )\n }\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push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