@tanstack/start-plugin-core 1.142.7 → 1.142.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/esm/create-server-fn-plugin/compiler.d.ts +8 -5
  2. package/dist/esm/create-server-fn-plugin/compiler.js +108 -43
  3. package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -1
  4. package/dist/esm/create-server-fn-plugin/handleCreateIsomorphicFn.d.ts +4 -0
  5. package/dist/esm/create-server-fn-plugin/handleCreateIsomorphicFn.js +31 -0
  6. package/dist/esm/create-server-fn-plugin/handleCreateIsomorphicFn.js.map +1 -0
  7. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js +1 -1
  8. package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -1
  9. package/dist/esm/create-server-fn-plugin/handleEnvOnly.d.ts +6 -0
  10. package/dist/esm/create-server-fn-plugin/handleEnvOnly.js +36 -0
  11. package/dist/esm/create-server-fn-plugin/handleEnvOnly.js.map +1 -0
  12. package/dist/esm/create-server-fn-plugin/plugin.d.ts +1 -1
  13. package/dist/esm/create-server-fn-plugin/plugin.js +50 -12
  14. package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -1
  15. package/dist/esm/create-server-fn-plugin/types.d.ts +0 -1
  16. package/dist/esm/{start-compiler-plugin → create-server-fn-plugin}/utils.d.ts +0 -3
  17. package/dist/esm/create-server-fn-plugin/utils.js +19 -0
  18. package/dist/esm/create-server-fn-plugin/utils.js.map +1 -0
  19. package/dist/esm/plugin.d.ts +2 -30
  20. package/dist/esm/plugin.js +2 -2
  21. package/dist/esm/plugin.js.map +1 -1
  22. package/dist/esm/schema.d.ts +288 -288
  23. package/dist/esm/schema.js.map +1 -1
  24. package/dist/esm/start-manifest-plugin/plugin.d.ts +1 -1
  25. package/dist/esm/start-manifest-plugin/plugin.js.map +1 -1
  26. package/dist/esm/start-router-plugin/plugin.d.ts +1 -1
  27. package/dist/esm/start-router-plugin/plugin.js.map +1 -1
  28. package/dist/esm/types.d.ts +30 -0
  29. package/package.json +6 -6
  30. package/src/create-server-fn-plugin/compiler.ts +164 -58
  31. package/src/create-server-fn-plugin/handleCreateIsomorphicFn.ts +46 -0
  32. package/src/create-server-fn-plugin/handleCreateServerFn.ts +1 -1
  33. package/src/create-server-fn-plugin/handleEnvOnly.ts +45 -0
  34. package/src/create-server-fn-plugin/plugin.ts +58 -14
  35. package/src/create-server-fn-plugin/types.ts +0 -8
  36. package/src/create-server-fn-plugin/utils.ts +24 -0
  37. package/src/plugin.ts +7 -34
  38. package/src/schema.ts +1 -1
  39. package/src/start-manifest-plugin/plugin.ts +1 -1
  40. package/src/start-router-plugin/plugin.ts +1 -1
  41. package/src/types.ts +34 -0
  42. package/dist/esm/start-compiler-plugin/compilers.d.ts +0 -15
  43. package/dist/esm/start-compiler-plugin/compilers.js +0 -114
  44. package/dist/esm/start-compiler-plugin/compilers.js.map +0 -1
  45. package/dist/esm/start-compiler-plugin/constants.d.ts +0 -1
  46. package/dist/esm/start-compiler-plugin/constants.js +0 -9
  47. package/dist/esm/start-compiler-plugin/constants.js.map +0 -1
  48. package/dist/esm/start-compiler-plugin/envOnly.d.ts +0 -5
  49. package/dist/esm/start-compiler-plugin/envOnly.js +0 -41
  50. package/dist/esm/start-compiler-plugin/envOnly.js.map +0 -1
  51. package/dist/esm/start-compiler-plugin/isomorphicFn.d.ts +0 -4
  52. package/dist/esm/start-compiler-plugin/isomorphicFn.js +0 -49
  53. package/dist/esm/start-compiler-plugin/isomorphicFn.js.map +0 -1
  54. package/dist/esm/start-compiler-plugin/plugin.d.ts +0 -12
  55. package/dist/esm/start-compiler-plugin/plugin.js +0 -88
  56. package/dist/esm/start-compiler-plugin/plugin.js.map +0 -1
  57. package/dist/esm/start-compiler-plugin/utils.js +0 -30
  58. package/dist/esm/start-compiler-plugin/utils.js.map +0 -1
  59. package/src/start-compiler-plugin/compilers.ts +0 -176
  60. package/src/start-compiler-plugin/constants.ts +0 -5
  61. package/src/start-compiler-plugin/envOnly.ts +0 -58
  62. package/src/start-compiler-plugin/isomorphicFn.ts +0 -78
  63. package/src/start-compiler-plugin/plugin.ts +0 -111
  64. package/src/start-compiler-plugin/utils.ts +0 -41
@@ -0,0 +1,45 @@
1
+ import * as t from '@babel/types'
2
+ import type { RewriteCandidate } from './types'
3
+ import type { LookupKind } from './compiler'
4
+
5
+ function capitalize(str: string) {
6
+ if (!str) return ''
7
+ return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
8
+ }
9
+
10
+ export function handleEnvOnlyFn(
11
+ candidate: RewriteCandidate,
12
+ opts: { env: 'client' | 'server'; kind: LookupKind },
13
+ ) {
14
+ const { path } = candidate
15
+ const targetEnv = opts.kind === 'ClientOnlyFn' ? 'client' : 'server'
16
+
17
+ if (opts.env === targetEnv) {
18
+ // Matching environment - extract the inner function
19
+ const innerFn = path.node.arguments[0]
20
+
21
+ if (!t.isExpression(innerFn)) {
22
+ throw new Error(
23
+ `create${capitalize(targetEnv)}OnlyFn() must be called with a function!`,
24
+ )
25
+ }
26
+
27
+ path.replaceWith(innerFn)
28
+ } else {
29
+ // Wrong environment - replace with a function that throws an error
30
+ path.replaceWith(
31
+ t.arrowFunctionExpression(
32
+ [],
33
+ t.blockStatement([
34
+ t.throwStatement(
35
+ t.newExpression(t.identifier('Error'), [
36
+ t.stringLiteral(
37
+ `create${capitalize(targetEnv)}OnlyFn() functions can only be called on the ${targetEnv}!`,
38
+ ),
39
+ ]),
40
+ ),
41
+ ]),
42
+ ),
43
+ )
44
+ }
45
+ }
@@ -1,41 +1,74 @@
1
1
  import { TRANSFORM_ID_REGEX } from '../constants'
2
2
  import { ServerFnCompiler } from './compiler'
3
+ import type { CompileStartFrameworkOptions } from '../types'
3
4
  import type { LookupConfig, LookupKind } from './compiler'
4
- import type { CompileStartFrameworkOptions } from '../start-compiler-plugin/compilers'
5
5
  import type { PluginOption } from 'vite'
6
6
 
7
7
  function cleanId(id: string): string {
8
- return id.split('?')[0]!
8
+ const queryIndex = id.indexOf('?')
9
+ return queryIndex === -1 ? id : id.substring(0, queryIndex)
9
10
  }
10
11
 
11
12
  const LookupKindsPerEnv: Record<'client' | 'server', Set<LookupKind>> = {
12
- client: new Set(['Middleware', 'ServerFn'] as const),
13
- server: new Set(['ServerFn'] as const),
13
+ client: new Set([
14
+ 'Middleware',
15
+ 'ServerFn',
16
+ 'IsomorphicFn',
17
+ 'ServerOnlyFn',
18
+ 'ClientOnlyFn',
19
+ ] as const),
20
+ server: new Set([
21
+ 'ServerFn',
22
+ 'IsomorphicFn',
23
+ 'ServerOnlyFn',
24
+ 'ClientOnlyFn',
25
+ ] as const),
14
26
  }
15
27
 
16
28
  const getLookupConfigurationsForEnv = (
17
29
  env: 'client' | 'server',
18
30
  framework: CompileStartFrameworkOptions,
19
31
  ): Array<LookupConfig> => {
20
- const createServerFnConfig: LookupConfig = {
21
- libName: `@tanstack/${framework}-start`,
22
- rootExport: 'createServerFn',
23
- }
32
+ // Common configs for all environments
33
+ const commonConfigs: Array<LookupConfig> = [
34
+ {
35
+ libName: `@tanstack/${framework}-start`,
36
+ rootExport: 'createServerFn',
37
+ kind: 'Root',
38
+ },
39
+ {
40
+ libName: `@tanstack/${framework}-start`,
41
+ rootExport: 'createIsomorphicFn',
42
+ kind: 'IsomorphicFn',
43
+ },
44
+ {
45
+ libName: `@tanstack/${framework}-start`,
46
+ rootExport: 'createServerOnlyFn',
47
+ kind: 'ServerOnlyFn',
48
+ },
49
+ {
50
+ libName: `@tanstack/${framework}-start`,
51
+ rootExport: 'createClientOnlyFn',
52
+ kind: 'ClientOnlyFn',
53
+ },
54
+ ]
55
+
24
56
  if (env === 'client') {
25
57
  return [
26
58
  {
27
59
  libName: `@tanstack/${framework}-start`,
28
60
  rootExport: 'createMiddleware',
61
+ kind: 'Root',
29
62
  },
30
63
  {
31
64
  libName: `@tanstack/${framework}-start`,
32
65
  rootExport: 'createStart',
66
+ kind: 'Root',
33
67
  },
34
-
35
- createServerFnConfig,
68
+ ...commonConfigs,
36
69
  ]
37
70
  } else {
38
- return [createServerFnConfig]
71
+ return commonConfigs
39
72
  }
40
73
  }
41
74
  const SERVER_FN_LOOKUP = 'server-fn-module-lookup'
@@ -44,6 +77,12 @@ function buildDirectiveSplitParam(directive: string) {
44
77
  return `tsr-directive-${directive.replace(/[^a-zA-Z0-9]/g, '-')}`
45
78
  }
46
79
 
80
+ const commonTransformCodeFilter = [
81
+ /\.\s*handler\(/,
82
+ /createIsomorphicFn/,
83
+ /createServerOnlyFn/,
84
+ /createClientOnlyFn/,
85
+ ]
47
86
  export function createServerFnPlugin(opts: {
48
87
  framework: CompileStartFrameworkOptions
49
88
  directive: string
@@ -56,11 +95,16 @@ export function createServerFnPlugin(opts: {
56
95
  name: string
57
96
  type: 'client' | 'server'
58
97
  }): PluginOption {
59
- // in server environments, we don't transform middleware calls
98
+ // Code filter patterns for transform functions:
99
+ // - `.handler(` for createServerFn
100
+ // - `createMiddleware(` for middleware (client only)
101
+ // - `createIsomorphicFn` for isomorphic functions
102
+ // - `createServerOnlyFn` for server-only functions
103
+ // - `createClientOnlyFn` for client-only functions
60
104
  const transformCodeFilter =
61
105
  environment.type === 'client'
62
- ? [/\.\s*handler\(/, /\.\s*createMiddleware\(\)/]
63
- : [/\.\s*handler\(/]
106
+ ? [...commonTransformCodeFilter, /createMiddleware\s*\(/]
107
+ : commonTransformCodeFilter
64
108
 
65
109
  return {
66
110
  name: `tanstack-start-core::server-fn:${environment.name}`,
@@ -25,14 +25,6 @@ export interface MethodChainPaths {
25
25
 
26
26
  export type MethodChainKey = keyof MethodChainPaths
27
27
 
28
- export const METHOD_CHAIN_KEYS: ReadonlyArray<MethodChainKey> = [
29
- 'middleware',
30
- 'inputValidator',
31
- 'handler',
32
- 'server',
33
- 'client',
34
- ] as const
35
-
36
28
  /**
37
29
  * Information about a candidate that needs to be rewritten.
38
30
  */
@@ -0,0 +1,24 @@
1
+ import { codeFrameColumns } from '@babel/code-frame'
2
+
3
+ export function codeFrameError(
4
+ code: string,
5
+ loc: {
6
+ start: { line: number; column: number }
7
+ end: { line: number; column: number }
8
+ },
9
+ message: string,
10
+ ) {
11
+ const frame = codeFrameColumns(
12
+ code,
13
+ {
14
+ start: loc.start,
15
+ end: loc.end,
16
+ },
17
+ {
18
+ highlightCode: true,
19
+ message,
20
+ },
21
+ )
22
+
23
+ return new Error(frame)
24
+ }
package/src/plugin.ts CHANGED
@@ -6,7 +6,6 @@ import { crawlFrameworkPkgs } from 'vitefu'
6
6
  import { join } from 'pathe'
7
7
  import { escapePath } from 'tinyglobby'
8
8
  import { startManifestPlugin } from './start-manifest-plugin/plugin'
9
- import { startCompilerPlugin } from './start-compiler-plugin/plugin'
10
9
  import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from './constants'
11
10
  import { tanStackStartRouter } from './start-router-plugin/plugin'
12
11
  import { loadEnvPlugin } from './load-env-plugin/plugin'
@@ -20,44 +19,17 @@ import {
20
19
  } from './output-directory'
21
20
  import { postServerBuild } from './post-server-build'
22
21
  import { createServerFnPlugin } from './create-server-fn-plugin/plugin'
22
+ import type {
23
+ GetConfigFn,
24
+ ResolvedStartConfig,
25
+ TanStackStartVitePluginCoreOptions,
26
+ } from './types'
23
27
  import type { ViteEnvironmentNames } from './constants'
24
28
  import type {
25
29
  TanStackStartInputConfig,
26
30
  TanStackStartOutputConfig,
27
31
  } from './schema'
28
32
  import type { PluginOption } from 'vite'
29
- import type { CompileStartFrameworkOptions } from './start-compiler-plugin/compilers'
30
-
31
- export interface TanStackStartVitePluginCoreOptions {
32
- framework: CompileStartFrameworkOptions
33
- defaultEntryPaths: {
34
- client: string
35
- server: string
36
- start: string
37
- }
38
- serverFn?: {
39
- directive?: string
40
- ssr?: {
41
- getServerFnById?: string
42
- }
43
- providerEnv?: string
44
- }
45
- }
46
-
47
- export interface ResolvedStartConfig {
48
- root: string
49
- startFilePath: string | undefined
50
- routerFilePath: string
51
- srcDirectory: string
52
- viteAppBase: string
53
- serverFnProviderEnv: string
54
- }
55
-
56
- export type GetConfigFn = () => {
57
- startConfig: TanStackStartOutputConfig
58
- resolvedStartConfig: ResolvedStartConfig
59
- corePluginOpts: TanStackStartVitePluginCoreOptions
60
- }
61
33
 
62
34
  function isFullUrl(str: string): boolean {
63
35
  try {
@@ -431,7 +403,8 @@ export function TanStackStartVitePluginCore(
431
403
  envName: serverFnProviderEnv,
432
404
  },
433
405
  }),
434
- startCompilerPlugin({ framework: corePluginOpts.framework, environments }),
406
+ // Note: startCompilerPlugin functionality (createIsomorphicFn, createServerOnlyFn, createClientOnlyFn)
407
+ // is now merged into createServerFnPlugin above
435
408
  loadEnvPlugin(),
436
409
  startManifestPlugin({
437
410
  getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),
package/src/schema.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from 'node:path'
2
2
  import { z } from 'zod'
3
3
  import { configSchema, getConfig } from '@tanstack/router-plugin'
4
- import type { TanStackStartVitePluginCoreOptions } from './plugin'
4
+ import type { TanStackStartVitePluginCoreOptions } from './types'
5
5
 
6
6
  const tsrConfig = configSchema
7
7
  .omit({ autoCodeSplitting: true, target: true, verboseFileRoutes: true })
@@ -4,7 +4,7 @@ import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
4
4
  import { tsrSplit } from '@tanstack/router-plugin'
5
5
  import { resolveViteId } from '../utils'
6
6
  import { ENTRY_POINTS } from '../constants'
7
- import type { GetConfigFn } from '../plugin'
7
+ import type { GetConfigFn } from '../types'
8
8
  import type { PluginOption, Rollup } from 'vite'
9
9
  import type { Manifest, RouterManagedTag } from '@tanstack/router-core'
10
10
 
@@ -10,6 +10,7 @@ import { routesManifestPlugin } from './generator-plugins/routes-manifest-plugin
10
10
  import { prerenderRoutesPlugin } from './generator-plugins/prerender-routes-plugin'
11
11
  import { pruneServerOnlySubtrees } from './pruneServerOnlySubtrees'
12
12
  import { SERVER_PROP } from './constants'
13
+ import type { GetConfigFn, TanStackStartVitePluginCoreOptions } from '../types'
13
14
  import type {
14
15
  Generator,
15
16
  GeneratorPlugin,
@@ -17,7 +18,6 @@ import type {
17
18
  } from '@tanstack/router-generator'
18
19
  import type { DevEnvironment, Plugin, PluginOption } from 'vite'
19
20
  import type { TanStackStartInputConfig } from '../schema'
20
- import type { GetConfigFn, TanStackStartVitePluginCoreOptions } from '../plugin'
21
21
 
22
22
  function isServerOnlyNode(node: RouteNode | undefined) {
23
23
  if (!node?.createFileRouteProps) {
package/src/types.ts ADDED
@@ -0,0 +1,34 @@
1
+ import type { TanStackStartOutputConfig } from './schema'
2
+
3
+ export type CompileStartFrameworkOptions = 'react' | 'solid' | 'vue'
4
+
5
+ export interface TanStackStartVitePluginCoreOptions {
6
+ framework: CompileStartFrameworkOptions
7
+ defaultEntryPaths: {
8
+ client: string
9
+ server: string
10
+ start: string
11
+ }
12
+ serverFn?: {
13
+ directive?: string
14
+ ssr?: {
15
+ getServerFnById?: string
16
+ }
17
+ providerEnv?: string
18
+ }
19
+ }
20
+
21
+ export interface ResolvedStartConfig {
22
+ root: string
23
+ startFilePath: string | undefined
24
+ routerFilePath: string
25
+ srcDirectory: string
26
+ viteAppBase: string
27
+ serverFnProviderEnv: string
28
+ }
29
+
30
+ export type GetConfigFn = () => {
31
+ startConfig: TanStackStartOutputConfig
32
+ resolvedStartConfig: ResolvedStartConfig
33
+ corePluginOpts: TanStackStartVitePluginCoreOptions
34
+ }
@@ -1,15 +0,0 @@
1
- import { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils';
2
- import * as babel from '@babel/core';
3
- import * as t from '@babel/types';
4
- export type CompileStartFrameworkOptions = 'react' | 'solid' | 'vue';
5
- export declare function compileStartOutputFactory(framework: CompileStartFrameworkOptions): (opts: CompileOptions) => GeneratorResult;
6
- export type CompileOptions = ParseAstOptions & {
7
- env: 'server' | 'client';
8
- dce?: boolean;
9
- filename: string;
10
- };
11
- export type IdentifierConfig = {
12
- name: string;
13
- handleCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
14
- paths: Array<babel.NodePath>;
15
- };
@@ -1,114 +0,0 @@
1
- import * as babel from "@babel/core";
2
- import * as t from "@babel/types";
3
- import { findReferencedIdentifiers, deadCodeElimination } from "babel-dead-code-elimination";
4
- import { parseAst, generateFromAst } from "@tanstack/router-utils";
5
- import { transformFuncs } from "./constants.js";
6
- import { handleCreateIsomorphicFnCallExpression } from "./isomorphicFn.js";
7
- import { handleCreateClientOnlyFnCallExpression, handleCreateServerOnlyFnCallExpression } from "./envOnly.js";
8
- function compileStartOutputFactory(framework) {
9
- return function compileStartOutput(opts) {
10
- const identifiers = {
11
- createServerOnlyFn: {
12
- name: "createServerOnlyFn",
13
- handleCallExpression: handleCreateServerOnlyFnCallExpression,
14
- paths: []
15
- },
16
- createClientOnlyFn: {
17
- name: "createClientOnlyFn",
18
- handleCallExpression: handleCreateClientOnlyFnCallExpression,
19
- paths: []
20
- },
21
- createIsomorphicFn: {
22
- name: "createIsomorphicFn",
23
- handleCallExpression: handleCreateIsomorphicFnCallExpression,
24
- paths: []
25
- }
26
- };
27
- const ast = parseAst(opts);
28
- const doDce = opts.dce ?? true;
29
- const refIdents = doDce ? findReferencedIdentifiers(ast) : void 0;
30
- const validImportSources = [
31
- `@tanstack/${framework}-start`,
32
- "@tanstack/start-client-core"
33
- ];
34
- babel.traverse(ast, {
35
- Program: {
36
- enter(programPath) {
37
- programPath.traverse({
38
- ImportDeclaration: (path) => {
39
- if (!validImportSources.includes(path.node.source.value)) {
40
- return;
41
- }
42
- path.node.specifiers.forEach((specifier) => {
43
- transformFuncs.forEach((identifierKey) => {
44
- const identifier = identifiers[identifierKey];
45
- if (!identifier) {
46
- return;
47
- }
48
- if (specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier") {
49
- if (specifier.imported.name === identifierKey) {
50
- identifier.name = specifier.local.name;
51
- }
52
- }
53
- if (specifier.type === "ImportNamespaceSpecifier") {
54
- identifier.name = `${specifier.local.name}.${identifierKey}`;
55
- }
56
- });
57
- });
58
- },
59
- CallExpression: (path) => {
60
- transformFuncs.forEach((identifierKey) => {
61
- const identifier = identifiers[identifierKey];
62
- if (!identifier) {
63
- return;
64
- }
65
- if (t.isIdentifier(path.node.callee) && path.node.callee.name === identifier.name) {
66
- if (path.scope.getBinding(identifier.name)?.path.node.type === "FunctionDeclaration") {
67
- return;
68
- }
69
- return identifier.paths.push(path);
70
- }
71
- if (t.isMemberExpression(path.node.callee)) {
72
- if (t.isIdentifier(path.node.callee.object) && t.isIdentifier(path.node.callee.property)) {
73
- const callname = [
74
- path.node.callee.object.name,
75
- path.node.callee.property.name
76
- ].join(".");
77
- if (callname === identifier.name) {
78
- identifier.paths.push(path);
79
- }
80
- }
81
- }
82
- return;
83
- });
84
- }
85
- });
86
- transformFuncs.forEach((identifierKey) => {
87
- const identifier = identifiers[identifierKey];
88
- if (!identifier) {
89
- return;
90
- }
91
- identifier.paths.forEach((path) => {
92
- identifier.handleCallExpression(
93
- path,
94
- opts
95
- );
96
- });
97
- });
98
- }
99
- }
100
- });
101
- if (doDce) {
102
- deadCodeElimination(ast, refIdents);
103
- }
104
- return generateFromAst(ast, {
105
- sourceMaps: true,
106
- sourceFileName: opts.filename,
107
- filename: opts.filename
108
- });
109
- };
110
- }
111
- export {
112
- compileStartOutputFactory
113
- };
114
- //# sourceMappingURL=compilers.js.map
@@ -1 +0,0 @@
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' | 'vue'\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: Partial<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 const validImportSources = [\n `@tanstack/${framework}-start`,\n '@tanstack/start-client-core',\n ]\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (!validImportSources.includes(path.node.source.value)) {\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 if (!identifier) {\n return\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 const identifier = identifiers[identifierKey]\n if (!identifier) {\n return\n }\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 === identifier.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(identifier.name)?.path.node.type ===\n 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifier.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 === identifier.name) {\n identifier.paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n if (!identifier) {\n return\n }\n identifier.paths.forEach((path) => {\n identifier.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":"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"}
@@ -1 +0,0 @@
1
- export declare const transformFuncs: readonly ["createServerOnlyFn", "createClientOnlyFn", "createIsomorphicFn"];
@@ -1,9 +0,0 @@
1
- const transformFuncs = [
2
- "createServerOnlyFn",
3
- "createClientOnlyFn",
4
- "createIsomorphicFn"
5
- ];
6
- export {
7
- transformFuncs
8
- };
9
- //# sourceMappingURL=constants.js.map
@@ -1 +0,0 @@
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,5 +0,0 @@
1
- import { CompileOptions } from './compilers.js';
2
- import * as t from '@babel/types';
3
- import type * as babel from '@babel/core';
4
- export declare const handleCreateServerOnlyFnCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
5
- export declare const handleCreateClientOnlyFnCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
@@ -1,41 +0,0 @@
1
- import * as t from "@babel/types";
2
- function capitalize(str) {
3
- if (!str) return "";
4
- return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
5
- }
6
- function buildEnvOnlyCallExpressionHandler(env) {
7
- return function envOnlyCallExpressionHandler(path, opts) {
8
- const isEnvMatch = env === "client" ? opts.env === "client" : opts.env === "server";
9
- if (isEnvMatch) {
10
- const innerInputExpression = path.node.arguments[0];
11
- if (!t.isExpression(innerInputExpression)) {
12
- throw new Error(
13
- `${env}Only() functions must be called with a function!`
14
- );
15
- }
16
- path.replaceWith(innerInputExpression);
17
- return;
18
- }
19
- path.replaceWith(
20
- t.arrowFunctionExpression(
21
- [],
22
- t.blockStatement([
23
- t.throwStatement(
24
- t.newExpression(t.identifier("Error"), [
25
- t.stringLiteral(
26
- `create${capitalize(env)}OnlyFn() functions can only be called on the ${env}!`
27
- )
28
- ])
29
- )
30
- ])
31
- )
32
- );
33
- };
34
- }
35
- const handleCreateServerOnlyFnCallExpression = buildEnvOnlyCallExpressionHandler("server");
36
- const handleCreateClientOnlyFnCallExpression = buildEnvOnlyCallExpressionHandler("client");
37
- export {
38
- handleCreateClientOnlyFnCallExpression,
39
- handleCreateServerOnlyFnCallExpression
40
- };
41
- //# sourceMappingURL=envOnly.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"envOnly.js","sources":["../../../src/start-compiler-plugin/envOnly.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nfunction capitalize(str: string) {\n if (!str) return ''\n return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()\n}\n\nfunction buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {\n return function envOnlyCallExpressionHandler(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n // if (debug)\n // console.info(`Handling ${env}Only call expression:`, path.toString())\n\n const isEnvMatch =\n env === 'client' ? opts.env === 'client' : opts.env === 'server'\n\n if (isEnvMatch) {\n // extract the inner function from the call expression\n const innerInputExpression = path.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `${env}Only() functions must be called with a function!`,\n )\n }\n\n path.replaceWith(innerInputExpression)\n return\n }\n\n // If we're on the wrong environment, replace the call expression\n // with a function that always throws an error.\n path.replaceWith(\n t.arrowFunctionExpression(\n [],\n t.blockStatement([\n t.throwStatement(\n t.newExpression(t.identifier('Error'), [\n t.stringLiteral(\n `create${capitalize(env)}OnlyFn() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\n\nexport const handleCreateServerOnlyFnCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleCreateClientOnlyFnCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\n"],"names":[],"mappings":";AAKA,SAAS,WAAW,KAAa;AAC/B,MAAI,CAAC,IAAK,QAAO;AACjB,SAAO,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC,EAAE,YAAA;AACpD;AAEA,SAAS,kCAAkC,KAA0B;AACnE,SAAO,SAAS,6BACd,MACA,MACA;AAIA,UAAM,aACJ,QAAQ,WAAW,KAAK,QAAQ,WAAW,KAAK,QAAQ;AAE1D,QAAI,YAAY;AAEd,YAAM,uBAAuB,KAAK,KAAK,UAAU,CAAC;AAElD,UAAI,CAAC,EAAE,aAAa,oBAAoB,GAAG;AACzC,cAAM,IAAI;AAAA,UACR,GAAG,GAAG;AAAA,QAAA;AAAA,MAEV;AAEA,WAAK,YAAY,oBAAoB;AACrC;AAAA,IACF;AAIA,SAAK;AAAA,MACH,EAAE;AAAA,QACA,CAAA;AAAA,QACA,EAAE,eAAe;AAAA,UACf,EAAE;AAAA,YACA,EAAE,cAAc,EAAE,WAAW,OAAO,GAAG;AAAA,cACrC,EAAE;AAAA,gBACA,SAAS,WAAW,GAAG,CAAC,gDAAgD,GAAG;AAAA,cAAA;AAAA,YAC7E,CACD;AAAA,UAAA;AAAA,QACH,CACD;AAAA,MAAA;AAAA,IACH;AAAA,EAEJ;AACF;AAEO,MAAM,yCACX,kCAAkC,QAAQ;AACrC,MAAM,yCACX,kCAAkC,QAAQ;"}
@@ -1,4 +0,0 @@
1
- import { CompileOptions } from './compilers.js';
2
- import * as t from '@babel/types';
3
- import type * as babel from '@babel/core';
4
- export declare function handleCreateIsomorphicFnCallExpression(path: babel.NodePath<t.CallExpression>, opts: CompileOptions): void;
@@ -1,49 +0,0 @@
1
- import * as t from "@babel/types";
2
- import { getRootCallExpression } from "./utils.js";
3
- function handleCreateIsomorphicFnCallExpression(path, opts) {
4
- const rootCallExpression = getRootCallExpression(path);
5
- const callExpressionPaths = {
6
- client: null,
7
- server: null
8
- };
9
- const validMethods = Object.keys(callExpressionPaths);
10
- rootCallExpression.traverse({
11
- MemberExpression(memberExpressionPath) {
12
- if (t.isIdentifier(memberExpressionPath.node.property)) {
13
- const name = memberExpressionPath.node.property.name;
14
- if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
15
- callExpressionPaths[name] = memberExpressionPath.parentPath;
16
- }
17
- }
18
- }
19
- });
20
- if (validMethods.every(
21
- (method) => !callExpressionPaths[method]
22
- )) {
23
- const variableId = rootCallExpression.parentPath.isVariableDeclarator() ? rootCallExpression.parentPath.node.id : null;
24
- console.warn(
25
- "createIsomorphicFn called without a client or server implementation!",
26
- "This will result in a no-op function.",
27
- "Variable name:",
28
- t.isIdentifier(variableId) ? variableId.name : "unknown"
29
- );
30
- }
31
- const envCallExpression = callExpressionPaths[opts.env];
32
- if (!envCallExpression) {
33
- rootCallExpression.replaceWith(
34
- t.arrowFunctionExpression([], t.blockStatement([]))
35
- );
36
- return;
37
- }
38
- const innerInputExpression = envCallExpression.node.arguments[0];
39
- if (!t.isExpression(innerInputExpression)) {
40
- throw new Error(
41
- `createIsomorphicFn().${opts.env}(func) must be called with a function!`
42
- );
43
- }
44
- rootCallExpression.replaceWith(innerInputExpression);
45
- }
46
- export {
47
- handleCreateIsomorphicFnCallExpression
48
- };
49
- //# sourceMappingURL=isomorphicFn.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isomorphicFn.js","sources":["../../../src/start-compiler-plugin/isomorphicFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateIsomorphicFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createIsomorphicFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (\n validMethods.every(\n (method) =>\n !callExpressionPaths[method as keyof typeof callExpressionPaths],\n )\n ) {\n const variableId = rootCallExpression.parentPath.isVariableDeclarator()\n ? rootCallExpression.parentPath.node.id\n : null\n console.warn(\n 'createIsomorphicFn called without a client or server implementation!',\n 'This will result in a no-op function.',\n 'Variable name:',\n t.isIdentifier(variableId) ? variableId.name : 'unknown',\n )\n }\n\n const envCallExpression = callExpressionPaths[opts.env]\n\n if (!envCallExpression) {\n // if we don't have an implementation for this environment, default to a no-op\n rootCallExpression.replaceWith(\n t.arrowFunctionExpression([], t.blockStatement([])),\n )\n return\n }\n\n const innerInputExpression = envCallExpression.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `createIsomorphicFn().${opts.env}(func) must be called with a function!`,\n )\n }\n\n rootCallExpression.replaceWith(innerInputExpression)\n}\n"],"names":[],"mappings":";;AAMO,SAAS,uCACd,MACA,MACA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,QAAQ;AAAA,IACR,QAAQ;AAAA,EAAA;AAGV,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SACpC;AAEH,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MACE,aAAa;AAAA,IACX,CAAC,WACC,CAAC,oBAAoB,MAA0C;AAAA,EAAA,GAEnE;AACA,UAAM,aAAa,mBAAmB,WAAW,qBAAA,IAC7C,mBAAmB,WAAW,KAAK,KACnC;AACJ,YAAQ;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA,EAAE,aAAa,UAAU,IAAI,WAAW,OAAO;AAAA,IAAA;AAAA,EAEnD;AAEA,QAAM,oBAAoB,oBAAoB,KAAK,GAAG;AAEtD,MAAI,CAAC,mBAAmB;AAEtB,uBAAmB;AAAA,MACjB,EAAE,wBAAwB,CAAA,GAAI,EAAE,eAAe,CAAA,CAAE,CAAC;AAAA,IAAA;AAEpD;AAAA,EACF;AAEA,QAAM,uBAAuB,kBAAkB,KAAK,UAAU,CAAC;AAE/D,MAAI,CAAC,EAAE,aAAa,oBAAoB,GAAG;AACzC,UAAM,IAAI;AAAA,MACR,wBAAwB,KAAK,GAAG;AAAA,IAAA;AAAA,EAEpC;AAEA,qBAAmB,YAAY,oBAAoB;AACrD;"}
@@ -1,12 +0,0 @@
1
- import { PluginOption } from 'vite';
2
- import { CompileStartFrameworkOptions } from './compilers.js';
3
- export type TanStackStartViteOptions = {
4
- globalMiddlewareEntry: string;
5
- };
6
- export declare function startCompilerPlugin(opts: {
7
- framework: CompileStartFrameworkOptions;
8
- environments: Array<{
9
- name: string;
10
- type: 'client' | 'server';
11
- }>;
12
- }): PluginOption;