@tanstack/start-plugin-core 1.134.20 → 1.135.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,7 +71,7 @@ function handleCreateServerFn(path, opts) {
71
71
  )
72
72
  )
73
73
  ],
74
- [t.directive(t.directiveLiteral("use server"))]
74
+ [t.directive(t.directiveLiteral(opts.directive))]
75
75
  )
76
76
  )
77
77
  );
@@ -1 +1 @@
1
- {"version":3,"file":"handleCreateServerFn.js","sources":["../../../src/create-server-fn-plugin/handleCreateServerFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n codeFrameError,\n getRootCallExpression,\n} from '../start-compiler-plugin/utils'\nimport type * as babel from '@babel/core'\n\nexport function handleCreateServerFn(\n path: babel.NodePath<t.CallExpression>,\n opts: {\n env: 'client' | 'server'\n code: string\n directive: string\n },\n) {\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const validMethods = ['middleware', 'inputValidator', 'handler'] as const\n type ValidMethods = (typeof validMethods)[number]\n const callExpressionPaths: Record<\n ValidMethods,\n babel.NodePath<t.CallExpression> | null\n > = {\n middleware: null,\n inputValidator: null,\n handler: null,\n }\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property.name as ValidMethods\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.inputValidator) {\n const innerInputExpression =\n callExpressionPaths.inputValidator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().inputValidator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (\n t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)\n ) {\n callExpressionPaths.inputValidator.replaceWith(\n callExpressionPaths.inputValidator.node.callee.object,\n )\n }\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n"],"names":[],"mappings":";;AAOO,SAAS,qBACd,MACA,MAKA;AAKA,QAAM,eAAe,CAAC,cAAc,kBAAkB,SAAS;AAE/D,QAAM,sBAGF;AAAA,IACF,YAAY;AAAA,IACZ,gBAAgB;AAAA,IAChB,SAAS;AAAA,EAAA;AAGX,QAAM,qBAAqB,sBAAsB,IAAI;AASrD,MAAI,CAAC,mBAAmB,WAAW,wBAAwB;AACzD,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAGA,QAAM,qBAAqB,mBAAmB,WAAW;AACzD,QAAM,uBAAwB,mBAAmB,GAAoB;AAErE,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SAAS;AAEhD,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MAAI,oBAAoB,gBAAgB;AACtC,UAAM,uBACJ,oBAAoB,eAAe,KAAK,UAAU,CAAC;AAErD,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QAAI,KAAK,QAAQ,UAAU;AACzB,UACE,EAAE,mBAAmB,oBAAoB,eAAe,KAAK,MAAM,GACnE;AACA,4BAAoB,eAAe;AAAA,UACjC,oBAAoB,eAAe,KAAK,OAAO;AAAA,QAAA;AAAA,MAEnD;AAAA,IACF;AAAA,EACF;AAKA,QAAM,gBAAgB,oBAAoB,SAAS;AAAA,IACjD;AAAA,EAAA;AAGF,MAAI,CAAC,oBAAoB,WAAW,CAAC,cAAc,MAAM;AACvD,UAAM;AAAA,MACJ,KAAK;AAAA,MACL,KAAK,KAAK,OAAO;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAEA,QAAM,YAAY,cAAc;AAuBhC,MAAI,EAAE,aAAa,SAAS,GAAG;AAC7B,QAAI,KAAK,QAAQ,UAAU;AAEzB,YAAM,UAAU,cAAc,MAAM,WAAW,UAAU,IAAI;AAE7D,UAAI,SAAS;AACX,gBAAQ,KAAK,OAAA;AAAA,MACf;AAAA,IACF;AAAA,EAEF;AAEA,gBAAc;AAAA,IACZ,EAAE;AAAA,MACA,CAAC,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,QAAQ,CAAC;AAAA,MAC7C,EAAE;AAAA;AAAA;AAAA,QAGA;AAAA,UACE,EAAE;AAAA,YACA,EAAE;AAAA,cACA,EAAE,WAAW,GAAG,oBAAoB,kBAAkB;AAAA,cACtD,CAAC,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,QAAQ,CAAC;AAAA,YAAA;AAAA,UAC/C;AAAA,QACF;AAAA,QAEF,CAAC,EAAE,UAAU,EAAE,iBAAiB,YAAY,CAAC,CAAC;AAAA,MAAA;AAAA,IAChD;AAAA,EACF;AAGF,MAAI,KAAK,QAAQ,UAAU;AACzB,wBAAoB,QAAQ,KAAK,UAAU,KAAK,SAAS;AAAA,EAC3D;AACF;"}
1
+ {"version":3,"file":"handleCreateServerFn.js","sources":["../../../src/create-server-fn-plugin/handleCreateServerFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n codeFrameError,\n getRootCallExpression,\n} from '../start-compiler-plugin/utils'\nimport type * as babel from '@babel/core'\n\nexport function handleCreateServerFn(\n path: babel.NodePath<t.CallExpression>,\n opts: {\n env: 'client' | 'server'\n code: string\n directive: string\n },\n) {\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const validMethods = ['middleware', 'inputValidator', 'handler'] as const\n type ValidMethods = (typeof validMethods)[number]\n const callExpressionPaths: Record<\n ValidMethods,\n babel.NodePath<t.CallExpression> | null\n > = {\n middleware: null,\n inputValidator: null,\n handler: null,\n }\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property.name as ValidMethods\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.inputValidator) {\n const innerInputExpression =\n callExpressionPaths.inputValidator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().inputValidator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (\n t.isMemberExpression(callExpressionPaths.inputValidator.node.callee)\n ) {\n callExpressionPaths.inputValidator.replaceWith(\n callExpressionPaths.inputValidator.node.callee.object,\n )\n }\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral(opts.directive))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n"],"names":[],"mappings":";;AAOO,SAAS,qBACd,MACA,MAKA;AAKA,QAAM,eAAe,CAAC,cAAc,kBAAkB,SAAS;AAE/D,QAAM,sBAGF;AAAA,IACF,YAAY;AAAA,IACZ,gBAAgB;AAAA,IAChB,SAAS;AAAA,EAAA;AAGX,QAAM,qBAAqB,sBAAsB,IAAI;AASrD,MAAI,CAAC,mBAAmB,WAAW,wBAAwB;AACzD,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAGA,QAAM,qBAAqB,mBAAmB,WAAW;AACzD,QAAM,uBAAwB,mBAAmB,GAAoB;AAErE,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,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QAAQ,CAAC;AAAA,YAAA;AAAA,UAC/C;AAAA,QACF;AAAA,QAEF,CAAC,EAAE,UAAU,EAAE,iBAAiB,KAAK,SAAS,CAAC,CAAC;AAAA,MAAA;AAAA,IAClD;AAAA,EACF;AAGF,MAAI,KAAK,QAAQ,UAAU;AACzB,wBAAoB,QAAQ,KAAK,UAAU,KAAK,SAAS;AAAA,EAC3D;AACF;"}
@@ -3,4 +3,8 @@ import { PluginOption } from 'vite';
3
3
  export declare function createServerFnPlugin(opts: {
4
4
  framework: CompileStartFrameworkOptions;
5
5
  directive: string;
6
+ environments: Array<{
7
+ name: string;
8
+ type: 'client' | 'server';
9
+ }>;
6
10
  }): PluginOption;
@@ -1,4 +1,4 @@
1
- import { TRANSFORM_ID_REGEX, VITE_ENVIRONMENT_NAMES } from "../constants.js";
1
+ import { TRANSFORM_ID_REGEX } from "../constants.js";
2
2
  import { ServerFnCompiler } from "./compiler.js";
3
3
  function cleanId(id) {
4
4
  return id.split("?")[0];
@@ -28,38 +28,16 @@ const getLookupConfigurationsForEnv = (env, framework) => {
28
28
  return [createServerFnConfig];
29
29
  }
30
30
  };
31
+ const SERVER_FN_LOOKUP = "server-fn-module-lookup";
31
32
  function createServerFnPlugin(opts) {
32
- const SERVER_FN_LOOKUP = "server-fn-module-lookup";
33
33
  const compilers = {};
34
- return [
35
- {
36
- name: "tanstack-start-core:capture-server-fn-module-lookup",
37
- // we only need this plugin in dev mode
38
- apply: "serve",
39
- applyToEnvironment(env) {
40
- return [
41
- VITE_ENVIRONMENT_NAMES.client,
42
- VITE_ENVIRONMENT_NAMES.server
43
- ].includes(env.name);
44
- },
45
- transform: {
46
- filter: {
47
- id: new RegExp(`${SERVER_FN_LOOKUP}$`)
48
- },
49
- handler(code, id) {
50
- const compiler = compilers[this.environment.name];
51
- compiler?.ingestModule({ code, id: cleanId(id) });
52
- }
53
- }
54
- },
55
- {
56
- name: "tanstack-start-core::server-fn",
34
+ function perEnvServerFnPlugin(environment) {
35
+ const transformCodeFilter = environment.type === "client" ? [/\.\s*handler\(/, /\.\s*createMiddleware\(\)/] : [/\.\s*handler\(/];
36
+ return {
37
+ name: `tanstack-start-core::server-fn:${environment.name}`,
57
38
  enforce: "pre",
58
39
  applyToEnvironment(env) {
59
- return [
60
- VITE_ENVIRONMENT_NAMES.client,
61
- VITE_ENVIRONMENT_NAMES.server
62
- ].includes(env.name);
40
+ return env.name === environment.name;
63
41
  },
64
42
  transform: {
65
43
  filter: {
@@ -68,25 +46,18 @@ function createServerFnPlugin(opts) {
68
46
  include: TRANSFORM_ID_REGEX
69
47
  },
70
48
  code: {
71
- // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env
72
- // only scan files that mention `.handler(` | `.createMiddleware()`
73
- include: [/\.\s*handler\(/, /\.\s*createMiddleware\(\)/]
49
+ include: transformCodeFilter
74
50
  }
75
51
  },
76
52
  async handler(code, id) {
77
53
  let compiler = compilers[this.environment.name];
78
54
  if (!compiler) {
79
- const env = this.environment.name === VITE_ENVIRONMENT_NAMES.client ? "client" : this.environment.name === VITE_ENVIRONMENT_NAMES.server ? "server" : (() => {
80
- throw new Error(
81
- `Environment ${this.environment.name} not configured`
82
- );
83
- })();
84
55
  compiler = new ServerFnCompiler({
85
- env,
56
+ env: environment.type,
86
57
  directive: opts.directive,
87
- lookupKinds: LookupKindsPerEnv[env],
58
+ lookupKinds: LookupKindsPerEnv[environment.type],
88
59
  lookupConfigurations: getLookupConfigurationsForEnv(
89
- env,
60
+ environment.type,
90
61
  opts.framework
91
62
  ),
92
63
  loadModule: async (id2) => {
@@ -138,6 +109,26 @@ function createServerFnPlugin(opts) {
138
109
  }
139
110
  });
140
111
  }
112
+ };
113
+ }
114
+ return [
115
+ ...opts.environments.map(perEnvServerFnPlugin),
116
+ {
117
+ name: "tanstack-start-core:capture-server-fn-module-lookup",
118
+ // we only need this plugin in dev mode
119
+ apply: "serve",
120
+ applyToEnvironment(env) {
121
+ return !!opts.environments.find((e) => e.name === env.name);
122
+ },
123
+ transform: {
124
+ filter: {
125
+ id: new RegExp(`${SERVER_FN_LOOKUP}$`)
126
+ },
127
+ handler(code, id) {
128
+ const compiler = compilers[this.environment.name];
129
+ compiler?.ingestModule({ code, id: cleanId(id) });
130
+ }
131
+ }
141
132
  }
142
133
  ];
143
134
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["../../../src/create-server-fn-plugin/plugin.ts"],"sourcesContent":["import { TRANSFORM_ID_REGEX, 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(opts: {\n framework: CompileStartFrameworkOptions\n directive: string\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 include: TRANSFORM_ID_REGEX,\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: [/\\.\\s*handler\\(/, /\\.\\s*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 directive: opts.directive,\n lookupKinds: LookupKindsPerEnv[env],\n lookupConfigurations: getLookupConfigurationsForEnv(\n env,\n opts.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,IAAI,CAAC,UAAU,CAAU;AACvC;AAEA,MAAM,gCAAgC,CACpC,KACA,cACwB;AACxB,QAAM,uBAAqC;AAAA,IACzC,SAAS,aAAa,SAAS;AAAA,IAC/B,YAAY;AAAA,EAAA;AAEd,MAAI,QAAQ,UAAU;AACpB,WAAO;AAAA,MACL;AAAA,QACE,SAAS,aAAa,SAAS;AAAA,QAC/B,YAAY;AAAA,MAAA;AAAA,MAEd;AAAA,QACE,SAAS,aAAa,SAAS;AAAA,QAC/B,YAAY;AAAA,MAAA;AAAA,MAGd;AAAA,IAAA;AAAA,EAEJ,OAAO;AACL,WAAO,CAAC,oBAAoB;AAAA,EAC9B;AACF;AACO,SAAS,qBAAqB,MAGpB;AACf,QAAM,mBAAmB;AAEzB,QAAM,YAAqE,CAAA;AAC3E,SAAO;AAAA,IACL;AAAA,MACE,MAAM;AAAA;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E;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 { TRANSFORM_ID_REGEX } from '../constants'\nimport { ServerFnCompiler } from './compiler'\nimport type { LookupConfig, LookupKind } from './compiler'\nimport type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'\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}\nconst SERVER_FN_LOOKUP = 'server-fn-module-lookup'\nexport function createServerFnPlugin(opts: {\n framework: CompileStartFrameworkOptions\n directive: string\n environments: Array<{ name: string; type: 'client' | 'server' }>\n}): PluginOption {\n const compilers: Record<string /* envName */, ServerFnCompiler> = {}\n\n function perEnvServerFnPlugin(environment: {\n name: string\n type: 'client' | 'server'\n }): PluginOption {\n // in server environments, we don't transform middleware calls\n const transformCodeFilter =\n environment.type === 'client'\n ? [/\\.\\s*handler\\(/, /\\.\\s*createMiddleware\\(\\)/]\n : [/\\.\\s*handler\\(/]\n\n return {\n name: `tanstack-start-core::server-fn:${environment.name}`,\n enforce: 'pre',\n applyToEnvironment(env) {\n return env.name === environment.name\n },\n transform: {\n filter: {\n id: {\n exclude: new RegExp(`${SERVER_FN_LOOKUP}$`),\n include: TRANSFORM_ID_REGEX,\n },\n code: {\n include: transformCodeFilter,\n },\n },\n async handler(code, id) {\n let compiler = compilers[this.environment.name]\n if (!compiler) {\n compiler = new ServerFnCompiler({\n env: environment.type,\n directive: opts.directive,\n lookupKinds: LookupKindsPerEnv[environment.type],\n lookupConfigurations: getLookupConfigurationsForEnv(\n environment.type,\n opts.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] = 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 = compilers[this.environment.name]\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 return [\n ...opts.environments.map(perEnvServerFnPlugin),\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 !!opts.environments.find((e) => e.name === env.name)\n },\n transform: {\n filter: {\n id: new RegExp(`${SERVER_FN_LOOKUP}$`),\n },\n handler(code, id) {\n const compiler = compilers[this.environment.name]\n compiler?.ingestModule({ code, id: cleanId(id) })\n },\n },\n },\n ]\n}\n"],"names":["id"],"mappings":";;AAMA,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,IAAI,CAAC,UAAU,CAAU;AACvC;AAEA,MAAM,gCAAgC,CACpC,KACA,cACwB;AACxB,QAAM,uBAAqC;AAAA,IACzC,SAAS,aAAa,SAAS;AAAA,IAC/B,YAAY;AAAA,EAAA;AAEd,MAAI,QAAQ,UAAU;AACpB,WAAO;AAAA,MACL;AAAA,QACE,SAAS,aAAa,SAAS;AAAA,QAC/B,YAAY;AAAA,MAAA;AAAA,MAEd;AAAA,QACE,SAAS,aAAa,SAAS;AAAA,QAC/B,YAAY;AAAA,MAAA;AAAA,MAGd;AAAA,IAAA;AAAA,EAEJ,OAAO;AACL,WAAO,CAAC,oBAAoB;AAAA,EAC9B;AACF;AACA,MAAM,mBAAmB;AAClB,SAAS,qBAAqB,MAIpB;AACf,QAAM,YAA4D,CAAA;AAElE,WAAS,qBAAqB,aAGb;AAEf,UAAM,sBACJ,YAAY,SAAS,WACjB,CAAC,kBAAkB,2BAA2B,IAC9C,CAAC,gBAAgB;AAEvB,WAAO;AAAA,MACL,MAAM,kCAAkC,YAAY,IAAI;AAAA,MACxD,SAAS;AAAA,MACT,mBAAmB,KAAK;AACtB,eAAO,IAAI,SAAS,YAAY;AAAA,MAClC;AAAA,MACA,WAAW;AAAA,QACT,QAAQ;AAAA,UACN,IAAI;AAAA,YACF,SAAS,IAAI,OAAO,GAAG,gBAAgB,GAAG;AAAA,YAC1C,SAAS;AAAA,UAAA;AAAA,UAEX,M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A,QACT;AAAA,MAAA;AAAA,MAGF,UAAU,KAAK;AACb,cAAM,WAAW,UAAU,KAAK,YAAY,IAAI;AAEhD,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,EAEJ;AAEA,SAAO;AAAA,IACL,GAAG,KAAK,aAAa,IAAI,oBAAoB;AAAA,IAC7C;AAAA,MACE,MAAM;AAAA;AAAA,MAEN,OAAO;AAAA,MACP,mBAAmB,KAAK;AACtB,eAAO,CAAC,CAAC,KAAK,aAAa,KAAK,CAAC,MAAM,EAAE,SAAS,IAAI,IAAI;AAAA,MAC5D;AAAA,MACA,WAAW;AAAA,QACT,QAAQ;AAAA,UACN,IAAI,IAAI,OAAO,GAAG,gBAAgB,GAAG;AAAA,QAAA;AAAA,QAEvC,QAAQ,MAAM,IAAI;AAChB,gBAAM,WAAW,UAAU,KAAK,YAAY,IAAI;AAChD,oBAAU,aAAa,EAAE,MAAM,IAAI,QAAQ,EAAE,GAAG;AAAA,QAClD;AAAA,MAAA;AAAA,IACF;AAAA,EACF;AAEJ;"}
@@ -13,6 +13,7 @@ export interface TanStackStartVitePluginCoreOptions {
13
13
  ssr?: {
14
14
  getServerFnById?: string;
15
15
  };
16
+ providerEnv?: string;
16
17
  };
17
18
  }
18
19
  export interface ResolvedStartConfig {
@@ -7,7 +7,7 @@ import { join } from "pathe";
7
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
- import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from "./constants.js";
10
+ import { VITE_ENVIRONMENT_NAMES, ENTRY_POINTS } from "./constants.js";
11
11
  import { tanStackStartRouter } from "./start-router-plugin/plugin.js";
12
12
  import { loadEnvPlugin } from "./load-env-plugin/plugin.js";
13
13
  import { devServerPlugin } from "./dev-server-plugin/plugin.js";
@@ -55,6 +55,16 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
55
55
  }
56
56
  return bundle;
57
57
  }
58
+ const environments = [
59
+ { name: VITE_ENVIRONMENT_NAMES.client, type: "client" },
60
+ { name: VITE_ENVIRONMENT_NAMES.server, type: "server" }
61
+ ];
62
+ if (corePluginOpts.serverFn?.providerEnv && !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)) {
63
+ environments.push({
64
+ name: corePluginOpts.serverFn.providerEnv,
65
+ type: "server"
66
+ });
67
+ }
58
68
  return [
59
69
  {
60
70
  name: "tanstack-start-core:config",
@@ -254,8 +264,12 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
254
264
  },
255
265
  tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),
256
266
  // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPlugin
257
- startCompilerPlugin(corePluginOpts.framework),
258
- createServerFnPlugin({ framework: corePluginOpts.framework, directive }),
267
+ startCompilerPlugin({ framework: corePluginOpts.framework, environments }),
268
+ createServerFnPlugin({
269
+ framework: corePluginOpts.framework,
270
+ directive,
271
+ environments
272
+ }),
259
273
  TanStackServerFnPlugin({
260
274
  // This is the ID that will be available to look up and import
261
275
  // our server function manifest and resolve its module
@@ -280,7 +294,7 @@ function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
280
294
  provider: {
281
295
  getRuntimeCode: () => `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server-rpc'`,
282
296
  replacer: (d) => `createServerRpc('${d.functionId}', ${d.fn})`,
283
- envName: VITE_ENVIRONMENT_NAMES.server
297
+ envName: corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server
284
298
  }
285
299
  }),
286
300
  loadEnvPlugin(),
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import { joinPaths } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPlugin } 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 serverFn?: {\n directive?: string\n ssr?: {\n getServerFnById?: string\n }\n }\n}\n\nexport interface ResolvedStartConfig {\n root: string\n startFilePath: string | undefined\n routerFilePath: string\n srcDirectory: string\n viteAppBase: string\n}\n\nexport type GetConfigFn = () => {\n startConfig: TanStackStartOutputConfig\n resolvedStartConfig: ResolvedStartConfig\n}\n\nfunction isFullUrl(str: string): boolean {\n try {\n new URL(str)\n return true\n } catch {\n return false\n }\n}\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 viteAppBase: '',\n }\n\n const directive = corePluginOpts.serverFn?.directive ?? 'use server'\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 resolvedStartConfig.viteAppBase = viteConfig.base ?? '/'\n if (!isFullUrl(resolvedStartConfig.viteAppBase)) {\n resolvedStartConfig.viteAppBase = joinPaths([\n '/',\n viteConfig.base,\n '/',\n ])\n }\n const root = viteConfig.root || process.cwd()\n resolvedStartConfig.root = root\n\n const { startConfig } = getConfig()\n if (startConfig.router.basepath === undefined) {\n if (!isFullUrl(resolvedStartConfig.viteAppBase)) {\n startConfig.router.basepath =\n resolvedStartConfig.viteAppBase.replace(/^\\/|\\/$/g, '')\n } else {\n startConfig.router.basepath = '/'\n }\n } else {\n if (command === 'serve' && !viteConfig.server?.middlewareMode) {\n // when serving, we must ensure that router basepath and viteAppBase are aligned\n if (\n !joinPaths(['/', startConfig.router.basepath, '/']).startsWith(\n joinPaths(['/', resolvedStartConfig.viteAppBase, '/']),\n )\n ) {\n this.error(\n '[tanstack-start]: During `vite dev`, `router.basepath` must start with the vite `base` config value',\n )\n }\n }\n }\n\n const TSS_SERVER_FN_BASE = joinPaths([\n '/',\n startConfig.router.basepath,\n startConfig.serverFns.base,\n '/',\n ])\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 if (\n startPackageName in peerDependencies ||\n '@tanstack/start-client-core' in peerDependencies\n ) {\n return true\n }\n }\n\n return false\n },\n })\n\n return {\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 exclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,\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', TSS_SERVER_FN_BASE),\n ...defineReplaceEnv('TSS_CLIENT_OUTPUT_DIR', getClientOutputDirectory(viteConfig)),\n ...defineReplaceEnv('TSS_ROUTER_BASEPATH', startConfig.router.basepath),\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 TanStackServerFnPlugin\n startCompilerPlugin(corePluginOpts.framework),\n createServerFnPlugin({ framework: corePluginOpts.framework, directive }),\n\n TanStackServerFnPlugin({\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 directive,\n generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,\n callers: [\n {\n envConsumer: 'client',\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${corePluginOpts.framework}-start/client-rpc'`,\n replacer: (d) => `createClientRpc('${d.functionId}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n {\n envConsumer: 'server',\n getRuntimeCode: () =>\n `import { createSsrRpc } from '@tanstack/${corePluginOpts.framework}-start/ssr-rpc'`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n replacer: (d) => `createSsrRpc('${d.functionId}')`,\n getServerFnById: corePluginOpts.serverFn?.ssr?.getServerFnById,\n },\n ],\n provider: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server-rpc'`,\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 getConfig,\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":";;;;;;;;;;;;;;;;;;AAyDA,SAAS,UAAU,KAAsB;AACvC,MAAI;AACF,QAAI,IAAI,GAAG;AACX,WA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B;AAAA,MAClB,iBAAiB,MAAM,UAAU,uBAAuB,MAAM;AAAA,MAC9D;AAAA,IAAA,CACD;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 { joinPaths } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPlugin } 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 serverFn?: {\n directive?: string\n ssr?: {\n getServerFnById?: string\n }\n providerEnv?: string\n }\n}\n\nexport interface ResolvedStartConfig {\n root: string\n startFilePath: string | undefined\n routerFilePath: string\n srcDirectory: string\n viteAppBase: string\n}\n\nexport type GetConfigFn = () => {\n startConfig: TanStackStartOutputConfig\n resolvedStartConfig: ResolvedStartConfig\n}\n\nfunction isFullUrl(str: string): boolean {\n try {\n new URL(str)\n return true\n } catch {\n return false\n }\n}\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 viteAppBase: '',\n }\n\n const directive = corePluginOpts.serverFn?.directive ?? 'use server'\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 const environments: Array<{ name: string; type: 'client' | 'server' }> = [\n { name: VITE_ENVIRONMENT_NAMES.client, type: 'client' },\n { name: VITE_ENVIRONMENT_NAMES.server, type: 'server' },\n ]\n if (\n corePluginOpts.serverFn?.providerEnv &&\n !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)\n ) {\n environments.push({\n name: corePluginOpts.serverFn.providerEnv,\n type: 'server',\n })\n }\n return [\n {\n name: 'tanstack-start-core:config',\n enforce: 'pre',\n async config(viteConfig, { command }) {\n resolvedStartConfig.viteAppBase = viteConfig.base ?? '/'\n if (!isFullUrl(resolvedStartConfig.viteAppBase)) {\n resolvedStartConfig.viteAppBase = joinPaths([\n '/',\n viteConfig.base,\n '/',\n ])\n }\n const root = viteConfig.root || process.cwd()\n resolvedStartConfig.root = root\n\n const { startConfig } = getConfig()\n if (startConfig.router.basepath === undefined) {\n if (!isFullUrl(resolvedStartConfig.viteAppBase)) {\n startConfig.router.basepath =\n resolvedStartConfig.viteAppBase.replace(/^\\/|\\/$/g, '')\n } else {\n startConfig.router.basepath = '/'\n }\n } else {\n if (command === 'serve' && !viteConfig.server?.middlewareMode) {\n // when serving, we must ensure that router basepath and viteAppBase are aligned\n if (\n !joinPaths(['/', startConfig.router.basepath, '/']).startsWith(\n joinPaths(['/', resolvedStartConfig.viteAppBase, '/']),\n )\n ) {\n this.error(\n '[tanstack-start]: During `vite dev`, `router.basepath` must start with the vite `base` config value',\n )\n }\n }\n }\n\n const TSS_SERVER_FN_BASE = joinPaths([\n '/',\n startConfig.router.basepath,\n startConfig.serverFns.base,\n '/',\n ])\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 if (\n startPackageName in peerDependencies ||\n '@tanstack/start-client-core' in peerDependencies\n ) {\n return true\n }\n }\n\n return false\n },\n })\n\n return {\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 exclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,\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', TSS_SERVER_FN_BASE),\n ...defineReplaceEnv('TSS_CLIENT_OUTPUT_DIR', getClientOutputDirectory(viteConfig)),\n ...defineReplaceEnv('TSS_ROUTER_BASEPATH', startConfig.router.basepath),\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 TanStackServerFnPlugin\n startCompilerPlugin({ framework: corePluginOpts.framework, environments }),\n createServerFnPlugin({\n framework: corePluginOpts.framework,\n directive,\n environments,\n }),\n\n TanStackServerFnPlugin({\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 directive,\n generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,\n callers: [\n {\n envConsumer: 'client',\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${corePluginOpts.framework}-start/client-rpc'`,\n replacer: (d) => `createClientRpc('${d.functionId}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n {\n envConsumer: 'server',\n getRuntimeCode: () =>\n `import { createSsrRpc } from '@tanstack/${corePluginOpts.framework}-start/ssr-rpc'`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n replacer: (d) => `createSsrRpc('${d.functionId}')`,\n getServerFnById: corePluginOpts.serverFn?.ssr?.getServerFnById,\n },\n ],\n provider: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server-rpc'`,\n replacer: (d) => `createServerRpc('${d.functionId}', ${d.fn})`,\n envName:\n corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server,\n },\n }),\n loadEnvPlugin(),\n startManifestPlugin({\n getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),\n getConfig,\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":";;;;;;;;;;;;;;;;;;AA0DA,SAAS,UAAU,KAAsB;AACvC,MAAI;AACF,QAAI,IAAI,GAAG;AACX,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEO,SAAS,4BACd,gBACA,iBACqB;AACrB,QAAM,sBAA2C;AAAA,IAC/C,MAAM;AAAA,IACN,eAAe;AAAA,IACf,gBAAgB;AAAA,IAChB,cAAc;AAAA,IACd,aAAa;AAAA,EAAA;AAGf,QAAM,YAAY,eAAe,UAAU,aAAa;AAExD,MAAI;AACJ,QAAM,YAAyB,MAAM;AACnC,QAAI,CAAC,oBAAoB,MAAM;AAC7B,YAAM,IAAI,MAAM,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,MAAA;AAAA,IACnE,CACD;AAAA,IACD,cAAA;AAAA,IACA,oBAAoB;AAAA,MAClB,iBAAiB,MAAM,UAAU,uBAAuB,MAAM;AAAA,MAC9D;AAAA,IAAA,CACD;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,6 +1,12 @@
1
- import { Plugin } from 'vite';
1
+ import { PluginOption } from 'vite';
2
2
  import { CompileStartFrameworkOptions } from './compilers.js';
3
3
  export type TanStackStartViteOptions = {
4
4
  globalMiddlewareEntry: string;
5
5
  };
6
- export declare function startCompilerPlugin(framework: CompileStartFrameworkOptions): Plugin;
6
+ export declare function startCompilerPlugin(opts: {
7
+ framework: CompileStartFrameworkOptions;
8
+ environments: Array<{
9
+ name: string;
10
+ type: 'client' | 'server';
11
+ }>;
12
+ }): PluginOption;
@@ -5,7 +5,7 @@ import { VIRTUAL_MODULES } from "@tanstack/start-server-core";
5
5
  import { normalizePath } from "vite";
6
6
  import path from "pathe";
7
7
  import { makeIdFiltersToMatchWithQuery } from "@rolldown/pluginutils";
8
- import { TRANSFORM_ID_REGEX, VITE_ENVIRONMENT_NAMES } from "../constants.js";
8
+ import { TRANSFORM_ID_REGEX } from "../constants.js";
9
9
  import { compileStartOutputFactory } from "./compilers.js";
10
10
  import { transformFuncs } from "./constants.js";
11
11
  const debug = process.env.TSR_VITE_DEBUG && ["true", "start-plugin"].includes(process.env.TSR_VITE_DEBUG);
@@ -20,71 +20,67 @@ function resolvePackage(packageName) {
20
20
  const pkgRoot = path.dirname(require2.resolve(packageName + "/package.json"));
21
21
  return pkgRoot;
22
22
  }
23
- function startCompilerPlugin(framework) {
24
- const compileStartOutput = compileStartOutputFactory(framework);
25
- return {
26
- name: "tanstack-start-core:compiler",
27
- enforce: "pre",
28
- applyToEnvironment(env) {
29
- return [
30
- VITE_ENVIRONMENT_NAMES.client,
31
- VITE_ENVIRONMENT_NAMES.server
32
- ].includes(env.name);
33
- },
34
- transform: {
35
- filter: {
36
- code: tokenRegex,
37
- id: {
38
- include: TRANSFORM_ID_REGEX,
39
- exclude: [
40
- VIRTUAL_MODULES.serverFnManifest,
41
- // N.B. the following files either just re-export or provide the runtime implementation of those functions
42
- // we do not want to include them in the transformation
43
- // however, those packages (especially start-client-core ATM) also USE these functions
44
- // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed
45
- ...makeIdFiltersToMatchWithQuery([
46
- ...resolveRuntimeFiles({
47
- package: "@tanstack/start-client-core",
48
- files: [
49
- "index.js",
50
- "createIsomorphicFn.js",
51
- "envOnly.js",
52
- "serverFnFetcher.js",
53
- "createStart.js",
54
- "createMiddleware.js"
55
- ]
56
- }),
57
- ...resolveRuntimeFiles({
58
- package: "@tanstack/start-server-core",
59
- files: ["index.js", "server-functions-handler.js"]
60
- })
61
- ])
23
+ const transformFilter = {
24
+ code: tokenRegex,
25
+ id: {
26
+ include: TRANSFORM_ID_REGEX,
27
+ exclude: [
28
+ VIRTUAL_MODULES.serverFnManifest,
29
+ // N.B. the following files either just re-export or provide the runtime implementation of those functions
30
+ // we do not want to include them in the transformation
31
+ // however, those packages (especially start-client-core ATM) also USE these functions
32
+ // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed
33
+ ...makeIdFiltersToMatchWithQuery([
34
+ ...resolveRuntimeFiles({
35
+ package: "@tanstack/start-client-core",
36
+ files: [
37
+ "index.js",
38
+ "createIsomorphicFn.js",
39
+ "envOnly.js",
40
+ "serverFnFetcher.js",
41
+ "createStart.js",
42
+ "createMiddleware.js"
62
43
  ]
63
- }
44
+ }),
45
+ ...resolveRuntimeFiles({
46
+ package: "@tanstack/start-server-core",
47
+ files: ["index.js", "server-functions-handler.js"]
48
+ })
49
+ ])
50
+ ]
51
+ }
52
+ };
53
+ function startCompilerPlugin(opts) {
54
+ const compileStartOutput = compileStartOutputFactory(opts.framework);
55
+ function perEnvCompilerPlugin(environment) {
56
+ return {
57
+ name: `tanstack-start-core:compiler:${environment.name}`,
58
+ enforce: "pre",
59
+ applyToEnvironment(env) {
60
+ return env.name === environment.name;
64
61
  },
65
- handler(code, id) {
66
- const env = this.environment.name === VITE_ENVIRONMENT_NAMES.client ? "client" : this.environment.name === VITE_ENVIRONMENT_NAMES.server ? "server" : (() => {
67
- throw new Error(
68
- `Environment ${this.environment.name} not configured`
69
- );
70
- })();
71
- const url = pathToFileURL(id);
72
- url.searchParams.delete("v");
73
- id = fileURLToPath(url).replace(/\\/g, "/");
74
- if (debug) console.info(`${env} Compiling Start: `, id);
75
- const compiled = compileStartOutput({
76
- code,
77
- filename: id,
78
- env
79
- });
80
- if (debug) {
81
- logDiff(code, compiled.code);
82
- console.log("Output:\n", compiled.code + "\n\n");
62
+ transform: {
63
+ filter: transformFilter,
64
+ handler(code, id) {
65
+ const url = pathToFileURL(id);
66
+ url.searchParams.delete("v");
67
+ id = fileURLToPath(url).replace(/\\/g, "/");
68
+ if (debug) console.info(`${environment.name} Compiling Start: `, id);
69
+ const compiled = compileStartOutput({
70
+ code,
71
+ filename: id,
72
+ env: environment.type
73
+ });
74
+ if (debug) {
75
+ logDiff(code, compiled.code);
76
+ console.log("Output:\n", compiled.code + "\n\n");
77
+ }
78
+ return compiled;
83
79
  }
84
- return compiled;
85
80
  }
86
- }
87
- };
81
+ };
82
+ }
83
+ return opts.environments.map(perEnvCompilerPlugin);
88
84
  }
89
85
  export {
90
86
  startCompilerPlugin
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["../../../src/start-compiler-plugin/plugin.ts"],"sourcesContent":["import { fileURLToPath, pathToFileURL } from 'node:url'\nimport { createRequire } from 'node:module'\nimport { logDiff } from '@tanstack/router-utils'\n\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { normalizePath } from 'vite'\nimport path from 'pathe'\nimport { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'\nimport { TRANSFORM_ID_REGEX, VITE_ENVIRONMENT_NAMES } from '../constants'\nimport { compileStartOutputFactory } from './compilers'\nimport { transformFuncs } from './constants'\nimport type { ViteEnvironmentNames } from '../constants'\nimport type { Plugin } from 'vite'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nconst debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin'].includes(process.env.TSR_VITE_DEBUG)\n\nexport type TanStackStartViteOptions = {\n globalMiddlewareEntry: string\n}\n\nconst tokenRegex = new RegExp(transformFuncs.join('|'))\n\nconst require = createRequire(import.meta.url)\n\nfunction resolveRuntimeFiles(opts: { package: string; files: Array<string> }) {\n const pkgRoot = resolvePackage(opts.package)\n const basePath = path.join(pkgRoot, 'dist', 'esm')\n return opts.files.map((file) => normalizePath(path.join(basePath, file)))\n}\n\nfunction resolvePackage(packageName: string): string {\n const pkgRoot = path.dirname(require.resolve(packageName + '/package.json'))\n return pkgRoot\n}\n\nexport function startCompilerPlugin(\n framework: CompileStartFrameworkOptions,\n): Plugin {\n const compileStartOutput = compileStartOutputFactory(framework)\n\n return {\n name: 'tanstack-start-core:compiler',\n enforce: 'pre',\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 code: tokenRegex,\n id: {\n include: TRANSFORM_ID_REGEX,\n exclude: [\n VIRTUAL_MODULES.serverFnManifest,\n // N.B. the following files either just re-export or provide the runtime implementation of those functions\n // we do not want to include them in the transformation\n // however, those packages (especially start-client-core ATM) also USE these functions\n // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed\n ...makeIdFiltersToMatchWithQuery([\n ...resolveRuntimeFiles({\n package: '@tanstack/start-client-core',\n files: [\n 'index.js',\n 'createIsomorphicFn.js',\n 'envOnly.js',\n 'serverFnFetcher.js',\n 'createStart.js',\n 'createMiddleware.js',\n ],\n }),\n ...resolveRuntimeFiles({\n package: '@tanstack/start-server-core',\n files: ['index.js', 'server-functions-handler.js'],\n }),\n ]),\n ],\n },\n },\n handler(code, id) {\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 const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (debug) console.info(`${env} Compiling Start: `, id)\n\n const compiled = compileStartOutput({\n code,\n filename: id,\n env,\n })\n\n if (debug) {\n logDiff(code, compiled.code)\n console.log('Output:\\n', compiled.code + '\\n\\n')\n }\n\n return compiled\n },\n },\n }\n}\n"],"names":["require"],"mappings":";;;;;;;;;;AAeA,MAAM,QACJ,QAAQ,IAAI,kBACZ,CAAC,QAAQ,cAAc,EAAE,SAAS,QAAQ,IAAI,cAAc;AAM9D,MAAM,aAAa,IAAI,OAAO,eAAe,KAAK,GAAG,CAAC;AAEtD,MAAMA,WAAU,cAAc,YAAY,GAAG;AAE7C,SAAS,oBAAoB,MAAiD;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AM,MAAM,cAAc,EAAE;AAC5B,YAAI,aAAa,OAAO,GAAG;AAC3B,aAAK,cAAc,GAAG,EAAE,QAAQ,OAAO,GAAG;AAE1C,YAAI,MAAO,SAAQ,KAAK,GAAG,GAAG,sBAAsB,EAAE;AAEtD,cAAM,WAAW,mBAAmB;AAAA,UAClC;AAAA,UACA,UAAU;AAAA,UACV;AAAA,QAAA,CACD;AAED,YAAI,OAAO;AACT,kBAAQ,MAAM,SAAS,IAAI;AAC3B,kBAAQ,IAAI,aAAa,SAAS,OAAO,MAAM;AAAA,QACjD;AAEA,eAAO;AAAA,MACT;AAAA,IAAA;AAAA,EACF;AAEJ;"}
1
+ {"version":3,"file":"plugin.js","sources":["../../../src/start-compiler-plugin/plugin.ts"],"sourcesContent":["import { fileURLToPath, pathToFileURL } from 'node:url'\nimport { createRequire } from 'node:module'\nimport { logDiff } from '@tanstack/router-utils'\n\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { normalizePath } from 'vite'\nimport path from 'pathe'\nimport { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'\nimport { TRANSFORM_ID_REGEX } from '../constants'\nimport { compileStartOutputFactory } from './compilers'\nimport { transformFuncs } from './constants'\nimport type { Plugin, PluginOption } from 'vite'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nconst debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin'].includes(process.env.TSR_VITE_DEBUG)\n\nexport type TanStackStartViteOptions = {\n globalMiddlewareEntry: string\n}\n\nconst tokenRegex = new RegExp(transformFuncs.join('|'))\n\nconst require = createRequire(import.meta.url)\n\nfunction resolveRuntimeFiles(opts: { package: string; files: Array<string> }) {\n const pkgRoot = resolvePackage(opts.package)\n const basePath = path.join(pkgRoot, 'dist', 'esm')\n return opts.files.map((file) => normalizePath(path.join(basePath, file)))\n}\n\nfunction resolvePackage(packageName: string): string {\n const pkgRoot = path.dirname(require.resolve(packageName + '/package.json'))\n return pkgRoot\n}\n\nconst transformFilter = {\n code: tokenRegex,\n id: {\n include: TRANSFORM_ID_REGEX,\n exclude: [\n VIRTUAL_MODULES.serverFnManifest,\n // N.B. the following files either just re-export or provide the runtime implementation of those functions\n // we do not want to include them in the transformation\n // however, those packages (especially start-client-core ATM) also USE these functions\n // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed\n ...makeIdFiltersToMatchWithQuery([\n ...resolveRuntimeFiles({\n package: '@tanstack/start-client-core',\n files: [\n 'index.js',\n 'createIsomorphicFn.js',\n 'envOnly.js',\n 'serverFnFetcher.js',\n 'createStart.js',\n 'createMiddleware.js',\n ],\n }),\n ...resolveRuntimeFiles({\n package: '@tanstack/start-server-core',\n files: ['index.js', 'server-functions-handler.js'],\n }),\n ]),\n ],\n },\n}\n\nexport function startCompilerPlugin(opts: {\n framework: CompileStartFrameworkOptions\n environments: Array<{ name: string; type: 'client' | 'server' }>\n}): PluginOption {\n const compileStartOutput = compileStartOutputFactory(opts.framework)\n\n function perEnvCompilerPlugin(environment: {\n name: string\n type: 'client' | 'server'\n }): Plugin {\n return {\n name: `tanstack-start-core:compiler:${environment.name}`,\n enforce: 'pre',\n applyToEnvironment(env) {\n return env.name === environment.name\n },\n transform: {\n filter: transformFilter,\n handler(code, id) {\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (debug) console.info(`${environment.name} Compiling Start: `, id)\n\n const compiled = compileStartOutput({\n code,\n filename: id,\n env: environment.type,\n })\n\n if (debug) {\n logDiff(code, compiled.code)\n console.log('Output:\\n', compiled.code + '\\n\\n')\n }\n\n return compiled\n },\n },\n }\n }\n return opts.environments.map(perEnvCompilerPlugin)\n}\n"],"names":["require"],"mappings":";;;;;;;;;;AAcA,MAAM,QACJ,QAAQ,IAAI,kBACZ,CAAC,QAAQ,cAAc,EAAE,SAAS,QAAQ,IAAI,cAAc;AAM9D,MAAM,aAAa,IAAI,OAAO,eAAe,KAAK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AG;AAE1C,cAAI,MAAO,SAAQ,KAAK,GAAG,YAAY,IAAI,sBAAsB,EAAE;AAEnE,gBAAM,WAAW,mBAAmB;AAAA,YAClC;AAAA,YACA,UAAU;AAAA,YACV,KAAK,YAAY;AAAA,UAAA,CAClB;AAED,cAAI,OAAO;AACT,oBAAQ,MAAM,SAAS,IAAI;AAC3B,oBAAQ,IAAI,aAAa,SAAS,OAAO,MAAM;AAAA,UACjD;AAEA,iBAAO;AAAA,QACT;AAAA,MAAA;AAAA,IACF;AAAA,EAEJ;AACA,SAAO,KAAK,aAAa,IAAI,oBAAoB;AACnD;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/start-plugin-core",
3
- "version": "1.134.20",
3
+ "version": "1.135.1",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -59,13 +59,13 @@
59
59
  "vitefu": "^1.1.1",
60
60
  "xmlbuilder2": "^3.1.1",
61
61
  "zod": "^3.24.2",
62
- "@tanstack/router-core": "1.134.20",
63
- "@tanstack/router-plugin": "1.134.20",
64
62
  "@tanstack/router-generator": "1.134.20",
63
+ "@tanstack/router-core": "1.134.20",
64
+ "@tanstack/router-plugin": "1.135.0",
65
65
  "@tanstack/router-utils": "1.133.19",
66
66
  "@tanstack/server-functions-plugin": "1.134.5",
67
- "@tanstack/start-server-core": "1.134.20",
68
- "@tanstack/start-client-core": "1.134.20"
67
+ "@tanstack/start-client-core": "1.134.20",
68
+ "@tanstack/start-server-core": "1.134.20"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@types/babel__code-frame": "^7.0.6",
@@ -146,7 +146,7 @@ export function handleCreateServerFn(
146
146
  ),
147
147
  ),
148
148
  ],
149
- [t.directive(t.directiveLiteral('use server'))],
149
+ [t.directive(t.directiveLiteral(opts.directive))],
150
150
  ),
151
151
  ),
152
152
  )
@@ -1,8 +1,7 @@
1
- import { TRANSFORM_ID_REGEX, VITE_ENVIRONMENT_NAMES } from '../constants'
1
+ import { TRANSFORM_ID_REGEX } from '../constants'
2
2
  import { ServerFnCompiler } from './compiler'
3
3
  import type { LookupConfig, LookupKind } from './compiler'
4
4
  import type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'
5
- import type { ViteEnvironmentNames } from '../constants'
6
5
  import type { PluginOption } from 'vite'
7
6
 
8
7
  function cleanId(id: string): string {
@@ -39,44 +38,29 @@ const getLookupConfigurationsForEnv = (
39
38
  return [createServerFnConfig]
40
39
  }
41
40
  }
41
+ const SERVER_FN_LOOKUP = 'server-fn-module-lookup'
42
42
  export function createServerFnPlugin(opts: {
43
43
  framework: CompileStartFrameworkOptions
44
44
  directive: string
45
+ environments: Array<{ name: string; type: 'client' | 'server' }>
45
46
  }): PluginOption {
46
- const SERVER_FN_LOOKUP = 'server-fn-module-lookup'
47
+ const compilers: Record<string /* envName */, ServerFnCompiler> = {}
47
48
 
48
- const compilers: Partial<Record<ViteEnvironmentNames, ServerFnCompiler>> = {}
49
- return [
50
- {
51
- name: 'tanstack-start-core:capture-server-fn-module-lookup',
52
- // we only need this plugin in dev mode
53
- apply: 'serve',
54
- applyToEnvironment(env) {
55
- return [
56
- VITE_ENVIRONMENT_NAMES.client,
57
- VITE_ENVIRONMENT_NAMES.server,
58
- ].includes(env.name as ViteEnvironmentNames)
59
- },
60
- transform: {
61
- filter: {
62
- id: new RegExp(`${SERVER_FN_LOOKUP}$`),
63
- },
64
- handler(code, id) {
65
- const compiler =
66
- compilers[this.environment.name as ViteEnvironmentNames]
67
- compiler?.ingestModule({ code, id: cleanId(id) })
68
- },
69
- },
70
- },
71
- {
72
- name: 'tanstack-start-core::server-fn',
73
- enforce: 'pre',
49
+ function perEnvServerFnPlugin(environment: {
50
+ name: string
51
+ type: 'client' | 'server'
52
+ }): PluginOption {
53
+ // in server environments, we don't transform middleware calls
54
+ const transformCodeFilter =
55
+ environment.type === 'client'
56
+ ? [/\.\s*handler\(/, /\.\s*createMiddleware\(\)/]
57
+ : [/\.\s*handler\(/]
74
58
 
59
+ return {
60
+ name: `tanstack-start-core::server-fn:${environment.name}`,
61
+ enforce: 'pre',
75
62
  applyToEnvironment(env) {
76
- return [
77
- VITE_ENVIRONMENT_NAMES.client,
78
- VITE_ENVIRONMENT_NAMES.server,
79
- ].includes(env.name as ViteEnvironmentNames)
63
+ return env.name === environment.name
80
64
  },
81
65
  transform: {
82
66
  filter: {
@@ -85,32 +69,18 @@ export function createServerFnPlugin(opts: {
85
69
  include: TRANSFORM_ID_REGEX,
86
70
  },
87
71
  code: {
88
- // TODO apply this plugin with a different filter per environment so that .createMiddleware() calls are not scanned in server env
89
- // only scan files that mention `.handler(` | `.createMiddleware()`
90
- include: [/\.\s*handler\(/, /\.\s*createMiddleware\(\)/],
72
+ include: transformCodeFilter,
91
73
  },
92
74
  },
93
75
  async handler(code, id) {
94
- let compiler =
95
- compilers[this.environment.name as ViteEnvironmentNames]
76
+ let compiler = compilers[this.environment.name]
96
77
  if (!compiler) {
97
- const env =
98
- this.environment.name === VITE_ENVIRONMENT_NAMES.client
99
- ? 'client'
100
- : this.environment.name === VITE_ENVIRONMENT_NAMES.server
101
- ? 'server'
102
- : (() => {
103
- throw new Error(
104
- `Environment ${this.environment.name} not configured`,
105
- )
106
- })()
107
-
108
78
  compiler = new ServerFnCompiler({
109
- env,
79
+ env: environment.type,
110
80
  directive: opts.directive,
111
- lookupKinds: LookupKindsPerEnv[env],
81
+ lookupKinds: LookupKindsPerEnv[environment.type],
112
82
  lookupConfigurations: getLookupConfigurationsForEnv(
113
- env,
83
+ environment.type,
114
84
  opts.framework,
115
85
  ),
116
86
  loadModule: async (id: string) => {
@@ -147,7 +117,7 @@ export function createServerFnPlugin(opts: {
147
117
  return null
148
118
  },
149
119
  })
150
- compilers[this.environment.name as ViteEnvironmentNames] = compiler
120
+ compilers[this.environment.name] = compiler
151
121
  }
152
122
 
153
123
  id = cleanId(id)
@@ -157,8 +127,7 @@ export function createServerFnPlugin(opts: {
157
127
  },
158
128
 
159
129
  hotUpdate(ctx) {
160
- const compiler =
161
- compilers[this.environment.name as ViteEnvironmentNames]
130
+ const compiler = compilers[this.environment.name]
162
131
 
163
132
  ctx.modules.forEach((m) => {
164
133
  if (m.id) {
@@ -173,6 +142,27 @@ export function createServerFnPlugin(opts: {
173
142
  }
174
143
  })
175
144
  },
145
+ }
146
+ }
147
+
148
+ return [
149
+ ...opts.environments.map(perEnvServerFnPlugin),
150
+ {
151
+ name: 'tanstack-start-core:capture-server-fn-module-lookup',
152
+ // we only need this plugin in dev mode
153
+ apply: 'serve',
154
+ applyToEnvironment(env) {
155
+ return !!opts.environments.find((e) => e.name === env.name)
156
+ },
157
+ transform: {
158
+ filter: {
159
+ id: new RegExp(`${SERVER_FN_LOOKUP}$`),
160
+ },
161
+ handler(code, id) {
162
+ const compiler = compilers[this.environment.name]
163
+ compiler?.ingestModule({ code, id: cleanId(id) })
164
+ },
165
+ },
176
166
  },
177
167
  ]
178
168
  }
package/src/plugin.ts CHANGED
@@ -39,6 +39,7 @@ export interface TanStackStartVitePluginCoreOptions {
39
39
  ssr?: {
40
40
  getServerFnById?: string
41
41
  }
42
+ providerEnv?: string
42
43
  }
43
44
  }
44
45
 
@@ -105,6 +106,19 @@ export function TanStackStartVitePluginCore(
105
106
  return bundle
106
107
  }
107
108
 
109
+ const environments: Array<{ name: string; type: 'client' | 'server' }> = [
110
+ { name: VITE_ENVIRONMENT_NAMES.client, type: 'client' },
111
+ { name: VITE_ENVIRONMENT_NAMES.server, type: 'server' },
112
+ ]
113
+ if (
114
+ corePluginOpts.serverFn?.providerEnv &&
115
+ !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)
116
+ ) {
117
+ environments.push({
118
+ name: corePluginOpts.serverFn.providerEnv,
119
+ type: 'server',
120
+ })
121
+ }
108
122
  return [
109
123
  {
110
124
  name: 'tanstack-start-core:config',
@@ -341,8 +355,12 @@ export function TanStackStartVitePluginCore(
341
355
  },
342
356
  tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),
343
357
  // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPlugin
344
- startCompilerPlugin(corePluginOpts.framework),
345
- createServerFnPlugin({ framework: corePluginOpts.framework, directive }),
358
+ startCompilerPlugin({ framework: corePluginOpts.framework, environments }),
359
+ createServerFnPlugin({
360
+ framework: corePluginOpts.framework,
361
+ directive,
362
+ environments,
363
+ }),
346
364
 
347
365
  TanStackServerFnPlugin({
348
366
  // This is the ID that will be available to look up and import
@@ -371,7 +389,8 @@ export function TanStackStartVitePluginCore(
371
389
  getRuntimeCode: () =>
372
390
  `import { createServerRpc } from '@tanstack/${corePluginOpts.framework}-start/server-rpc'`,
373
391
  replacer: (d) => `createServerRpc('${d.functionId}', ${d.fn})`,
374
- envName: VITE_ENVIRONMENT_NAMES.server,
392
+ envName:
393
+ corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server,
375
394
  },
376
395
  }),
377
396
  loadEnvPlugin(),
@@ -6,11 +6,10 @@ import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
6
6
  import { normalizePath } from 'vite'
7
7
  import path from 'pathe'
8
8
  import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'
9
- import { TRANSFORM_ID_REGEX, VITE_ENVIRONMENT_NAMES } from '../constants'
9
+ import { TRANSFORM_ID_REGEX } from '../constants'
10
10
  import { compileStartOutputFactory } from './compilers'
11
11
  import { transformFuncs } from './constants'
12
- import type { ViteEnvironmentNames } from '../constants'
13
- import type { Plugin } from 'vite'
12
+ import type { Plugin, PluginOption } from 'vite'
14
13
  import type { CompileStartFrameworkOptions } from './compilers'
15
14
 
16
15
  const debug =
@@ -36,82 +35,77 @@ function resolvePackage(packageName: string): string {
36
35
  return pkgRoot
37
36
  }
38
37
 
39
- export function startCompilerPlugin(
40
- framework: CompileStartFrameworkOptions,
41
- ): Plugin {
42
- const compileStartOutput = compileStartOutputFactory(framework)
43
-
44
- return {
45
- name: 'tanstack-start-core:compiler',
46
- enforce: 'pre',
47
- applyToEnvironment(env) {
48
- return [
49
- VITE_ENVIRONMENT_NAMES.client,
50
- VITE_ENVIRONMENT_NAMES.server,
51
- ].includes(env.name as ViteEnvironmentNames)
52
- },
53
- transform: {
54
- filter: {
55
- code: tokenRegex,
56
- id: {
57
- include: TRANSFORM_ID_REGEX,
58
- exclude: [
59
- VIRTUAL_MODULES.serverFnManifest,
60
- // N.B. the following files either just re-export or provide the runtime implementation of those functions
61
- // we do not want to include them in the transformation
62
- // however, those packages (especially start-client-core ATM) also USE these functions
63
- // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed
64
- ...makeIdFiltersToMatchWithQuery([
65
- ...resolveRuntimeFiles({
66
- package: '@tanstack/start-client-core',
67
- files: [
68
- 'index.js',
69
- 'createIsomorphicFn.js',
70
- 'envOnly.js',
71
- 'serverFnFetcher.js',
72
- 'createStart.js',
73
- 'createMiddleware.js',
74
- ],
75
- }),
76
- ...resolveRuntimeFiles({
77
- package: '@tanstack/start-server-core',
78
- files: ['index.js', 'server-functions-handler.js'],
79
- }),
80
- ]),
38
+ const transformFilter = {
39
+ code: tokenRegex,
40
+ id: {
41
+ include: TRANSFORM_ID_REGEX,
42
+ exclude: [
43
+ VIRTUAL_MODULES.serverFnManifest,
44
+ // N.B. the following files either just re-export or provide the runtime implementation of those functions
45
+ // we do not want to include them in the transformation
46
+ // however, those packages (especially start-client-core ATM) also USE these functions
47
+ // (namely `createIsomorphicFn` in `packages/start-client-core/src/getRouterInstance.ts`) and thus need to be transformed
48
+ ...makeIdFiltersToMatchWithQuery([
49
+ ...resolveRuntimeFiles({
50
+ package: '@tanstack/start-client-core',
51
+ files: [
52
+ 'index.js',
53
+ 'createIsomorphicFn.js',
54
+ 'envOnly.js',
55
+ 'serverFnFetcher.js',
56
+ 'createStart.js',
57
+ 'createMiddleware.js',
81
58
  ],
82
- },
83
- },
84
- handler(code, id) {
85
- const env =
86
- this.environment.name === VITE_ENVIRONMENT_NAMES.client
87
- ? 'client'
88
- : this.environment.name === VITE_ENVIRONMENT_NAMES.server
89
- ? 'server'
90
- : (() => {
91
- throw new Error(
92
- `Environment ${this.environment.name} not configured`,
93
- )
94
- })()
59
+ }),
60
+ ...resolveRuntimeFiles({
61
+ package: '@tanstack/start-server-core',
62
+ files: ['index.js', 'server-functions-handler.js'],
63
+ }),
64
+ ]),
65
+ ],
66
+ },
67
+ }
95
68
 
96
- const url = pathToFileURL(id)
97
- url.searchParams.delete('v')
98
- id = fileURLToPath(url).replace(/\\/g, '/')
69
+ export function startCompilerPlugin(opts: {
70
+ framework: CompileStartFrameworkOptions
71
+ environments: Array<{ name: string; type: 'client' | 'server' }>
72
+ }): PluginOption {
73
+ const compileStartOutput = compileStartOutputFactory(opts.framework)
99
74
 
100
- if (debug) console.info(`${env} Compiling Start: `, id)
75
+ function perEnvCompilerPlugin(environment: {
76
+ name: string
77
+ type: 'client' | 'server'
78
+ }): Plugin {
79
+ return {
80
+ name: `tanstack-start-core:compiler:${environment.name}`,
81
+ enforce: 'pre',
82
+ applyToEnvironment(env) {
83
+ return env.name === environment.name
84
+ },
85
+ transform: {
86
+ filter: transformFilter,
87
+ handler(code, id) {
88
+ const url = pathToFileURL(id)
89
+ url.searchParams.delete('v')
90
+ id = fileURLToPath(url).replace(/\\/g, '/')
91
+
92
+ if (debug) console.info(`${environment.name} Compiling Start: `, id)
101
93
 
102
- const compiled = compileStartOutput({
103
- code,
104
- filename: id,
105
- env,
106
- })
94
+ const compiled = compileStartOutput({
95
+ code,
96
+ filename: id,
97
+ env: environment.type,
98
+ })
107
99
 
108
- if (debug) {
109
- logDiff(code, compiled.code)
110
- console.log('Output:\n', compiled.code + '\n\n')
111
- }
100
+ if (debug) {
101
+ logDiff(code, compiled.code)
102
+ console.log('Output:\n', compiled.code + '\n\n')
103
+ }
112
104
 
113
- return compiled
105
+ return compiled
106
+ },
114
107
  },
115
- },
108
+ }
116
109
  }
110
+ return opts.environments.map(perEnvCompilerPlugin)
117
111
  }