@tanstack/start-plugin-core 1.132.7 → 1.132.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.
@@ -4,8 +4,8 @@ export declare const VITE_ENVIRONMENT_NAMES: {
4
4
  };
5
5
  export type ViteEnvironmentNames = (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof VITE_ENVIRONMENT_NAMES];
6
6
  export declare const ENTRY_POINTS: {
7
- readonly client: "virtual:tanstack-start-client-entry";
8
- readonly server: "virtual:tanstack-start-server-request-entry";
7
+ readonly client: "__tanstack-start-client-entry__";
8
+ readonly server: "__tanstack-start-server-entry__";
9
9
  readonly start: "#tanstack-start-entry";
10
10
  readonly router: "#tanstack-router-entry";
11
11
  };
@@ -5,8 +5,8 @@ const VITE_ENVIRONMENT_NAMES = {
5
5
  client: "client"
6
6
  };
7
7
  const ENTRY_POINTS = {
8
- client: "virtual:tanstack-start-client-entry",
9
- server: "virtual:tanstack-start-server-request-entry",
8
+ client: "__tanstack-start-client-entry__",
9
+ server: "__tanstack-start-server-entry__",
10
10
  // the start entry point must always be provided by the user
11
11
  start: "#tanstack-start-entry",
12
12
  router: "#tanstack-router-entry"
@@ -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 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;"}
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: '__tanstack-start-client-entry__',\n server: '__tanstack-start-server-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;"}
@@ -22,7 +22,7 @@ type ExportEntry = {
22
22
  targetId: string;
23
23
  };
24
24
  type Kind = 'None' | `Root` | `Builder` | LookupKind;
25
- type LookupKind = 'ServerFn' | 'Middleware';
25
+ export type LookupKind = 'ServerFn' | 'Middleware';
26
26
  export type LookupConfig = {
27
27
  libName: string;
28
28
  rootExport: string;
@@ -38,9 +38,11 @@ export declare class ServerFnCompiler {
38
38
  private options;
39
39
  private moduleCache;
40
40
  private initialized;
41
+ private validLookupKinds;
41
42
  constructor(options: {
42
43
  env: 'client' | 'server';
43
44
  lookupConfigurations: Array<LookupConfig>;
45
+ lookupKinds: Set<LookupKind>;
44
46
  loadModule: (id: string) => Promise<void>;
45
47
  resolveId: (id: string, importer?: string) => Promise<string | null>;
46
48
  });
@@ -4,14 +4,20 @@ import babel__default from "@babel/core";
4
4
  import { findReferencedIdentifiers, deadCodeElimination } from "babel-dead-code-elimination";
5
5
  import { handleCreateServerFn } from "./handleCreateServerFn.js";
6
6
  import { handleCreateMiddleware } from "./handleCreateMiddleware.js";
7
- const validLookupKinds = ["ServerFn", "Middleware"];
8
- const candidateCallIdentifier = ["handler", "server", "client"];
7
+ const LookupSetup = {
8
+ ServerFn: { candidateCallIdentifier: /* @__PURE__ */ new Set(["handler"]) },
9
+ Middleware: {
10
+ candidateCallIdentifier: /* @__PURE__ */ new Set(["server", "client", "createMiddlewares"])
11
+ }
12
+ };
9
13
  class ServerFnCompiler {
10
14
  constructor(options) {
11
15
  this.options = options;
16
+ this.validLookupKinds = options.lookupKinds;
12
17
  }
13
18
  moduleCache = /* @__PURE__ */ new Map();
14
19
  initialized = false;
20
+ validLookupKinds;
15
21
  async init(id) {
16
22
  await Promise.all(
17
23
  this.options.lookupConfigurations.map(async (config) => {
@@ -137,7 +143,7 @@ class ServerFnCompiler {
137
143
  const toRewrite = [];
138
144
  for (const handler of candidates) {
139
145
  const kind = await this.resolveExprKind(handler, id);
140
- if (validLookupKinds.includes(kind)) {
146
+ if (this.validLookupKinds.has(kind)) {
141
147
  toRewrite.push({ callExpression: handler, kind });
142
148
  }
143
149
  }
@@ -179,7 +185,10 @@ class ServerFnCompiler {
179
185
  const candidates = [];
180
186
  for (const binding of bindings.values()) {
181
187
  if (binding.type === "var") {
182
- const handler = isCandidateCallExpression(binding.init);
188
+ const handler = isCandidateCallExpression(
189
+ binding.init,
190
+ this.validLookupKinds
191
+ );
183
192
  if (handler) {
184
193
  candidates.push(handler);
185
194
  }
@@ -260,7 +269,7 @@ class ServerFnCompiler {
260
269
  if (calleeKind === `Root` || calleeKind === `Builder`) {
261
270
  return `Builder`;
262
271
  }
263
- for (const kind of validLookupKinds) {
272
+ for (const kind of this.validLookupKinds) {
264
273
  if (calleeKind === kind) {
265
274
  return kind;
266
275
  }
@@ -289,13 +298,13 @@ class ServerFnCompiler {
289
298
  }
290
299
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
291
300
  const prop = callee.property.name;
292
- if (prop === "handler") {
301
+ if (this.validLookupKinds.has("ServerFn") && LookupSetup["ServerFn"].candidateCallIdentifier.has(prop)) {
293
302
  const base = await this.resolveExprKind(callee.object, fileId, visited);
294
303
  if (base === "Root" || base === "Builder") {
295
304
  return "ServerFn";
296
305
  }
297
306
  return "None";
298
- } else if (prop === "client" || prop === "server" || prop === "createMiddleware") {
307
+ } else if (this.validLookupKinds.has("Middleware") && LookupSetup["Middleware"].candidateCallIdentifier.has(prop)) {
299
308
  const base = await this.resolveExprKind(callee.object, fileId, visited);
300
309
  if (base === "Root" || base === "Builder" || base === "Middleware") {
301
310
  return "Middleware";
@@ -347,16 +356,18 @@ class ServerFnCompiler {
347
356
  return cached;
348
357
  }
349
358
  }
350
- function isCandidateCallExpression(node) {
359
+ function isCandidateCallExpression(node, lookupKinds) {
351
360
  if (!t.isCallExpression(node)) return void 0;
352
361
  const callee = node.callee;
353
362
  if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) {
354
363
  return void 0;
355
364
  }
356
- if (!candidateCallIdentifier.includes(callee.property.name)) {
357
- return void 0;
365
+ for (const kind of lookupKinds) {
366
+ if (LookupSetup[kind].candidateCallIdentifier.has(callee.property.name)) {
367
+ return node;
368
+ }
358
369
  }
359
- return node;
370
+ return void 0;
360
371
  }
361
372
  export {
362
373
  ServerFnCompiler
@@ -1 +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","resolvedKind"],"mappings":";;;;;;AAiCA,MAAM,mBAAsC,CAAC,YAAY,YAAY;AACrE,MAAM,0BAA0B,CAAC,WAAW,UAAU,QAAQ;AAavD,MAAM,iBAAiB;AAAA,EAG5B,YACU,SAMR;AANQ,SAAA,UAAA;AAAA,EAMP;AAAA,EATK,kCAAkB,IAAA;AAAA,EAClB,cAAc;AAAA,EAUtB,MAAc,KAAK,IAAY;AAC7B,UAAM,QAAQ;AAAA,MACZ,KAAK,QAAQ,qBAAqB,IAAI,OAAO,WAAW;AACtD,cAAM,QAAQ,MAAM,KAAK,QAAQ,UAAU,OAAO,SAAS,EAAE;AAC7D,YAAI,CAAC,OAAO;AACV,gBAAM,IAAI,MAAM,sBAAsB,OAAO,OAAO,GAAG;AAAA,QACzD;AACA,YAAI,aAAa,KAAK,YAAY,IAAI,KAAK;AAC3C,YAAI,CAAC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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
+ {"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\nexport type LookupKind = 'ServerFn' | 'Middleware'\n\nconst LookupSetup: Record<\n LookupKind,\n { candidateCallIdentifier: Set<string> }\n> = {\n ServerFn: { candidateCallIdentifier: new Set(['handler']) },\n Middleware: {\n candidateCallIdentifier: new Set(['server', 'client', 'createMiddlewares']),\n },\n}\n\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 private validLookupKinds: Set<LookupKind>\n constructor(\n private options: {\n env: 'client' | 'server'\n lookupConfigurations: Array<LookupConfig>\n lookupKinds: Set<LookupKind>\n loadModule: (id: string) => Promise<void>\n resolveId: (id: string, importer?: string) => Promise<string | null>\n },\n ) {\n this.validLookupKinds = options.lookupKinds\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 (this.validLookupKinds.has(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(\n binding.init,\n this.validLookupKinds,\n )\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 this.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 (\n this.validLookupKinds.has('ServerFn') &&\n LookupSetup['ServerFn'].candidateCallIdentifier.has(prop)\n ) {\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 this.validLookupKinds.has('Middleware') &&\n LookupSetup['Middleware'].candidateCallIdentifier.has(prop)\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 lookupKinds: Set<LookupKind>,\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 for (const kind of lookupKinds) {\n if (LookupSetup[kind].candidateCallIdentifier.has(callee.property.name)) {\n return node\n }\n }\n\n return undefined\n}\n"],"names":["babel","resolvedKind"],"mappings":";;;;;;AAiCA,MAAM,cAGF;AAAA,EACF,UAAU,EAAE,yBAAyB,oBAAI,IAAI,CAAC,SAAS,CAAC,EAAA;AAAA,EACxD,YAAY;AAAA,IACV,yBAAyB,oBAAI,IAAI,CAAC,UAAU,UAAU,mBAAmB,CAAC;AAAA,EAAA;AAE9E;AAcO,MAAM,iBAAiB;AAAA,EAI5B,YACU,SAOR;AAPQ,SAAA,UAAA;AAQR,SAAK,mBAAmB,QAAQ;AAAA,EAClC;AAAA,EAbQ,kCAAkB,IAAA;AAAA,EAClB,cAAc;AAAA,EACd;AAAA,EAaR,MAAc,KAAK,IAAY;AAC7B,UAAM,QAAQ;AAAA,MACZ,KAAK,QAAQ,qBAAqB,IAAI,OAAO,WAAW;AACtD,cAAM,QAAQ,MAAM,KAAK,QAAQ,UAAU,OAAO,SAAS,EAAE;AAC7D,YAAI,CAAC,OAAO;AACV,gBAAM,IAAI,MAAM,sBAAsB,OAAO,OAAO,GAAG;AAAA,QACzD;AACA,YAAI,aAAa,KAAK,YAAY,IAAI,KAAK;AAC3C,YAAI,CAAC,YAAY;AAEf,uBAAa;AAAA,YACX,KAAK;AAAA,YACL,8BAAc,IAAA;AAAA,YACd,6BAAa,IAAA;AAAA,YACb,MAAM;AAAA,YACN,IAAI;AAAA,UAAA;AAEN,eAAK,YAAY,IAAI,OAAO,UAAU;AAAA,QACxC;AAEA,mBAAW,QAAQ,IAAI,OAAO,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;AAAA,IACxD;AACA,WAAO;AAAA,EACT;AACF;AAEA,SAAS,0BACP,MACA,aAC8B;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,aAAW,QAAQ,aAAa;AAC9B,QAAI,YAAY,IAAI,EAAE,wBAAwB,IAAI,OAAO,SAAS,IAAI,GAAG;AACvE,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;"}
@@ -3,6 +3,31 @@ import { ServerFnCompiler } from "./compiler.js";
3
3
  function cleanId(id) {
4
4
  return id.split("?")[0];
5
5
  }
6
+ const LookupKindsPerEnv = {
7
+ client: /* @__PURE__ */ new Set(["Middleware", "ServerFn"]),
8
+ server: /* @__PURE__ */ new Set(["ServerFn"])
9
+ };
10
+ const getLookupConfigurationsForEnv = (env, framework) => {
11
+ const createServerFnConfig = {
12
+ libName: `@tanstack/${framework}-start`,
13
+ rootExport: "createServerFn"
14
+ };
15
+ if (env === "client") {
16
+ return [
17
+ {
18
+ libName: `@tanstack/${framework}-start`,
19
+ rootExport: "createMiddleware"
20
+ },
21
+ {
22
+ libName: `@tanstack/${framework}-start`,
23
+ rootExport: "createStart"
24
+ },
25
+ createServerFnConfig
26
+ ];
27
+ } else {
28
+ return [createServerFnConfig];
29
+ }
30
+ };
6
31
  function createServerFnPlugin(framework) {
7
32
  const SERVER_FN_LOOKUP = "server-fn-module-lookup";
8
33
  const compilers = {};
@@ -42,8 +67,9 @@ function createServerFnPlugin(framework) {
42
67
  exclude: new RegExp(`${SERVER_FN_LOOKUP}$`)
43
68
  },
44
69
  code: {
45
- // only scan files that mention `.handler(` | `.server(` | `.client(`
46
- include: [/\.handler\(/, /\.server\(/, /\.client\(/]
70
+ // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env
71
+ // only scan files that mention `.handler(` | `.createMiddleware()`
72
+ include: [/\.handler\(/, /.createMiddleware\(\)/]
47
73
  }
48
74
  },
49
75
  async handler(code, id) {
@@ -56,20 +82,11 @@ function createServerFnPlugin(framework) {
56
82
  })();
57
83
  compiler = new ServerFnCompiler({
58
84
  env,
59
- lookupConfigurations: [
60
- {
61
- libName: `@tanstack/${framework}-start`,
62
- rootExport: "createMiddleware"
63
- },
64
- {
65
- libName: `@tanstack/${framework}-start`,
66
- rootExport: "createServerFn"
67
- },
68
- {
69
- libName: `@tanstack/${framework}-start`,
70
- rootExport: "createStart"
71
- }
72
- ],
85
+ lookupKinds: LookupKindsPerEnv[env],
86
+ lookupConfigurations: getLookupConfigurationsForEnv(
87
+ env,
88
+ framework
89
+ ),
73
90
  loadModule: async (id2) => {
74
91
  if (this.environment.mode === "build") {
75
92
  const loaded = await this.load({ id: id2 });
@@ -1 +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 { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'\nimport type { ViteEnvironmentNames } from '../constants'\nimport type { PluginOption } from 'vite'\n\nfunction cleanId(id: string): string {\n return id.split('?')[0]!\n}\n\nexport function createServerFnPlugin(\n framework: CompileStartFrameworkOptions,\n): PluginOption {\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(` | `.server(` | `.client(`\n include: [/\\.handler\\(/, /\\.server\\(/, /\\.client\\(/],\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 lookupConfigurations: [\n {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createMiddleware',\n },\n\n {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createServerFn',\n },\n {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createStart',\n },\n ],\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 if (r) {\n if (!r.external) {\n return cleanId(r.id)\n }\n }\n return 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,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,MAET,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;AAAA,Y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BAElF;AAAA,cACF;AAAA,cACA,WAAW,OAAO,QAAgB,aAAsB;AACtD,sBAAM,IAAI,MAAM,KAAK,QAAQ,QAAQ,QAAQ;AAC7C,oBAAI,GAAG;AACL,sBAAI,CAAC,EAAE,UAAU;AACf,2BAAO,QAAQ,EAAE,EAAE;AAAA,kBACrB;AAAA,gBACF;AACA,uBAAO;AAAA,cACT;AAAA,YAAA,CACD;AACD,sBAAU,KAAK,YAAY,IAA4B,IAAI;AAAA,UAC7D;AAEA,eAAK,QAAQ,EAAE;AACf,gBAAM,SAAS,MAAM,SAAS,QAAQ,EAAE,IAAI,MAAM;AAClD,iBAAO;AAAA,QACT;AAAA,MAAA;AAAA,MAGF,UAAU,KAAK;AACb,cAAM,WACJ,UAAU,KAAK,YAAY,IAA4B;AAEzD,YAAI,QAAQ,QAAQ,CAAC,MAAM;AACzB,cAAI,EAAE,IAAI;AACR,kBAAM,UAAU,UAAU,iBAAiB,EAAE,EAAE;AAC/C,gBAAI,SAAS;AACX,gBAAE,UAAU,QAAQ,CAAC,aAAa;AAChC,oBAAI,SAAS,IAAI;AACf,4BAAU,iBAAiB,SAAS,EAAE;AAAA,gBACxC;AAAA,cACF,CAAC;AAAA,YACH;AAAA,UACF;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IAAA;AAAA,EACF;AAEJ;"}
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 { LookupConfig, LookupKind } from './compiler'\nimport type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'\nimport type { ViteEnvironmentNames } from '../constants'\nimport type { PluginOption } from 'vite'\n\nfunction cleanId(id: string): string {\n return id.split('?')[0]!\n}\n\nconst LookupKindsPerEnv: Record<'client' | 'server', Set<LookupKind>> = {\n client: new Set(['Middleware', 'ServerFn'] as const),\n server: new Set(['ServerFn'] as const),\n}\n\nconst getLookupConfigurationsForEnv = (\n env: 'client' | 'server',\n framework: CompileStartFrameworkOptions,\n): Array<LookupConfig> => {\n const createServerFnConfig: LookupConfig = {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createServerFn',\n }\n if (env === 'client') {\n return [\n {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createMiddleware',\n },\n {\n libName: `@tanstack/${framework}-start`,\n rootExport: 'createStart',\n },\n\n createServerFnConfig,\n ]\n } else {\n return [createServerFnConfig]\n }\n}\nexport function createServerFnPlugin(\n framework: CompileStartFrameworkOptions,\n): PluginOption {\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 // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env\n // only scan files that mention `.handler(` | `.createMiddleware()`\n include: [/\\.handler\\(/, /.createMiddleware\\(\\)/],\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 lookupKinds: LookupKindsPerEnv[env],\n lookupConfigurations: getLookupConfigurationsForEnv(\n env,\n framework,\n ),\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 if (r) {\n if (!r.external) {\n return cleanId(r.id)\n }\n }\n return 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":";;AAOA,SAAS,QAAQ,IAAoB;AACnC,SAAO,GAAG,MAAM,GAAG,EAAE,CAAC;AACxB;AAEA,MAAM,oBAAkE;AAAA,EACtE,QAAQ,oBAAI,IAAI,CAAC,cAAc,UAAU,CAAU;AAAA,EACnD,QAAQ,oBAAI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AAA,cACT;AAAA,YAAA,CACD;AACD,sBAAU,KAAK,YAAY,IAA4B,IAAI;AAAA,UAC7D;AAEA,eAAK,QAAQ,EAAE;AACf,gBAAM,SAAS,MAAM,SAAS,QAAQ,EAAE,IAAI,MAAM;AAClD,iBAAO;AAAA,QACT;AAAA,MAAA;AAAA,MAGF,UAAU,KAAK;AACb,cAAM,WACJ,UAAU,KAAK,YAAY,IAA4B;AAEzD,YAAI,QAAQ,QAAQ,CAAC,MAAM;AACzB,cAAI,EAAE,IAAI;AACR,kBAAM,UAAU,UAAU,iBAAiB,EAAE,EAAE;AAC/C,gBAAI,SAAS;AACX,gBAAE,UAAU,QAAQ,CAAC,aAAa;AAChC,oBAAI,SAAS,IAAI;AACf,4BAAU,iBAAiB,SAAS,EAAE;AAAA,gBACxC;AAAA,cACF,CAAC;AAAA,YACH;AAAA,UACF;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IAAA;AAAA,EACF;AAEJ;"}
@@ -1,10 +1,10 @@
1
- import path from "node:path";
2
1
  import { trimPathRight } from "@tanstack/router-core";
3
2
  import { VIRTUAL_MODULES } from "@tanstack/start-server-core";
4
3
  import { TanStackServerFnPluginEnv } from "@tanstack/server-functions-plugin";
5
4
  import * as vite from "vite";
6
5
  import { crawlFrameworkPkgs } from "vitefu";
7
6
  import { join } from "pathe";
7
+ import { escapePath } from "tinyglobby";
8
8
  import { startManifestPlugin } from "./start-manifest-plugin/plugin.js";
9
9
  import { startCompilerPlugin } from "./start-compiler-plugin/plugin.js";
10
10
  import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from "./constants.js";
@@ -87,31 +87,21 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
87
87
  resolvedSrcDirectory,
88
88
  required: false
89
89
  });
90
- let clientAlias;
91
- if (clientEntryPath) {
92
- clientAlias = vite.normalizePath(
93
- path.join("/@fs", path.resolve(root, clientEntryPath))
94
- );
95
- } else {
96
- clientAlias = corePluginOpts.defaultEntryPaths.client;
97
- }
98
- let serverAlias;
99
- if (serverEntryPath) {
100
- serverAlias = vite.normalizePath(path.resolve(root, serverEntryPath));
101
- } else {
102
- serverAlias = corePluginOpts.defaultEntryPaths.server;
103
- }
104
- let startAlias;
105
- if (startFilePath) {
106
- startAlias = vite.normalizePath(path.resolve(root, startFilePath));
107
- } else {
108
- startAlias = corePluginOpts.defaultEntryPaths.start;
109
- }
90
+ const clientAlias = vite.normalizePath(
91
+ clientEntryPath ?? corePluginOpts.defaultEntryPaths.client
92
+ );
93
+ const serverAlias = vite.normalizePath(
94
+ serverEntryPath ?? corePluginOpts.defaultEntryPaths.server
95
+ );
96
+ const startAlias = vite.normalizePath(
97
+ startFilePath ?? corePluginOpts.defaultEntryPaths.start
98
+ );
99
+ const routerAlias = vite.normalizePath(routerFilePath);
110
100
  const entryAliasConfiguration = {
111
- [ENTRY_POINTS.start]: startAlias,
112
- [ENTRY_POINTS.router]: routerFilePath,
113
101
  [ENTRY_POINTS.client]: clientAlias,
114
- [ENTRY_POINTS.server]: serverAlias
102
+ [ENTRY_POINTS.server]: serverAlias,
103
+ [ENTRY_POINTS.start]: startAlias,
104
+ [ENTRY_POINTS.router]: routerAlias
115
105
  };
116
106
  const startPackageName = `@tanstack/${corePluginOpts.framework}-start`;
117
107
  const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({
@@ -140,6 +130,15 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
140
130
  }
141
131
  },
142
132
  outDir: getClientOutputDirectory(viteConfig)
133
+ },
134
+ optimizeDeps: {
135
+ // Ensure user code can be crawled for dependencies
136
+ entries: [clientAlias, routerAlias].map(
137
+ (entry) => (
138
+ // Entries are treated as `tinyglobby` patterns so need to be escaped
139
+ escapePath(entry)
140
+ )
141
+ )
143
142
  }
144
143
  },
145
144
  [VITE_ENVIRONMENT_NAMES.server]: {
@@ -154,6 +153,15 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
154
153
  include: [/node_modules/]
155
154
  },
156
155
  copyPublicDir: viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]?.build?.copyPublicDir ?? false
156
+ },
157
+ optimizeDeps: {
158
+ // Ensure user code can be crawled for dependencies
159
+ entries: [serverAlias, startAlias, routerAlias].map(
160
+ (entry) => (
161
+ // Entries are treated as `tinyglobby` patterns so need to be escaped
162
+ escapePath(entry)
163
+ )
164
+ )
157
165
  }
158
166
  }
159
167
  },
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { crawlFrameworkPkgs } from 'vitefu'\nimport { join } from 'pathe'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin/plugin'\nimport { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { parseStartConfig } from './schema'\nimport { resolveEntry } from './resolve-entries'\nimport {\n getClientOutputDirectory,\n getServerOutputDirectory,\n} from './output-directory'\nimport { postServerBuild } from './post-server-build'\nimport { createServerFnPlugin } from './create-server-fn-plugin/plugin'\nimport type { ViteEnvironmentNames } from './constants'\nimport type {\n TanStackStartInputConfig,\n TanStackStartOutputConfig,\n} from './schema'\nimport type { PluginOption } from 'vite'\nimport type { CompileStartFrameworkOptions } from './start-compiler-plugin/compilers'\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n defaultEntryPaths: {\n client: string\n server: string\n start: string\n }\n}\n\nexport interface ResolvedStartConfig {\n root: string\n startFilePath: string | undefined\n routerFilePath: string\n srcDirectory: string\n}\n\nexport type GetConfigFn = () => {\n startConfig: TanStackStartOutputConfig\n resolvedStartConfig: ResolvedStartConfig\n}\nexport function TanStackStartVitePluginCore(\n corePluginOpts: TanStackStartVitePluginCoreOptions,\n startPluginOpts: TanStackStartInputConfig,\n): Array<PluginOption> {\n const resolvedStartConfig: ResolvedStartConfig = {\n root: '',\n startFilePath: undefined,\n routerFilePath: '',\n srcDirectory: '',\n }\n\n let startConfig: TanStackStartOutputConfig | null\n const getConfig: GetConfigFn = () => {\n if (!resolvedStartConfig.root) {\n throw new Error(`Cannot get config before root is resolved`)\n }\n if (!startConfig) {\n startConfig = parseStartConfig(\n startPluginOpts,\n corePluginOpts,\n resolvedStartConfig.root,\n )\n }\n return { startConfig, resolvedStartConfig }\n }\n\n const capturedBundle: Partial<\n Record<ViteEnvironmentNames, vite.Rollup.OutputBundle>\n > = {}\n\n function getBundle(envName: ViteEnvironmentNames): vite.Rollup.OutputBundle {\n const bundle = capturedBundle[envName]\n if (!bundle) {\n throw new Error(`No bundle captured for environment: ${envName}`)\n }\n return bundle\n }\n\n return [\n {\n name: 'tanstack-start-core:config',\n enforce: 'pre',\n async config(viteConfig, { command }) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const root = viteConfig.root || process.cwd()\n resolvedStartConfig.root = root\n\n const { startConfig } = getConfig()\n const resolvedSrcDirectory = join(root, startConfig.srcDirectory)\n resolvedStartConfig.srcDirectory = resolvedSrcDirectory\n\n const startFilePath = resolveEntry({\n type: 'start entry',\n configuredEntry: startConfig.start.entry,\n defaultEntry: 'start',\n resolvedSrcDirectory,\n required: false,\n })\n resolvedStartConfig.startFilePath = startFilePath\n\n const routerFilePath = resolveEntry({\n type: 'router entry',\n configuredEntry: startConfig.router.entry,\n defaultEntry: 'router',\n resolvedSrcDirectory,\n required: true,\n })\n resolvedStartConfig.routerFilePath = routerFilePath\n\n const clientEntryPath = resolveEntry({\n type: 'client entry',\n configuredEntry: startConfig.client.entry,\n defaultEntry: 'client',\n resolvedSrcDirectory,\n required: false,\n })\n\n const serverEntryPath = resolveEntry({\n type: 'server entry',\n configuredEntry: startConfig.server.entry,\n defaultEntry: 'server',\n resolvedSrcDirectory,\n required: false,\n })\n\n let clientAlias: string\n if (clientEntryPath) {\n clientAlias = vite.normalizePath(\n path.join('/@fs', path.resolve(root, clientEntryPath)),\n )\n } else {\n clientAlias = corePluginOpts.defaultEntryPaths.client\n }\n\n let serverAlias: string\n if (serverEntryPath) {\n serverAlias = vite.normalizePath(path.resolve(root, serverEntryPath))\n } else {\n serverAlias = corePluginOpts.defaultEntryPaths.server\n }\n\n let startAlias: string\n if (startFilePath) {\n startAlias = vite.normalizePath(path.resolve(root, startFilePath))\n } else {\n startAlias = corePluginOpts.defaultEntryPaths.start\n }\n\n const entryAliasConfiguration: Record<\n (typeof ENTRY_POINTS)[keyof typeof ENTRY_POINTS],\n string\n > = {\n [ENTRY_POINTS.start]: startAlias,\n [ENTRY_POINTS.router]: routerFilePath,\n [ENTRY_POINTS.client]: clientAlias,\n [ENTRY_POINTS.server]: serverAlias,\n }\n\n const startPackageName =\n `@tanstack/${corePluginOpts.framework}-start` as const\n\n // crawl packages that have start in \"peerDependencies\"\n // see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101\n\n // this is currently uncached; could be implemented similarly as vite handles lock file changes\n // see https://github.com/vitejs/vite/blob/557f797d29422027e8c451ca50dd84bf8c41b5f0/packages/vite/src/node/optimizer/index.ts#L1282\n\n const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({\n root: process.cwd(),\n isBuild: command === 'build',\n isFrameworkPkgByJson(pkgJson) {\n const peerDependencies = pkgJson['peerDependencies']\n\n if (peerDependencies) {\n return startPackageName in peerDependencies\n }\n\n return false\n },\n })\n\n return {\n base: viteAppBase,\n // see https://vite.dev/config/shared-options.html#apptype\n // this will prevent vite from injecting middlewares that we don't want\n appType: viteConfig.appType ?? 'custom',\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n rollupOptions: {\n input: {\n main: ENTRY_POINTS.client,\n },\n },\n outDir: getClientOutputDirectory(viteConfig),\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n\n build: {\n ssr: true,\n rollupOptions: {\n input:\n viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]\n ?.build?.rollupOptions?.input ?? ENTRY_POINTS.server,\n },\n outDir: getServerOutputDirectory(viteConfig),\n commonjsOptions: {\n include: [/node_modules/],\n },\n copyPublicDir:\n viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]\n ?.build?.copyPublicDir ?? false,\n },\n },\n },\n\n resolve: {\n noExternal: [\n // ENTRY_POINTS.start,\n '@tanstack/start**',\n `@tanstack/${corePluginOpts.framework}-start**`,\n ...crawlFrameworkPkgsResult.ssr.noExternal.sort(),\n ],\n alias: {\n ...entryAliasConfiguration,\n },\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_CLIENT_OUTPUT_DIR', getClientOutputDirectory(viteConfig)),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase),\n ...(command === 'serve' ? defineReplaceEnv('TSS_SHELL', startConfig.spa?.enabled ? 'true' : 'false') : {}),\n ...defineReplaceEnv('TSS_DEV_SERVER', command === 'serve' ? 'true' : 'false'),\n },\n builder: {\n sharedPlugins: true,\n async buildApp(builder) {\n const client = builder.environments[VITE_ENVIRONMENT_NAMES.client]\n const server = builder.environments[VITE_ENVIRONMENT_NAMES.server]\n\n if (!client) {\n throw new Error('Client environment not found')\n }\n\n if (!server) {\n throw new Error('SSR environment not found')\n }\n\n if (!client.isBuilt) {\n // Build the client bundle first\n await builder.build(client)\n }\n if (!server.isBuilt) {\n // Build the SSR bundle\n await builder.build(server)\n }\n const serverBundle = getBundle(VITE_ENVIRONMENT_NAMES.server)\n await postServerBuild({ builder, startConfig, serverBundle })\n },\n },\n }\n },\n },\n tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(corePluginOpts.framework),\n createServerFnPlugin(corePluginOpts.framework),\n\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${corePluginOpts.framework}-start/client'`,\n replacer: (d) => `createClientRpc('${d.functionId}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server'`,\n replacer: (d) => `createServerRpc('${d.functionId}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n }),\n loadEnvPlugin(),\n startManifestPlugin({\n getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),\n }),\n devServerPlugin({ getConfig }),\n {\n name: 'tanstack-start:core:capture-bundle',\n applyToEnvironment(e) {\n return (\n e.name === VITE_ENVIRONMENT_NAMES.client ||\n e.name === VITE_ENVIRONMENT_NAMES.server\n )\n },\n enforce: 'post',\n generateBundle(_options, bundle) {\n const environment = this.environment.name as ViteEnvironmentNames\n if (!Object.values(VITE_ENVIRONMENT_NAMES).includes(environment)) {\n throw new Error(`Unknown environment: ${environment}`)\n }\n capturedBundle[environment] = bundle\n },\n },\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: J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S,uBAAuB;AAAA,MAAA;AAAA,IAClC,CACD;AAAA,IACD,cAAA;AAAA,IACA,oBAAoB;AAAA,MAClB,iBAAiB,MAAM,UAAU,uBAAuB,MAAM;AAAA,IAAA,CAC/D;AAAA,IACD,gBAAgB,EAAE,WAAW;AAAA,IAC7B;AAAA,MACE,MAAM;AAAA,MACN,mBAAmB,GAAG;AACpB,eACE,EAAE,SAAS,uBAAuB,UAClC,EAAE,SAAS,uBAAuB;AAAA,MAEtC;AAAA,MACA,SAAS;AAAA,MACT,eAAe,UAAU,QAAQ;AAC/B,cAAM,cAAc,KAAK,YAAY;AACrC,YAAI,CAAC,OAAO,OAAO,sBAAsB,EAAE,SAAS,WAAW,GAAG;AAChE,gBAAM,IAAI,MAAM,wBAAwB,WAAW,EAAE;AAAA,QACvD;AACA,uBAAe,WAAW,IAAI;AAAA,MAChC;AAAA,IAAA;AAAA,EACF;AAEJ;AAEA,SAAS,iBACP,KACA,OACsE;AACtE,SAAO;AAAA,IACL,CAAC,eAAe,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAAA;AAEpD;"}
1
+ {"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { crawlFrameworkPkgs } from 'vitefu'\nimport { join } from 'pathe'\nimport { escapePath } from 'tinyglobby'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin/plugin'\nimport { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { parseStartConfig } from './schema'\nimport { resolveEntry } from './resolve-entries'\nimport {\n getClientOutputDirectory,\n getServerOutputDirectory,\n} from './output-directory'\nimport { postServerBuild } from './post-server-build'\nimport { createServerFnPlugin } from './create-server-fn-plugin/plugin'\nimport type { ViteEnvironmentNames } from './constants'\nimport type {\n TanStackStartInputConfig,\n TanStackStartOutputConfig,\n} from './schema'\nimport type { PluginOption } from 'vite'\nimport type { CompileStartFrameworkOptions } from './start-compiler-plugin/compilers'\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n defaultEntryPaths: {\n client: string\n server: string\n start: string\n }\n}\n\nexport interface ResolvedStartConfig {\n root: string\n startFilePath: string | undefined\n routerFilePath: string\n srcDirectory: string\n}\n\nexport type GetConfigFn = () => {\n startConfig: TanStackStartOutputConfig\n resolvedStartConfig: ResolvedStartConfig\n}\nexport function TanStackStartVitePluginCore(\n corePluginOpts: TanStackStartVitePluginCoreOptions,\n startPluginOpts: TanStackStartInputConfig,\n): Array<PluginOption> {\n const resolvedStartConfig: ResolvedStartConfig = {\n root: '',\n startFilePath: undefined,\n routerFilePath: '',\n srcDirectory: '',\n }\n\n let startConfig: TanStackStartOutputConfig | null\n const getConfig: GetConfigFn = () => {\n if (!resolvedStartConfig.root) {\n throw new Error(`Cannot get config before root is resolved`)\n }\n if (!startConfig) {\n startConfig = parseStartConfig(\n startPluginOpts,\n corePluginOpts,\n resolvedStartConfig.root,\n )\n }\n return { startConfig, resolvedStartConfig }\n }\n\n const capturedBundle: Partial<\n Record<ViteEnvironmentNames, vite.Rollup.OutputBundle>\n > = {}\n\n function getBundle(envName: ViteEnvironmentNames): vite.Rollup.OutputBundle {\n const bundle = capturedBundle[envName]\n if (!bundle) {\n throw new Error(`No bundle captured for environment: ${envName}`)\n }\n return bundle\n }\n\n return [\n {\n name: 'tanstack-start-core:config',\n enforce: 'pre',\n async config(viteConfig, { command }) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const root = viteConfig.root || process.cwd()\n resolvedStartConfig.root = root\n\n const { startConfig } = getConfig()\n const resolvedSrcDirectory = join(root, startConfig.srcDirectory)\n resolvedStartConfig.srcDirectory = resolvedSrcDirectory\n\n const startFilePath = resolveEntry({\n type: 'start entry',\n configuredEntry: startConfig.start.entry,\n defaultEntry: 'start',\n resolvedSrcDirectory,\n required: false,\n })\n resolvedStartConfig.startFilePath = startFilePath\n\n const routerFilePath = resolveEntry({\n type: 'router entry',\n configuredEntry: startConfig.router.entry,\n defaultEntry: 'router',\n resolvedSrcDirectory,\n required: true,\n })\n resolvedStartConfig.routerFilePath = routerFilePath\n\n const clientEntryPath = resolveEntry({\n type: 'client entry',\n configuredEntry: startConfig.client.entry,\n defaultEntry: 'client',\n resolvedSrcDirectory,\n required: false,\n })\n\n const serverEntryPath = resolveEntry({\n type: 'server entry',\n configuredEntry: startConfig.server.entry,\n defaultEntry: 'server',\n resolvedSrcDirectory,\n required: false,\n })\n\n const clientAlias = vite.normalizePath(\n clientEntryPath ?? corePluginOpts.defaultEntryPaths.client,\n )\n const serverAlias = vite.normalizePath(\n serverEntryPath ?? corePluginOpts.defaultEntryPaths.server,\n )\n const startAlias = vite.normalizePath(\n startFilePath ?? corePluginOpts.defaultEntryPaths.start,\n )\n const routerAlias = vite.normalizePath(routerFilePath)\n\n const entryAliasConfiguration: Record<\n (typeof ENTRY_POINTS)[keyof typeof ENTRY_POINTS],\n string\n > = {\n [ENTRY_POINTS.client]: clientAlias,\n [ENTRY_POINTS.server]: serverAlias,\n [ENTRY_POINTS.start]: startAlias,\n [ENTRY_POINTS.router]: routerAlias,\n }\n\n const startPackageName =\n `@tanstack/${corePluginOpts.framework}-start` as const\n\n // crawl packages that have start in \"peerDependencies\"\n // see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101\n\n // this is currently uncached; could be implemented similarly as vite handles lock file changes\n // see https://github.com/vitejs/vite/blob/557f797d29422027e8c451ca50dd84bf8c41b5f0/packages/vite/src/node/optimizer/index.ts#L1282\n\n const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({\n root: process.cwd(),\n isBuild: command === 'build',\n isFrameworkPkgByJson(pkgJson) {\n const peerDependencies = pkgJson['peerDependencies']\n\n if (peerDependencies) {\n return startPackageName in peerDependencies\n }\n\n return false\n },\n })\n\n return {\n base: viteAppBase,\n // see https://vite.dev/config/shared-options.html#apptype\n // this will prevent vite from injecting middlewares that we don't want\n appType: viteConfig.appType ?? 'custom',\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n rollupOptions: {\n input: {\n main: ENTRY_POINTS.client,\n },\n },\n outDir: getClientOutputDirectory(viteConfig),\n },\n optimizeDeps: {\n // Ensure user code can be crawled for dependencies\n entries: [clientAlias, routerAlias].map((entry) =>\n // Entries are treated as `tinyglobby` patterns so need to be escaped\n escapePath(entry),\n ),\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n build: {\n ssr: true,\n rollupOptions: {\n input:\n viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]\n ?.build?.rollupOptions?.input ?? ENTRY_POINTS.server,\n },\n outDir: getServerOutputDirectory(viteConfig),\n commonjsOptions: {\n include: [/node_modules/],\n },\n copyPublicDir:\n viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]\n ?.build?.copyPublicDir ?? false,\n },\n optimizeDeps: {\n // Ensure user code can be crawled for dependencies\n entries: [serverAlias, startAlias, routerAlias].map((entry) =>\n // Entries are treated as `tinyglobby` patterns so need to be escaped\n escapePath(entry),\n ),\n },\n },\n },\n\n resolve: {\n noExternal: [\n // ENTRY_POINTS.start,\n '@tanstack/start**',\n `@tanstack/${corePluginOpts.framework}-start**`,\n ...crawlFrameworkPkgsResult.ssr.noExternal.sort(),\n ],\n alias: {\n ...entryAliasConfiguration,\n },\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_CLIENT_OUTPUT_DIR', getClientOutputDirectory(viteConfig)),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase),\n ...(command === 'serve' ? defineReplaceEnv('TSS_SHELL', startConfig.spa?.enabled ? 'true' : 'false') : {}),\n ...defineReplaceEnv('TSS_DEV_SERVER', command === 'serve' ? 'true' : 'false'),\n },\n builder: {\n sharedPlugins: true,\n async buildApp(builder) {\n const client = builder.environments[VITE_ENVIRONMENT_NAMES.client]\n const server = builder.environments[VITE_ENVIRONMENT_NAMES.server]\n\n if (!client) {\n throw new Error('Client environment not found')\n }\n\n if (!server) {\n throw new Error('SSR environment not found')\n }\n\n if (!client.isBuilt) {\n // Build the client bundle first\n await builder.build(client)\n }\n if (!server.isBuilt) {\n // Build the SSR bundle\n await builder.build(server)\n }\n const serverBundle = getBundle(VITE_ENVIRONMENT_NAMES.server)\n await postServerBuild({ builder, startConfig, serverBundle })\n },\n },\n }\n },\n },\n tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(corePluginOpts.framework),\n createServerFnPlugin(corePluginOpts.framework),\n\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${corePluginOpts.framework}-start/client'`,\n replacer: (d) => `createClientRpc('${d.functionId}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server'`,\n replacer: (d) => `createServerRpc('${d.functionId}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n }),\n loadEnvPlugin(),\n startManifestPlugin({\n getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),\n }),\n devServerPlugin({ getConfig }),\n {\n name: 'tanstack-start:core:capture-bundle',\n applyToEnvironment(e) {\n return (\n e.name === VITE_ENVIRONMENT_NAMES.client ||\n e.name === VITE_ENVIRONMENT_NAMES.server\n )\n },\n enforce: 'post',\n generateBundle(_options, bundle) {\n const environment = this.environment.name as ViteEnvironmentNames\n if (!Object.values(VITE_ENVIRONMENT_NAMES).includes(environment)) {\n throw new Error(`Unknown environment: ${environment}`)\n }\n capturedBundle[environment] = bundle\n },\n },\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: JSON.stringify(value),\n } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }\n}\n"],"names":["startConfig"],"mappings":";;;;;;;;;;;;;;;;;;AAiDO,SAAS,4BACd,gBACA,iBACqB;AACrB,QAAM,sBAA2C;AAAA,IAC/C,MAAM;AAAA,IACN,eAAe;AAAA,IACf,gBAAgB;AAAA,IAChB,cAAc;AAAA,EAAA;AAGhB,MAAI;AACJ,QAAM,YAAyB,MAAM;AACnC,QAAI,CAAC,oBAAoB,MAAM;AAC7B,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC7D;AACA,QAAI,CAAC,aAAa;AAChB,oBAAc;AAAA,QACZ;AAAA,QACA;AAAA,QACA,oBAAoB;AAAA,MAAA;AAAA,IAExB;AACA,WAAO,EAAE,aAAa,oBAAA;AAAA,EACxB;AAEA,QAAM,iBAEF,CAAA;AAEJ,WAAS,UAAU,SAAyD;AAC1E,UAAM,SAAS,eAAe,OAAO;AACrC,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,M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AAA,EACF;AAEJ;AAEA,SAAS,iBACP,KACA,OACsE;AACtE,SAAO;AAAA,IACL,CAAC,eAAe,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAAA;AAEpD;"}
@@ -52,10 +52,10 @@ export declare function parseStartConfig(opts: z.input<typeof tanstackStartOptio
52
52
  base: string;
53
53
  dir: string;
54
54
  };
55
- srcDirectory: string;
56
55
  start: {
57
56
  entry?: string | undefined;
58
57
  };
58
+ srcDirectory: string;
59
59
  serverFns: {
60
60
  base: string;
61
61
  };
@@ -3026,10 +3026,10 @@ declare const tanstackStartOptionsSchema: z.ZodDefault<z.ZodOptional<z.ZodObject
3026
3026
  base: string;
3027
3027
  dir: string;
3028
3028
  };
3029
- srcDirectory: string;
3030
3029
  start: {
3031
3030
  entry?: string | undefined;
3032
3031
  };
3032
+ srcDirectory: string;
3033
3033
  router: {
3034
3034
  entry?: string | undefined;
3035
3035
  } & {
@@ -3352,10 +3352,10 @@ declare const tanstackStartOptionsSchema: z.ZodDefault<z.ZodOptional<z.ZodObject
3352
3352
  base?: string | undefined;
3353
3353
  dir?: string | undefined;
3354
3354
  } | undefined;
3355
- srcDirectory?: string | undefined;
3356
3355
  start?: {
3357
3356
  entry?: string | undefined;
3358
3357
  } | undefined;
3358
+ srcDirectory?: string | undefined;
3359
3359
  router?: ({
3360
3360
  entry?: string | undefined;
3361
3361
  } & {
@@ -2,6 +2,7 @@ import * as babel from "@babel/core";
2
2
  import * as t from "@babel/types";
3
3
  import { findReferencedIdentifiers, deadCodeElimination } from "babel-dead-code-elimination";
4
4
  import { parseAst, generateFromAst } from "@tanstack/router-utils";
5
+ import { handleCreateMiddleware } from "../create-server-fn-plugin/handleCreateMiddleware.js";
5
6
  import { transformFuncs } from "./constants.js";
6
7
  import { handleCreateIsomorphicFnCallExpression } from "./isomorphicFn.js";
7
8
  import { handleCreateClientOnlyFnCallExpression, handleCreateServerOnlyFnCallExpression } from "./envOnly.js";
@@ -22,6 +23,11 @@ function compileStartOutputFactory(framework) {
22
23
  name: "createIsomorphicFn",
23
24
  handleCallExpression: handleCreateIsomorphicFnCallExpression,
24
25
  paths: []
26
+ },
27
+ createMiddleware: {
28
+ name: "createMiddleware",
29
+ handleCallExpression: handleCreateMiddleware,
30
+ paths: []
25
31
  }
26
32
  };
27
33
  const ast = parseAst(opts);
@@ -1 +1 @@
1
- {"version":3,"file":"compilers.js","sources":["../../../src/start-compiler-plugin/compilers.ts"],"sourcesContent":["import * as babel from '@babel/core'\nimport * as t from '@babel/types'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport { transformFuncs } from './constants'\nimport { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'\nimport {\n handleCreateClientOnlyFnCallExpression,\n handleCreateServerOnlyFnCallExpression,\n} from './envOnly'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const identifiers: Identifiers = {\n createServerOnlyFn: {\n name: 'createServerOnlyFn',\n handleCallExpression: handleCreateServerOnlyFnCallExpression,\n paths: [],\n },\n createClientOnlyFn: {\n name: 'createClientOnlyFn',\n handleCallExpression: handleCreateClientOnlyFnCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n }\n\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n transformFuncs.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n transformFuncs.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n // which are then called like \"TanStackStart.createServerFn()\"\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: Array<babel.NodePath>\n}\n"],"names":[],"mappings":";;;;;;;AAoBO,SAAS,0BACd,WACA;AACA,SAAO,SAAS,mBAAmB,MAAuC;AACxE,UAAM,cAA2B;AAAA,MAC/B,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,MAEV,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,MAEV,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,IACV;AAGF,UAAM,MAAM,SAAS,IAAI;AAEzB,UAAM,QAAQ,KAAK,OAAO;AAE1B,UAAM,YAAY,QAAQ,0BAA0B,GAAG,IAAI;AAE3D,UAAM,SAAS,KAAK;AAAA,MAClB,SAAS;AAAA,QACP,MAAM,aAAa;AACjB,sBAAY,SAAS;AAAA,YACnB,mBAAmB,CAAC,SAAS;AAC3B,kBAAI,KAAK,KAAK,OAAO,UAAU,aAAa,SAAS,UAAU;AAC7D;AAAA,cACF;AAGA,mBAAK,KAAK,WAAW,QAAQ,CAAC,cAAc;AAC1C,+BAAe,QAAQ,CAAC,kBAAkB;AACxC,wBAAM,aAAa,YAAY,aAAa;AAE5C,sBACE,UAAU,SAAS,qBACnB,UAAU,SAAS,SAAS,cAC5B;AACA,wBAAI,UAAU,SAAS,SAAS,eAAe;AAC7C,iCAAW,OAAO,UAAU,MAAM;AAAA,oBACpC;AAAA,kBACF;A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AA,MAC1B,YAAY;AAAA,MACZ,gBAAgB,KAAK;AAAA,MACrB,UAAU,KAAK;AAAA,IAAA,CAChB;AAAA,EACH;AACF;"}
1
+ {"version":3,"file":"compilers.js","sources":["../../../src/start-compiler-plugin/compilers.ts"],"sourcesContent":["import * as babel from '@babel/core'\nimport * as t from '@babel/types'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport { handleCreateMiddleware } from '../create-server-fn-plugin/handleCreateMiddleware'\nimport { transformFuncs } from './constants'\nimport { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'\nimport {\n handleCreateClientOnlyFnCallExpression,\n handleCreateServerOnlyFnCallExpression,\n} from './envOnly'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const identifiers: Identifiers = {\n createServerOnlyFn: {\n name: 'createServerOnlyFn',\n handleCallExpression: handleCreateServerOnlyFnCallExpression,\n paths: [],\n },\n createClientOnlyFn: {\n name: 'createClientOnlyFn',\n handleCallExpression: handleCreateClientOnlyFnCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddleware,\n paths: [],\n },\n }\n\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n transformFuncs.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n transformFuncs.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n // which are then called like \"TanStackStart.createServerFn()\"\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: Array<babel.NodePath>\n}\n"],"names":[],"mappings":";;;;;;;;AAqBO,SAAS,0BACd,WACA;AACA,SAAO,SAAS,mBAAmB,MAAuC;AACxE,UAAM,cAA2B;AAAA,MAC/B,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,MAEV,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,MAEV,oBAAoB;AAAA,QAClB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,MAEV,kBAAkB;AAAA,QAChB,MAAM;AAAA,QACN,sBAAsB;AAAA,QACtB,OAAO,CAAA;AAAA,MAAC;AAAA,IACV;AAGF,UAAM,MAAM,SAAS,IAAI;AAEzB,UAAM,QAAQ,KAAK,OAAO;AAE1B,UAAM,YAAY,QAAQ,0BAA0B,GAAG,IAAI;AAE3D,UAAM,SAAS,KAAK;AAAA,MAClB,SAAS;AAAA,QACP,MAAM,aAAa;AACjB,sBAAY,SAAS;AAAA,YACnB,mBAAmB,CAAC,SAAS;AAC3B,kBAAI,KAAK,KAAK,OAAO,UAAU,aAAa,SAAS,UAAU;AAC7D;AAAA,cACF;AAGA,mBAAK,KAAK,WAAW,QAAQ,CAAC,cAAc;AAC1C,+BAAe,QAAQ,CAAC,kBAAkB;AACxC,wBAAM,aAAa,YAAY,aAAa;AAE5C,sBACE,UAAU,SAAS,qBACnB,UAAU,SAAS,SAAS,cAC5B;AACA,wBAAI,UAAU,SAAS,SAAS,eAAe;AAC7C,iCAAW,OAAO,UAAU,MAAM;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EA,WAAO,gBAAgB,KAAK;AAAA,MAC1B,YAAY;AAAA,MACZ,gBAAgB,KAAK;AAAA,MACrB,UAAU,KAAK;AAAA,IAAA,CAChB;AAAA,EACH;AACF;"}
@@ -1 +1 @@
1
- export declare const transformFuncs: readonly ["createServerOnlyFn", "createClientOnlyFn", "createIsomorphicFn"];
1
+ export declare const transformFuncs: readonly ["createServerOnlyFn", "createClientOnlyFn", "createIsomorphicFn", "createMiddleware"];
@@ -1,7 +1,8 @@
1
1
  const transformFuncs = [
2
2
  "createServerOnlyFn",
3
3
  "createClientOnlyFn",
4
- "createIsomorphicFn"
4
+ "createIsomorphicFn",
5
+ "createMiddleware"
5
6
  ];
6
7
  export {
7
8
  transformFuncs
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerOnlyFn',\n 'createClientOnlyFn',\n 'createIsomorphicFn',\n] as const\n"],"names":[],"mappings":"AAAO,MAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AACF;"}
1
+ {"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerOnlyFn',\n 'createClientOnlyFn',\n 'createIsomorphicFn',\n 'createMiddleware',\n] as const\n"],"names":[],"mappings":"AAAO,MAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/start-plugin-core",
3
- "version": "1.132.7",
3
+ "version": "1.132.9",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -54,15 +54,16 @@
54
54
  "exsolve": "^1.0.7",
55
55
  "pathe": "^2.0.3",
56
56
  "srvx": "^0.8.2",
57
+ "tinyglobby": "^0.2.15",
57
58
  "ufo": "^1.5.4",
58
59
  "vitefu": "^1.1.1",
59
60
  "xmlbuilder2": "^3.1.1",
60
61
  "zod": "^3.24.2",
61
62
  "@tanstack/router-core": "1.132.7",
62
- "@tanstack/router-generator": "1.132.7",
63
63
  "@tanstack/router-plugin": "1.132.7",
64
- "@tanstack/server-functions-plugin": "1.132.0",
65
64
  "@tanstack/router-utils": "1.132.0",
65
+ "@tanstack/router-generator": "1.132.7",
66
+ "@tanstack/server-functions-plugin": "1.132.0",
66
67
  "@tanstack/start-server-core": "1.132.7"
67
68
  },
68
69
  "devDependencies": {
package/src/constants.ts CHANGED
@@ -12,8 +12,8 @@ export type ViteEnvironmentNames =
12
12
  // if a user has a custom server/client entry point file, resolve.alias will point to this
13
13
  // otherwise it will be aliased to the default entry point in the respective framework plugin
14
14
  export const ENTRY_POINTS = {
15
- client: 'virtual:tanstack-start-client-entry',
16
- server: 'virtual:tanstack-start-server-request-entry',
15
+ client: '__tanstack-start-client-entry__',
16
+ server: '__tanstack-start-server-entry__',
17
17
  // the start entry point must always be provided by the user
18
18
  start: '#tanstack-start-entry',
19
19
  router: '#tanstack-router-entry',
@@ -29,10 +29,18 @@ type ExportEntry =
29
29
 
30
30
  type Kind = 'None' | `Root` | `Builder` | LookupKind
31
31
 
32
- type LookupKind = 'ServerFn' | 'Middleware'
32
+ export type LookupKind = 'ServerFn' | 'Middleware'
33
+
34
+ const LookupSetup: Record<
35
+ LookupKind,
36
+ { candidateCallIdentifier: Set<string> }
37
+ > = {
38
+ ServerFn: { candidateCallIdentifier: new Set(['handler']) },
39
+ Middleware: {
40
+ candidateCallIdentifier: new Set(['server', 'client', 'createMiddlewares']),
41
+ },
42
+ }
33
43
 
34
- const validLookupKinds: Array<LookupKind> = ['ServerFn', 'Middleware']
35
- const candidateCallIdentifier = ['handler', 'server', 'client']
36
44
  export type LookupConfig = {
37
45
  libName: string
38
46
  rootExport: string
@@ -48,14 +56,18 @@ interface ModuleInfo {
48
56
  export class ServerFnCompiler {
49
57
  private moduleCache = new Map<string, ModuleInfo>()
50
58
  private initialized = false
59
+ private validLookupKinds: Set<LookupKind>
51
60
  constructor(
52
61
  private options: {
53
62
  env: 'client' | 'server'
54
63
  lookupConfigurations: Array<LookupConfig>
64
+ lookupKinds: Set<LookupKind>
55
65
  loadModule: (id: string) => Promise<void>
56
66
  resolveId: (id: string, importer?: string) => Promise<string | null>
57
67
  },
58
- ) {}
68
+ ) {
69
+ this.validLookupKinds = options.lookupKinds
70
+ }
59
71
 
60
72
  private async init(id: string) {
61
73
  await Promise.all(
@@ -207,7 +219,7 @@ export class ServerFnCompiler {
207
219
  }> = []
208
220
  for (const handler of candidates) {
209
221
  const kind = await this.resolveExprKind(handler, id)
210
- if (validLookupKinds.includes(kind as LookupKind)) {
222
+ if (this.validLookupKinds.has(kind as LookupKind)) {
211
223
  toRewrite.push({ callExpression: handler, kind: kind as LookupKind })
212
224
  }
213
225
  }
@@ -261,7 +273,10 @@ export class ServerFnCompiler {
261
273
 
262
274
  for (const binding of bindings.values()) {
263
275
  if (binding.type === 'var') {
264
- const handler = isCandidateCallExpression(binding.init)
276
+ const handler = isCandidateCallExpression(
277
+ binding.init,
278
+ this.validLookupKinds,
279
+ )
265
280
  if (handler) {
266
281
  candidates.push(handler)
267
282
  }
@@ -368,7 +383,7 @@ export class ServerFnCompiler {
368
383
  if (calleeKind === `Root` || calleeKind === `Builder`) {
369
384
  return `Builder`
370
385
  }
371
- for (const kind of validLookupKinds) {
386
+ for (const kind of this.validLookupKinds) {
372
387
  if (calleeKind === kind) {
373
388
  return kind
374
389
  }
@@ -407,16 +422,18 @@ export class ServerFnCompiler {
407
422
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
408
423
  const prop = callee.property.name
409
424
 
410
- if (prop === 'handler') {
425
+ if (
426
+ this.validLookupKinds.has('ServerFn') &&
427
+ LookupSetup['ServerFn'].candidateCallIdentifier.has(prop)
428
+ ) {
411
429
  const base = await this.resolveExprKind(callee.object, fileId, visited)
412
430
  if (base === 'Root' || base === 'Builder') {
413
431
  return 'ServerFn'
414
432
  }
415
433
  return 'None'
416
434
  } else if (
417
- prop === 'client' ||
418
- prop === 'server' ||
419
- prop === 'createMiddleware'
435
+ this.validLookupKinds.has('Middleware') &&
436
+ LookupSetup['Middleware'].candidateCallIdentifier.has(prop)
420
437
  ) {
421
438
  const base = await this.resolveExprKind(callee.object, fileId, visited)
422
439
  if (base === 'Root' || base === 'Builder' || base === 'Middleware') {
@@ -483,6 +500,7 @@ export class ServerFnCompiler {
483
500
 
484
501
  function isCandidateCallExpression(
485
502
  node: t.Node | null | undefined,
503
+ lookupKinds: Set<LookupKind>,
486
504
  ): undefined | t.CallExpression {
487
505
  if (!t.isCallExpression(node)) return undefined
488
506
 
@@ -490,9 +508,11 @@ function isCandidateCallExpression(
490
508
  if (!t.isMemberExpression(callee) || !t.isIdentifier(callee.property)) {
491
509
  return undefined
492
510
  }
493
- if (!candidateCallIdentifier.includes(callee.property.name)) {
494
- return undefined
511
+ for (const kind of lookupKinds) {
512
+ if (LookupSetup[kind].candidateCallIdentifier.has(callee.property.name)) {
513
+ return node
514
+ }
495
515
  }
496
516
 
497
- return node
517
+ return undefined
498
518
  }
@@ -1,5 +1,6 @@
1
1
  import { VITE_ENVIRONMENT_NAMES } from '../constants'
2
2
  import { ServerFnCompiler } from './compiler'
3
+ import type { LookupConfig, LookupKind } from './compiler'
3
4
  import type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'
4
5
  import type { ViteEnvironmentNames } from '../constants'
5
6
  import type { PluginOption } from 'vite'
@@ -8,6 +9,36 @@ function cleanId(id: string): string {
8
9
  return id.split('?')[0]!
9
10
  }
10
11
 
12
+ const LookupKindsPerEnv: Record<'client' | 'server', Set<LookupKind>> = {
13
+ client: new Set(['Middleware', 'ServerFn'] as const),
14
+ server: new Set(['ServerFn'] as const),
15
+ }
16
+
17
+ const getLookupConfigurationsForEnv = (
18
+ env: 'client' | 'server',
19
+ framework: CompileStartFrameworkOptions,
20
+ ): Array<LookupConfig> => {
21
+ const createServerFnConfig: LookupConfig = {
22
+ libName: `@tanstack/${framework}-start`,
23
+ rootExport: 'createServerFn',
24
+ }
25
+ if (env === 'client') {
26
+ return [
27
+ {
28
+ libName: `@tanstack/${framework}-start`,
29
+ rootExport: 'createMiddleware',
30
+ },
31
+ {
32
+ libName: `@tanstack/${framework}-start`,
33
+ rootExport: 'createStart',
34
+ },
35
+
36
+ createServerFnConfig,
37
+ ]
38
+ } else {
39
+ return [createServerFnConfig]
40
+ }
41
+ }
11
42
  export function createServerFnPlugin(
12
43
  framework: CompileStartFrameworkOptions,
13
44
  ): PluginOption {
@@ -52,8 +83,9 @@ export function createServerFnPlugin(
52
83
  exclude: new RegExp(`${SERVER_FN_LOOKUP}$`),
53
84
  },
54
85
  code: {
55
- // only scan files that mention `.handler(` | `.server(` | `.client(`
56
- include: [/\.handler\(/, /\.server\(/, /\.client\(/],
86
+ // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env
87
+ // only scan files that mention `.handler(` | `.createMiddleware()`
88
+ include: [/\.handler\(/, /.createMiddleware\(\)/],
57
89
  },
58
90
  },
59
91
  async handler(code, id) {
@@ -73,21 +105,11 @@ export function createServerFnPlugin(
73
105
 
74
106
  compiler = new ServerFnCompiler({
75
107
  env,
76
- lookupConfigurations: [
77
- {
78
- libName: `@tanstack/${framework}-start`,
79
- rootExport: 'createMiddleware',
80
- },
81
-
82
- {
83
- libName: `@tanstack/${framework}-start`,
84
- rootExport: 'createServerFn',
85
- },
86
- {
87
- libName: `@tanstack/${framework}-start`,
88
- rootExport: 'createStart',
89
- },
90
- ],
108
+ lookupKinds: LookupKindsPerEnv[env],
109
+ lookupConfigurations: getLookupConfigurationsForEnv(
110
+ env,
111
+ framework,
112
+ ),
91
113
  loadModule: async (id: string) => {
92
114
  if (this.environment.mode === 'build') {
93
115
  const loaded = await this.load({ id })
package/src/plugin.ts CHANGED
@@ -1,10 +1,10 @@
1
- import path from 'node:path'
2
1
  import { trimPathRight } from '@tanstack/router-core'
3
2
  import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
4
3
  import { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'
5
4
  import * as vite from 'vite'
6
5
  import { crawlFrameworkPkgs } from 'vitefu'
7
6
  import { join } from 'pathe'
7
+ import { escapePath } from 'tinyglobby'
8
8
  import { startManifestPlugin } from './start-manifest-plugin/plugin'
9
9
  import { startCompilerPlugin } from './start-compiler-plugin/plugin'
10
10
  import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from './constants'
@@ -134,37 +134,25 @@ export function TanStackStartVitePluginCore(
134
134
  required: false,
135
135
  })
136
136
 
137
- let clientAlias: string
138
- if (clientEntryPath) {
139
- clientAlias = vite.normalizePath(
140
- path.join('/@fs', path.resolve(root, clientEntryPath)),
141
- )
142
- } else {
143
- clientAlias = corePluginOpts.defaultEntryPaths.client
144
- }
145
-
146
- let serverAlias: string
147
- if (serverEntryPath) {
148
- serverAlias = vite.normalizePath(path.resolve(root, serverEntryPath))
149
- } else {
150
- serverAlias = corePluginOpts.defaultEntryPaths.server
151
- }
152
-
153
- let startAlias: string
154
- if (startFilePath) {
155
- startAlias = vite.normalizePath(path.resolve(root, startFilePath))
156
- } else {
157
- startAlias = corePluginOpts.defaultEntryPaths.start
158
- }
137
+ const clientAlias = vite.normalizePath(
138
+ clientEntryPath ?? corePluginOpts.defaultEntryPaths.client,
139
+ )
140
+ const serverAlias = vite.normalizePath(
141
+ serverEntryPath ?? corePluginOpts.defaultEntryPaths.server,
142
+ )
143
+ const startAlias = vite.normalizePath(
144
+ startFilePath ?? corePluginOpts.defaultEntryPaths.start,
145
+ )
146
+ const routerAlias = vite.normalizePath(routerFilePath)
159
147
 
160
148
  const entryAliasConfiguration: Record<
161
149
  (typeof ENTRY_POINTS)[keyof typeof ENTRY_POINTS],
162
150
  string
163
151
  > = {
164
- [ENTRY_POINTS.start]: startAlias,
165
- [ENTRY_POINTS.router]: routerFilePath,
166
152
  [ENTRY_POINTS.client]: clientAlias,
167
153
  [ENTRY_POINTS.server]: serverAlias,
154
+ [ENTRY_POINTS.start]: startAlias,
155
+ [ENTRY_POINTS.router]: routerAlias,
168
156
  }
169
157
 
170
158
  const startPackageName =
@@ -206,10 +194,16 @@ export function TanStackStartVitePluginCore(
206
194
  },
207
195
  outDir: getClientOutputDirectory(viteConfig),
208
196
  },
197
+ optimizeDeps: {
198
+ // Ensure user code can be crawled for dependencies
199
+ entries: [clientAlias, routerAlias].map((entry) =>
200
+ // Entries are treated as `tinyglobby` patterns so need to be escaped
201
+ escapePath(entry),
202
+ ),
203
+ },
209
204
  },
210
205
  [VITE_ENVIRONMENT_NAMES.server]: {
211
206
  consumer: 'server',
212
-
213
207
  build: {
214
208
  ssr: true,
215
209
  rollupOptions: {
@@ -225,6 +219,13 @@ export function TanStackStartVitePluginCore(
225
219
  viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]
226
220
  ?.build?.copyPublicDir ?? false,
227
221
  },
222
+ optimizeDeps: {
223
+ // Ensure user code can be crawled for dependencies
224
+ entries: [serverAlias, startAlias, routerAlias].map((entry) =>
225
+ // Entries are treated as `tinyglobby` patterns so need to be escaped
226
+ escapePath(entry),
227
+ ),
228
+ },
228
229
  },
229
230
  },
230
231
 
@@ -6,6 +6,7 @@ import {
6
6
  findReferencedIdentifiers,
7
7
  } from 'babel-dead-code-elimination'
8
8
  import { generateFromAst, parseAst } from '@tanstack/router-utils'
9
+ import { handleCreateMiddleware } from '../create-server-fn-plugin/handleCreateMiddleware'
9
10
  import { transformFuncs } from './constants'
10
11
  import { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'
11
12
  import {
@@ -38,6 +39,11 @@ export function compileStartOutputFactory(
38
39
  handleCallExpression: handleCreateIsomorphicFnCallExpression,
39
40
  paths: [],
40
41
  },
42
+ createMiddleware: {
43
+ name: 'createMiddleware',
44
+ handleCallExpression: handleCreateMiddleware,
45
+ paths: [],
46
+ },
41
47
  }
42
48
 
43
49
  const ast = parseAst(opts)
@@ -2,4 +2,5 @@ export const transformFuncs = [
2
2
  'createServerOnlyFn',
3
3
  'createClientOnlyFn',
4
4
  'createIsomorphicFn',
5
+ 'createMiddleware',
5
6
  ] as const