@tanstack/start-plugin-core 1.132.0-alpha.7 → 1.132.0-alpha.9

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.
Files changed (29) hide show
  1. package/dist/esm/create-server-fn-plugin/compiler.d.ts +61 -0
  2. package/dist/esm/create-server-fn-plugin/compiler.js +336 -0
  3. package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -0
  4. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.d.ts +6 -0
  5. package/dist/esm/{start-compiler-plugin/serverFn.js → create-server-fn-plugin/handleCreateServerFn.js} +11 -13
  6. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -0
  7. package/dist/esm/create-server-fn-plugin/plugin.d.ts +3 -0
  8. package/dist/esm/create-server-fn-plugin/plugin.js +113 -0
  9. package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -0
  10. package/dist/esm/plugin.js +3 -4
  11. package/dist/esm/plugin.js.map +1 -1
  12. package/dist/esm/start-compiler-plugin/compilers.js +0 -6
  13. package/dist/esm/start-compiler-plugin/compilers.js.map +1 -1
  14. package/dist/esm/start-compiler-plugin/constants.d.ts +1 -1
  15. package/dist/esm/start-compiler-plugin/constants.js +0 -1
  16. package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
  17. package/dist/esm/start-compiler-plugin/plugin.d.ts +1 -8
  18. package/dist/esm/start-compiler-plugin/plugin.js +6 -13
  19. package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
  20. package/package.json +7 -7
  21. package/src/create-server-fn-plugin/compiler.ts +456 -0
  22. package/src/{start-compiler-plugin/serverFn.ts → create-server-fn-plugin/handleCreateServerFn.ts} +26 -36
  23. package/src/create-server-fn-plugin/plugin.ts +138 -0
  24. package/src/plugin.ts +3 -4
  25. package/src/start-compiler-plugin/compilers.ts +0 -6
  26. package/src/start-compiler-plugin/constants.ts +0 -1
  27. package/src/start-compiler-plugin/plugin.ts +7 -22
  28. package/dist/esm/start-compiler-plugin/serverFn.d.ts +0 -4
  29. package/dist/esm/start-compiler-plugin/serverFn.js.map +0 -1
@@ -0,0 +1,61 @@
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' | 'ServerFn';
25
+ interface ModuleInfo {
26
+ id: string;
27
+ code: string;
28
+ ast: ReturnType<typeof parseAst>;
29
+ bindings: Map<string, Binding>;
30
+ exports: Map<string, ExportEntry>;
31
+ }
32
+ export declare class ServerFnCompiler {
33
+ private options;
34
+ private moduleCache;
35
+ private resolvedLibId;
36
+ private initialized;
37
+ constructor(options: {
38
+ env: 'client' | 'server';
39
+ libName: string;
40
+ rootExport: string;
41
+ loadModule: (id: string) => Promise<void>;
42
+ resolveId: (id: string, importer?: string) => Promise<string | null>;
43
+ });
44
+ private init;
45
+ ingestModule({ code, id }: {
46
+ code: string;
47
+ id: string;
48
+ }): ModuleInfo;
49
+ invalidateModule(id: string): boolean;
50
+ compile({ code, id }: {
51
+ code: string;
52
+ id: string;
53
+ }): Promise<import('@tanstack/router-utils').GeneratorResult | null>;
54
+ private collectHandlerCandidates;
55
+ private resolveIdentifierKind;
56
+ private resolveBindingKind;
57
+ private resolveExprKind;
58
+ private resolveCalleeKind;
59
+ private getModuleInfo;
60
+ }
61
+ export {};
@@ -0,0 +1,336 @@
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
+ class ServerFnCompiler {
7
+ constructor(options) {
8
+ this.options = options;
9
+ }
10
+ moduleCache = /* @__PURE__ */ new Map();
11
+ resolvedLibId;
12
+ initialized = false;
13
+ async init(id) {
14
+ const libId = await this.options.resolveId(this.options.libName, id);
15
+ if (!libId) {
16
+ throw new Error(`could not resolve "${this.options.libName}"`);
17
+ }
18
+ const rootModule = {
19
+ ast: null,
20
+ bindings: /* @__PURE__ */ new Map(),
21
+ exports: /* @__PURE__ */ new Map(),
22
+ code: "",
23
+ id: libId
24
+ };
25
+ rootModule.exports.set(this.options.rootExport, {
26
+ tag: "Normal",
27
+ name: this.options.rootExport
28
+ });
29
+ rootModule.bindings.set(this.options.rootExport, {
30
+ type: "var",
31
+ init: t.identifier(this.options.rootExport),
32
+ resolvedKind: "Root"
33
+ });
34
+ this.moduleCache.set(libId, rootModule);
35
+ this.initialized = true;
36
+ this.resolvedLibId = libId;
37
+ }
38
+ ingestModule({ code, id }) {
39
+ const ast = parseAst({ code });
40
+ const bindings = /* @__PURE__ */ new Map();
41
+ const exports = /* @__PURE__ */ new Map();
42
+ for (const node of ast.program.body) {
43
+ if (t.isImportDeclaration(node)) {
44
+ const source = node.source.value;
45
+ for (const s of node.specifiers) {
46
+ if (t.isImportSpecifier(s)) {
47
+ const importedName = t.isIdentifier(s.imported) ? s.imported.name : s.imported.value;
48
+ bindings.set(s.local.name, { type: "import", source, importedName });
49
+ } else if (t.isImportDefaultSpecifier(s)) {
50
+ bindings.set(s.local.name, {
51
+ type: "import",
52
+ source,
53
+ importedName: "default"
54
+ });
55
+ } else if (t.isImportNamespaceSpecifier(s)) {
56
+ bindings.set(s.local.name, {
57
+ type: "import",
58
+ source,
59
+ importedName: "*"
60
+ });
61
+ }
62
+ }
63
+ } else if (t.isVariableDeclaration(node)) {
64
+ for (const decl of node.declarations) {
65
+ if (t.isIdentifier(decl.id)) {
66
+ bindings.set(decl.id.name, {
67
+ type: "var",
68
+ init: decl.init ?? null
69
+ });
70
+ }
71
+ }
72
+ } else if (t.isExportNamedDeclaration(node)) {
73
+ if (node.declaration) {
74
+ if (t.isVariableDeclaration(node.declaration)) {
75
+ for (const d of node.declaration.declarations) {
76
+ if (t.isIdentifier(d.id)) {
77
+ exports.set(d.id.name, { tag: "Normal", name: d.id.name });
78
+ bindings.set(d.id.name, { type: "var", init: d.init ?? null });
79
+ }
80
+ }
81
+ }
82
+ }
83
+ for (const sp of node.specifiers) {
84
+ if (t.isExportNamespaceSpecifier(sp)) {
85
+ exports.set(sp.exported.name, {
86
+ tag: "Namespace",
87
+ name: sp.exported.name,
88
+ targetId: node.source?.value || ""
89
+ });
90
+ } else if (t.isExportSpecifier(sp)) {
91
+ const local = sp.local.name;
92
+ const exported = t.isIdentifier(sp.exported) ? sp.exported.name : sp.exported.value;
93
+ exports.set(exported, { tag: "Normal", name: local });
94
+ }
95
+ }
96
+ } else if (t.isExportDefaultDeclaration(node)) {
97
+ const d = node.declaration;
98
+ if (t.isIdentifier(d)) {
99
+ exports.set("default", { tag: "Default", name: d.name });
100
+ } else {
101
+ const synth = "__default_export__";
102
+ bindings.set(synth, { type: "var", init: d });
103
+ exports.set("default", { tag: "Default", name: synth });
104
+ }
105
+ }
106
+ }
107
+ const info = { code, id, ast, bindings, exports };
108
+ this.moduleCache.set(id, info);
109
+ return info;
110
+ }
111
+ invalidateModule(id) {
112
+ return this.moduleCache.delete(id);
113
+ }
114
+ async compile({ code, id }) {
115
+ if (!this.initialized) {
116
+ await this.init(id);
117
+ }
118
+ const { bindings, ast } = this.ingestModule({ code, id });
119
+ const candidates = this.collectHandlerCandidates(bindings);
120
+ if (candidates.length === 0) {
121
+ return null;
122
+ }
123
+ const toRewrite = [];
124
+ for (const handler of candidates) {
125
+ const kind = await this.resolveExprKind(handler, id);
126
+ if (kind === "ServerFn") {
127
+ toRewrite.push(handler);
128
+ }
129
+ }
130
+ if (toRewrite.length === 0) {
131
+ return null;
132
+ }
133
+ const pathsToRewrite = [];
134
+ babel__default.traverse(ast, {
135
+ CallExpression(path) {
136
+ const found = toRewrite.findIndex((h) => path.node === h);
137
+ if (found !== -1) {
138
+ pathsToRewrite.push(path);
139
+ toRewrite.splice(found, 1);
140
+ }
141
+ }
142
+ });
143
+ if (toRewrite.length > 0) {
144
+ throw new Error(
145
+ `Internal error: could not find all paths to rewrite. please file an issue`
146
+ );
147
+ }
148
+ const refIdents = findReferencedIdentifiers(ast);
149
+ pathsToRewrite.map(
150
+ (p) => handleCreateServerFn(p, { env: this.options.env, code })
151
+ );
152
+ deadCodeElimination(ast, refIdents);
153
+ return generateFromAst(ast, {
154
+ sourceMaps: true,
155
+ sourceFileName: id,
156
+ filename: id
157
+ });
158
+ }
159
+ // collects all `.handler(...)` CallExpressions at top-level
160
+ collectHandlerCandidates(bindings) {
161
+ const candidates = [];
162
+ for (const binding of bindings.values()) {
163
+ if (binding.type === "var") {
164
+ const handler = isHandlerCall(binding.init);
165
+ if (handler) {
166
+ candidates.push(handler);
167
+ }
168
+ }
169
+ }
170
+ return candidates;
171
+ }
172
+ async resolveIdentifierKind(ident, id, visited = /* @__PURE__ */ new Set()) {
173
+ const info = await this.getModuleInfo(id);
174
+ const binding = info.bindings.get(ident);
175
+ if (!binding) {
176
+ return "None";
177
+ }
178
+ if (binding.resolvedKind) {
179
+ return binding.resolvedKind;
180
+ }
181
+ const vKey = `${id}:${ident}`;
182
+ if (visited.has(vKey)) {
183
+ return "None";
184
+ }
185
+ visited.add(vKey);
186
+ const resolvedKind = await this.resolveBindingKind(binding, id, visited);
187
+ binding.resolvedKind = resolvedKind;
188
+ return resolvedKind;
189
+ }
190
+ async resolveBindingKind(binding, fileId, visited = /* @__PURE__ */ new Set()) {
191
+ if (binding.resolvedKind) {
192
+ return binding.resolvedKind;
193
+ }
194
+ if (binding.type === "import") {
195
+ const target = await this.options.resolveId(binding.source, fileId);
196
+ if (!target) {
197
+ return "None";
198
+ }
199
+ if (binding.importedName === "*") {
200
+ throw new Error(
201
+ `should never get here, namespace imports are handled in resolveCalleeKind`
202
+ );
203
+ }
204
+ const importedModule = await this.getModuleInfo(target);
205
+ const moduleExport = importedModule.exports.get(binding.importedName);
206
+ if (!moduleExport) {
207
+ return "None";
208
+ }
209
+ const importedBinding = importedModule.bindings.get(moduleExport.name);
210
+ if (!importedBinding) {
211
+ return "None";
212
+ }
213
+ if (importedBinding.resolvedKind) {
214
+ return importedBinding.resolvedKind;
215
+ }
216
+ const resolvedKind2 = await this.resolveBindingKind(
217
+ importedBinding,
218
+ importedModule.id,
219
+ visited
220
+ );
221
+ importedBinding.resolvedKind = resolvedKind2;
222
+ return resolvedKind2;
223
+ }
224
+ const resolvedKind = await this.resolveExprKind(
225
+ binding.init,
226
+ fileId,
227
+ visited
228
+ );
229
+ binding.resolvedKind = resolvedKind;
230
+ return resolvedKind;
231
+ }
232
+ async resolveExprKind(expr, fileId, visited = /* @__PURE__ */ new Set()) {
233
+ if (!expr) {
234
+ return "None";
235
+ }
236
+ let result = "None";
237
+ if (t.isCallExpression(expr)) {
238
+ if (!t.isExpression(expr.callee)) {
239
+ return "None";
240
+ }
241
+ const calleeKind = await this.resolveCalleeKind(
242
+ expr.callee,
243
+ fileId,
244
+ visited
245
+ );
246
+ if (calleeKind === "Root" || calleeKind === "Builder") {
247
+ return "Builder";
248
+ }
249
+ if (calleeKind === "ServerFn") {
250
+ return "ServerFn";
251
+ }
252
+ } else if (t.isMemberExpression(expr) && t.isIdentifier(expr.property)) {
253
+ result = await this.resolveCalleeKind(expr.object, fileId, visited);
254
+ }
255
+ if (result === "None" && t.isIdentifier(expr)) {
256
+ result = await this.resolveIdentifierKind(expr.name, fileId, visited);
257
+ }
258
+ if (result === "None" && t.isTSAsExpression(expr)) {
259
+ result = await this.resolveExprKind(expr.expression, fileId, visited);
260
+ }
261
+ if (result === "None" && t.isTSNonNullExpression(expr)) {
262
+ result = await this.resolveExprKind(expr.expression, fileId, visited);
263
+ }
264
+ if (result === "None" && t.isParenthesizedExpression(expr)) {
265
+ result = await this.resolveExprKind(expr.expression, fileId, visited);
266
+ }
267
+ return result;
268
+ }
269
+ async resolveCalleeKind(callee, fileId, visited = /* @__PURE__ */ new Set()) {
270
+ if (t.isIdentifier(callee)) {
271
+ return this.resolveIdentifierKind(callee.name, fileId, visited);
272
+ }
273
+ if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
274
+ const prop = callee.property.name;
275
+ if (prop === "handler") {
276
+ const base = await this.resolveExprKind(callee.object, fileId, visited);
277
+ if (base === "Root" || base === "Builder") {
278
+ return "ServerFn";
279
+ }
280
+ return "None";
281
+ }
282
+ if (t.isIdentifier(callee.object)) {
283
+ const info = await this.getModuleInfo(fileId);
284
+ const binding = info.bindings.get(callee.object.name);
285
+ if (binding && binding.type === "import" && binding.importedName === "*") {
286
+ const targetModuleId = await this.options.resolveId(
287
+ binding.source,
288
+ fileId
289
+ );
290
+ if (targetModuleId) {
291
+ const targetModule = await this.getModuleInfo(targetModuleId);
292
+ const exportEntry = targetModule.exports.get(callee.property.name);
293
+ if (exportEntry) {
294
+ const exportedBinding = targetModule.bindings.get(
295
+ exportEntry.name
296
+ );
297
+ if (exportedBinding) {
298
+ return await this.resolveBindingKind(
299
+ exportedBinding,
300
+ targetModule.id,
301
+ visited
302
+ );
303
+ }
304
+ }
305
+ }
306
+ }
307
+ }
308
+ return this.resolveExprKind(callee.object, fileId, visited);
309
+ }
310
+ return this.resolveExprKind(callee, fileId, visited);
311
+ }
312
+ async getModuleInfo(id) {
313
+ let cached = this.moduleCache.get(id);
314
+ if (cached) {
315
+ return cached;
316
+ }
317
+ await this.options.loadModule(id);
318
+ cached = this.moduleCache.get(id);
319
+ if (!cached) {
320
+ throw new Error(`could not load module info for ${id}`);
321
+ }
322
+ return cached;
323
+ }
324
+ }
325
+ function isHandlerCall(node) {
326
+ if (!t.isCallExpression(node)) return void 0;
327
+ const callee = node.callee;
328
+ if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property, { name: "handler" })) {
329
+ return void 0;
330
+ }
331
+ return node;
332
+ }
333
+ export {
334
+ ServerFnCompiler
335
+ };
336
+ //# 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'\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' | 'ServerFn'\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 resolvedLibId!: string\n private initialized = false\n constructor(\n private options: {\n env: 'client' | 'server'\n libName: string\n rootExport: string\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 const libId = await this.options.resolveId(this.options.libName, id)\n if (!libId) {\n throw new Error(`could not resolve \"${this.options.libName}\"`)\n }\n // insert root binding\n const rootModule = {\n ast: null as any,\n bindings: new Map(),\n exports: new Map(),\n code: '',\n id: libId,\n }\n rootModule.exports.set(this.options.rootExport, {\n tag: 'Normal',\n name: this.options.rootExport,\n })\n rootModule.bindings.set(this.options.rootExport, {\n type: 'var',\n init: t.identifier(this.options.rootExport),\n resolvedKind: 'Root',\n })\n this.moduleCache.set(libId, rootModule)\n this.initialized = true\n this.resolvedLibId = libId\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.collectHandlerCandidates(bindings)\n if (candidates.length === 0) {\n // this hook will only be invoked if there is `.handler(` 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<t.CallExpression> = []\n for (const handler of candidates) {\n const kind = await this.resolveExprKind(handler, id)\n if (kind === 'ServerFn') {\n toRewrite.push(handler)\n }\n }\n if (toRewrite.length === 0) {\n return null\n }\n const pathsToRewrite: Array<babel.NodePath<t.CallExpression>> = []\n babel.traverse(ast, {\n CallExpression(path) {\n const found = toRewrite.findIndex((h) => path.node === h)\n if (found !== -1) {\n pathsToRewrite.push(path)\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 handleCreateServerFn(p, { env: this.options.env, code }),\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 `.handler(...)` CallExpressions at top-level\n private collectHandlerCandidates(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 = isHandlerCall(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 if (binding.importedName === '*') {\n throw new Error(\n `should never get here, namespace imports are handled in resolveCalleeKind`,\n )\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 === 'Root' || calleeKind === 'Builder') {\n return 'Builder'\n }\n if (calleeKind === 'ServerFn') {\n return 'ServerFn'\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 }\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 }\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 isHandlerCall(\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 (\n !t.isMemberExpression(callee) ||\n !t.isIdentifier(callee.property, { name: 'handler' })\n ) {\n return undefined\n }\n\n return node\n}\n"],"names":["babel","resolvedKind"],"mappings":";;;;;AAsCO,MAAM,iBAAiB;AAAA,EAI5B,YACU,SAOR;AAPQ,SAAA,UAAA;AAAA,EAOP;AAAA,EAXK,kCAAkB,IAAA;AAAA,EAClB;AAAA,EACA,cAAc;AAAA,EAWtB,MAAc,KAAK,IAAY;AAC7B,UAAM,QAAQ,MAAM,KAAK,QAAQ,UAAU,KAAK,QAAQ,SAAS,EAAE;AACnE,QAAI,CAAC,OAAO;AACV,YAAM,IAAI,MAAM,sBAAsB,KAAK,QAAQ,OAAO,GAAG;AAAA,IAC/D;AAEA,UAAM,aAAa;AAAA,MACjB,KAAK;AAAA,MACL,8BAAc,IAAA;AAAA,MACd,6BAAa,IAAA;AAAA,MACb,MAAM;AAAA,MACN,IAAI;AAAA,IAAA;AAEN,eAAW,QAAQ,IAAI,KAAK,QAAQ,YAAY;AAAA,MAC9C,KAAK;AAAA,MACL,MAAM,KAAK,QAAQ;AAAA,IAAA,CACpB;AACD,eAAW,SAAS,IAAI,KAAK,QAAQ,YAAY;AAAA,MAC/C,MAAM;AAAA,MACN,MAAM,EAAE,WAAW,KAAK,QAAQ,UAAU;AAAA,MAC1C,cAAc;AAAA,IAAA,CACf;AACD,SAAK,YAAY,IAAI,OAAO,UAAU;AACtC,SAAK,cAAc;AACnB,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEO,aAAa,EAAE,MAAM,MAAoC;AAC9D,UAAM,MAAM,SAAS,EAAE,MAAM;AAE7B,UAAM,+BAAe,IAAA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AAI,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,cACP,MAC8B;AAC9B,MAAI,CAAC,EAAE,iBAAiB,IAAI,EAAG,QAAO;AAEtC,QAAM,SAAS,KAAK;AACpB,MACE,CAAC,EAAE,mBAAmB,MAAM,KAC5B,CAAC,EAAE,aAAa,OAAO,UAAU,EAAE,MAAM,UAAA,CAAW,GACpD;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;"}
@@ -0,0 +1,6 @@
1
+ import * as t from '@babel/types';
2
+ import type * as babel from '@babel/core';
3
+ export declare function handleCreateServerFn(path: babel.NodePath<t.CallExpression>, opts: {
4
+ env: 'client' | 'server';
5
+ code: string;
6
+ }): void;
@@ -1,16 +1,12 @@
1
1
  import * as t from "@babel/types";
2
- import { getRootCallExpression, codeFrameError } from "./utils.js";
3
- function handleCreateServerFnCallExpression(path, opts) {
4
- const calledOptions = path.node.arguments[0] ? path.get("arguments.0") : null;
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", "validator", "handler"];
8
5
  const callExpressionPaths = {
9
6
  middleware: null,
10
7
  validator: 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!");
@@ -34,10 +30,12 @@ function handleCreateServerFnCallExpression(path, opts) {
34
30
  "createServerFn().validator() must be called with a validator!"
35
31
  );
36
32
  }
37
- if (opts.env === "client" && !shouldValidateClient && t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
38
- callExpressionPaths.validator.replaceWith(
39
- callExpressionPaths.validator.node.callee.object
40
- );
33
+ if (opts.env === "client") {
34
+ if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
35
+ callExpressionPaths.validator.replaceWith(
36
+ callExpressionPaths.validator.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
- handleCreateServerFnCallExpression
83
+ handleCreateServerFn
86
84
  };
87
- //# sourceMappingURL=serverFn.js.map
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', 'validator', '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 validator: 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.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().validator() 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 (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.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(handlerFn)\n }\n}\n"],"names":[],"mappings":";;AAOO,SAAS,qBACd,MACA,MAIA;AAKA,QAAM,eAAe,CAAC,cAAc,aAAa,SAAS;AAE1D,QAAM,sBAGF;AAAA,IACF,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,SAAS;AAAA,EAAA;AAGX,QAAM,qBAAqB,sBAAsB,IAAI;AASrD,MAAI,CAAC,mBAAmB,WAAW,wBAAwB;AACzD,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAGA,QAAM,qBAAqB,mBAAmB,WAAW;AACzD,QAAM,uBAAwB,mBAAmB,GAAoB;AAErE,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SAAS;AAEhD,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,WAAW;AACjC,UAAM,uBAAuB,oBAAoB,UAAU,KAAK,UAAU,CAAC;AAE3E,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QA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export declare function createServerFnPlugin(framework: CompileStartFrameworkOptions): PluginOption;
@@ -0,0 +1,113 @@
1
+ import { VITE_ENVIRONMENT_NAMES } from "../constants.js";
2
+ import { ServerFnCompiler } from "./compiler.js";
3
+ function cleanId(id) {
4
+ return id.split("?")[0];
5
+ }
6
+ function createServerFnPlugin(framework) {
7
+ const libName = `@tanstack/${framework}-start`;
8
+ const rootExport = "createServerFn";
9
+ const SERVER_FN_LOOKUP = "server-fn-module-lookup";
10
+ const compilers = {};
11
+ return [
12
+ {
13
+ name: "tanstack-start-core:capture-server-fn-module-lookup",
14
+ // we only need this plugin in dev mode
15
+ apply: "serve",
16
+ applyToEnvironment(env) {
17
+ return [
18
+ VITE_ENVIRONMENT_NAMES.client,
19
+ VITE_ENVIRONMENT_NAMES.server
20
+ ].includes(env.name);
21
+ },
22
+ transform: {
23
+ filter: {
24
+ id: new RegExp(`${SERVER_FN_LOOKUP}$`)
25
+ },
26
+ handler(code, id) {
27
+ const compiler = compilers[this.environment.name];
28
+ compiler?.ingestModule({ code, id: cleanId(id) });
29
+ }
30
+ }
31
+ },
32
+ {
33
+ name: "tanstack-start-core::server-fn",
34
+ enforce: "pre",
35
+ applyToEnvironment(env) {
36
+ return [
37
+ VITE_ENVIRONMENT_NAMES.client,
38
+ VITE_ENVIRONMENT_NAMES.server
39
+ ].includes(env.name);
40
+ },
41
+ transform: {
42
+ filter: {
43
+ id: {
44
+ exclude: new RegExp(`${SERVER_FN_LOOKUP}$`)
45
+ },
46
+ code: {
47
+ // only scan files that mention `.handler(`
48
+ include: [/\.handler\(/]
49
+ }
50
+ },
51
+ async handler(code, id) {
52
+ let compiler = compilers[this.environment.name];
53
+ if (!compiler) {
54
+ const env = this.environment.name === VITE_ENVIRONMENT_NAMES.client ? "client" : this.environment.name === VITE_ENVIRONMENT_NAMES.server ? "server" : (() => {
55
+ throw new Error(
56
+ `Environment ${this.environment.name} not configured`
57
+ );
58
+ })();
59
+ compiler = new ServerFnCompiler({
60
+ env,
61
+ libName,
62
+ rootExport,
63
+ loadModule: async (id2) => {
64
+ if (this.environment.mode === "build") {
65
+ const loaded = await this.load({ id: id2 });
66
+ if (!loaded.code) {
67
+ throw new Error(`could not load module ${id2}`);
68
+ }
69
+ compiler.ingestModule({ code: loaded.code, id: id2 });
70
+ } else if (this.environment.mode === "dev") {
71
+ await this.environment.fetchModule(
72
+ id2 + "?" + SERVER_FN_LOOKUP
73
+ );
74
+ } else {
75
+ throw new Error(
76
+ `could not load module ${id2}: unknown environment mode ${this.environment.mode}`
77
+ );
78
+ }
79
+ },
80
+ resolveId: async (source, importer) => {
81
+ const r = await this.resolve(source, importer);
82
+ return r ? cleanId(r.id) : null;
83
+ }
84
+ });
85
+ compilers[this.environment.name] = compiler;
86
+ }
87
+ id = cleanId(id);
88
+ const result = await compiler.compile({ id, code });
89
+ return result;
90
+ }
91
+ },
92
+ hotUpdate(ctx) {
93
+ const compiler = compilers[this.environment.name];
94
+ ctx.modules.forEach((m) => {
95
+ if (m.id) {
96
+ const deleted = compiler?.invalidateModule(m.id);
97
+ if (deleted) {
98
+ m.importers.forEach((importer) => {
99
+ if (importer.id) {
100
+ compiler?.invalidateModule(importer.id);
101
+ }
102
+ });
103
+ }
104
+ }
105
+ });
106
+ }
107
+ }
108
+ ];
109
+ }
110
+ export {
111
+ createServerFnPlugin
112
+ };
113
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["../../../src/create-server-fn-plugin/plugin.ts"],"sourcesContent":["import { VITE_ENVIRONMENT_NAMES } from '../constants'\nimport { ServerFnCompiler } from './compiler'\nimport type { ViteEnvironmentNames } from '../constants'\nimport type { PluginOption } from 'vite'\nimport type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'\n\nfunction cleanId(id: string): string {\n return id.split('?')[0]!\n}\n\nexport function createServerFnPlugin(\n framework: CompileStartFrameworkOptions,\n): PluginOption {\n const libName = `@tanstack/${framework}-start`\n const rootExport = 'createServerFn'\n\n const SERVER_FN_LOOKUP = 'server-fn-module-lookup'\n\n const compilers: Partial<Record<ViteEnvironmentNames, ServerFnCompiler>> = {}\n return [\n {\n name: 'tanstack-start-core:capture-server-fn-module-lookup',\n // we only need this plugin in dev mode\n apply: 'serve',\n applyToEnvironment(env) {\n return [\n VITE_ENVIRONMENT_NAMES.client,\n VITE_ENVIRONMENT_NAMES.server,\n ].includes(env.name as ViteEnvironmentNames)\n },\n transform: {\n filter: {\n id: new RegExp(`${SERVER_FN_LOOKUP}$`),\n },\n handler(code, id) {\n const compiler =\n compilers[this.environment.name as ViteEnvironmentNames]\n compiler?.ingestModule({ code, id: cleanId(id) })\n },\n },\n },\n {\n name: 'tanstack-start-core::server-fn',\n enforce: 'pre',\n\n applyToEnvironment(env) {\n return [\n VITE_ENVIRONMENT_NAMES.client,\n VITE_ENVIRONMENT_NAMES.server,\n ].includes(env.name as ViteEnvironmentNames)\n },\n transform: {\n filter: {\n id: {\n exclude: new RegExp(`${SERVER_FN_LOOKUP}$`),\n },\n code: {\n // only scan files that mention `.handler(`\n include: [/\\.handler\\(/],\n },\n },\n async handler(code, id) {\n let compiler =\n compilers[this.environment.name as ViteEnvironmentNames]\n if (!compiler) {\n const env =\n this.environment.name === VITE_ENVIRONMENT_NAMES.client\n ? 'client'\n : this.environment.name === VITE_ENVIRONMENT_NAMES.server\n ? 'server'\n : (() => {\n throw new Error(\n `Environment ${this.environment.name} not configured`,\n )\n })()\n\n compiler = new ServerFnCompiler({\n env,\n libName,\n rootExport,\n loadModule: async (id: string) => {\n if (this.environment.mode === 'build') {\n const loaded = await this.load({ id })\n if (!loaded.code) {\n throw new Error(`could not load module ${id}`)\n }\n compiler!.ingestModule({ code: loaded.code, id })\n } else if (this.environment.mode === 'dev') {\n /**\n * in dev, vite does not return code from `ctx.load()`\n * so instead, we need to take a different approach\n * we must force vite to load the module and run it through the vite plugin pipeline\n * we can do this by using the `fetchModule` method\n * the `captureServerFnModuleLookupPlugin` captures the module code via its transform hook and invokes analyzeModuleAST\n */\n await this.environment.fetchModule(\n id + '?' + SERVER_FN_LOOKUP,\n )\n } else {\n throw new Error(\n `could not load module ${id}: unknown environment mode ${this.environment.mode}`,\n )\n }\n },\n resolveId: async (source: string, importer?: string) => {\n const r = await this.resolve(source, importer)\n return r ? cleanId(r.id) : null\n },\n })\n compilers[this.environment.name as ViteEnvironmentNames] = compiler\n }\n\n id = cleanId(id)\n const result = await compiler.compile({ id, code })\n return result\n },\n },\n\n hotUpdate(ctx) {\n const compiler =\n compilers[this.environment.name as ViteEnvironmentNames]\n\n ctx.modules.forEach((m) => {\n if (m.id) {\n const deleted = compiler?.invalidateModule(m.id)\n if (deleted) {\n m.importers.forEach((importer) => {\n if (importer.id) {\n compiler?.invalidateModule(importer.id)\n }\n })\n }\n }\n })\n },\n },\n ]\n}\n"],"names":["id"],"mappings":";;AAMA,SAAS,QAAQ,IAAoB;AACnC,SAAO,GAAG,MAAM,GAAG,EAAE,CAAC;AACxB;AAEO,SAAS,qBACd,WACc;AACd,QAAM,UAAU,aAAa,SAAS;AACtC,QAAM,aAAa;AAEnB,QAAM,mBAAmB;AAEzB,QAAM,YAAqE,CAAA;AAC3E,SAAO;AAAA,IACL;AAAA,MACE,MAAM;AAAA;AAAA,MAEN,OAAO;AAAA,MACP,mBAAmB,KAAK;AACtB,eAAO;AAAA,UACL,uBAAuB;AAAA,UACvB,uBAAuB;AAAA,QAAA,EACvB,SAAS,IAAI,IAA4B;AAAA,MAC7C;AAAA,MACA,WAAW;AAAA,QACT,QAAQ;AAAA,UACN,IAAI,IAAI,OAAO,GAAG,gBAAgB,GAAG;AAAA,QAAA;AAAA,QAEvC,QAAQ,MAAM,IAAI;AAChB,gBAAM,WACJ,UAAU,KAAK,YAAY,IAA4B;AACzD,oBAAU,aAAa,EAAE,MAAM,IAAI,QAAQ,EAAE,GAAG;AAAA,QAClD;AAAA,MAAA;AAAA,IACF;AAAA,IAEF;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,M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