@tanstack/start-plugin-core 1.169.11 → 1.169.13

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 (43) hide show
  1. package/dist/esm/index.d.ts +1 -1
  2. package/dist/esm/rsbuild/index.d.ts +1 -0
  3. package/dist/esm/rsbuild/plugin.js +2 -0
  4. package/dist/esm/rsbuild/plugin.js.map +1 -1
  5. package/dist/esm/rsbuild/schema.d.ts +27 -27
  6. package/dist/esm/rsbuild/start-compiler-host.d.ts +3 -1
  7. package/dist/esm/rsbuild/start-compiler-host.js +6 -2
  8. package/dist/esm/rsbuild/start-compiler-host.js.map +1 -1
  9. package/dist/esm/schema.d.ts +51 -51
  10. package/dist/esm/start-compiler/compiler.d.ts +21 -5
  11. package/dist/esm/start-compiler/compiler.js +197 -48
  12. package/dist/esm/start-compiler/compiler.js.map +1 -1
  13. package/dist/esm/start-compiler/config.d.ts +7 -3
  14. package/dist/esm/start-compiler/config.js +19 -7
  15. package/dist/esm/start-compiler/config.js.map +1 -1
  16. package/dist/esm/start-compiler/handleCreateServerFn.js +12 -0
  17. package/dist/esm/start-compiler/handleCreateServerFn.js.map +1 -1
  18. package/dist/esm/start-compiler/host.d.ts +3 -1
  19. package/dist/esm/start-compiler/host.js +5 -3
  20. package/dist/esm/start-compiler/host.js.map +1 -1
  21. package/dist/esm/start-compiler/types.d.ts +4 -13
  22. package/dist/esm/types.d.ts +33 -0
  23. package/dist/esm/vite/index.d.ts +1 -0
  24. package/dist/esm/vite/plugin.js +2 -0
  25. package/dist/esm/vite/plugin.js.map +1 -1
  26. package/dist/esm/vite/schema.d.ts +27 -27
  27. package/dist/esm/vite/start-compiler-plugin/plugin.d.ts +3 -1
  28. package/dist/esm/vite/start-compiler-plugin/plugin.js +6 -2
  29. package/dist/esm/vite/start-compiler-plugin/plugin.js.map +1 -1
  30. package/package.json +6 -6
  31. package/src/index.ts +6 -1
  32. package/src/rsbuild/index.ts +5 -0
  33. package/src/rsbuild/plugin.ts +3 -0
  34. package/src/rsbuild/start-compiler-host.ts +22 -3
  35. package/src/start-compiler/compiler.ts +389 -70
  36. package/src/start-compiler/config.ts +43 -6
  37. package/src/start-compiler/handleCreateServerFn.ts +29 -0
  38. package/src/start-compiler/host.ts +13 -3
  39. package/src/start-compiler/types.ts +5 -14
  40. package/src/types.ts +44 -0
  41. package/src/vite/index.ts +5 -0
  42. package/src/vite/plugin.ts +3 -0
  43. package/src/vite/start-compiler-plugin/plugin.ts +22 -3
@@ -1,27 +1,47 @@
1
- import { KindDetectionPatterns, LookupKindsPerEnv } from './compiler'
2
- import type { LookupConfig, LookupKind } from './compiler'
3
- import type { CompileStartFrameworkOptions } from '../types'
1
+ import {
2
+ KindDetectionPatterns,
3
+ getExternalLookupKind,
4
+ getLookupKindsForEnv,
5
+ isCompilerTransformEnabledForEnv,
6
+ } from './compiler'
7
+ import type { BuiltInLookupKind, LookupConfig } from './compiler'
8
+ import type {
9
+ CompileStartFrameworkOptions,
10
+ StartCompilerImportTransform,
11
+ } from '../types'
4
12
 
5
13
  export function getTransformCodeFilterForEnv(
6
14
  env: 'client' | 'server',
15
+ opts?: {
16
+ compilerTransforms?: Array<StartCompilerImportTransform> | undefined
17
+ },
7
18
  ): Array<RegExp> {
8
- const validKinds = LookupKindsPerEnv[env]
19
+ const validKinds = getLookupKindsForEnv(env, opts)
9
20
  const patterns: Array<RegExp> = []
10
21
 
11
22
  for (const [kind, pattern] of Object.entries(KindDetectionPatterns) as Array<
12
- [LookupKind, RegExp]
23
+ [BuiltInLookupKind, RegExp]
13
24
  >) {
14
25
  if (validKinds.has(kind)) {
15
26
  patterns.push(pattern)
16
27
  }
17
28
  }
18
29
 
30
+ for (const transform of opts?.compilerTransforms ?? []) {
31
+ if (isCompilerTransformEnabledForEnv(transform, env)) {
32
+ patterns.push(transform.detect)
33
+ }
34
+ }
35
+
19
36
  return patterns
20
37
  }
21
38
 
22
39
  export function getLookupConfigurationsForEnv(
23
40
  env: 'client' | 'server',
24
41
  framework: CompileStartFrameworkOptions,
42
+ opts?: {
43
+ compilerTransforms?: Array<StartCompilerImportTransform> | undefined
44
+ },
25
45
  ): Array<LookupConfig> {
26
46
  const commonConfigs: Array<LookupConfig> = [
27
47
  {
@@ -46,6 +66,20 @@ export function getLookupConfigurationsForEnv(
46
66
  },
47
67
  ]
48
68
 
69
+ const externalConfigs: Array<LookupConfig> = []
70
+ for (const transform of opts?.compilerTransforms ?? []) {
71
+ if (!isCompilerTransformEnabledForEnv(transform, env)) continue
72
+
73
+ const kind = getExternalLookupKind(transform)
74
+ for (const imported of transform.imports) {
75
+ externalConfigs.push({
76
+ libName: imported.libName,
77
+ rootExport: imported.rootExport,
78
+ kind,
79
+ })
80
+ }
81
+ }
82
+
49
83
  if (env === 'client') {
50
84
  return [
51
85
  {
@@ -59,10 +93,11 @@ export function getLookupConfigurationsForEnv(
59
93
  kind: 'Root',
60
94
  },
61
95
  ...commonConfigs,
96
+ ...externalConfigs,
62
97
  ]
63
98
  }
64
99
 
65
- return [
100
+ const serverConfigs: Array<LookupConfig> = [
66
101
  ...commonConfigs,
67
102
  {
68
103
  libName: `@tanstack/${framework}-router`,
@@ -70,4 +105,6 @@ export function getLookupConfigurationsForEnv(
70
105
  kind: 'ClientOnlyJSX',
71
106
  },
72
107
  ]
108
+
109
+ return [...serverConfigs, ...externalConfigs]
73
110
  }
@@ -196,6 +196,13 @@ export function handleCreateServerFn(
196
196
  }
197
197
 
198
198
  const isProviderFile = context.id.includes(TSS_SERVERFN_SPLIT_PARAM)
199
+ if (isProviderFile && context.serverFnProviderModuleDirectives) {
200
+ ensureDirectivePrologue(
201
+ context.ast,
202
+ context.serverFnProviderModuleDirectives,
203
+ )
204
+ }
205
+
199
206
  // Get environment-specific configuration
200
207
  const envConfig = getEnvConfig(context, isProviderFile)
201
208
 
@@ -504,3 +511,25 @@ function safeRemoveExports(ast: t.File): void {
504
511
  return node
505
512
  })
506
513
  }
514
+
515
+ function ensureDirectivePrologue(
516
+ ast: t.File,
517
+ directiveValues: ReadonlyArray<string>,
518
+ ): void {
519
+ const directives = ast.program.directives
520
+ const existingDirectives = new Set(
521
+ directives.map((directive) => directive.value.value),
522
+ )
523
+ const missingDirectives: Array<string> = []
524
+
525
+ for (const directiveValue of directiveValues) {
526
+ if (!directiveValue || existingDirectives.has(directiveValue)) continue
527
+
528
+ existingDirectives.add(directiveValue)
529
+ missingDirectives.push(directiveValue)
530
+ }
531
+
532
+ for (let i = missingDirectives.length - 1; i >= 0; i--) {
533
+ directives.unshift(t.directive(t.directiveLiteral(missingDirectives[i]!)))
534
+ }
535
+ }
@@ -1,6 +1,9 @@
1
- import { LookupKindsPerEnv, StartCompiler } from './compiler'
1
+ import { StartCompiler, getLookupKindsForEnv } from './compiler'
2
2
  import { getLookupConfigurationsForEnv } from './config'
3
- import type { CompileStartFrameworkOptions } from '../types'
3
+ import type {
4
+ CompileStartFrameworkOptions,
5
+ StartCompilerImportTransform,
6
+ } from '../types'
4
7
  import type {
5
8
  DevServerFnModuleSpecifierEncoder,
6
9
  GenerateFunctionIdFnOptional,
@@ -15,6 +18,8 @@ export interface CreateStartCompilerOptions {
15
18
  providerEnvName: string
16
19
  mode: 'dev' | 'build'
17
20
  generateFunctionId?: GenerateFunctionIdFnOptional
21
+ compilerTransforms?: Array<StartCompilerImportTransform> | undefined
22
+ serverFnProviderModuleDirectives?: ReadonlyArray<string> | undefined
18
23
  onServerFnsById?: (d: Record<string, ServerFn>) => void
19
24
  getKnownServerFns: () => Record<string, ServerFn>
20
25
  encodeModuleSpecifierInDev?: DevServerFnModuleSpecifierEncoder
@@ -29,16 +34,21 @@ export function createStartCompiler(
29
34
  env: options.env,
30
35
  envName: options.envName,
31
36
  root: options.root,
32
- lookupKinds: LookupKindsPerEnv[options.env],
37
+ lookupKinds: getLookupKindsForEnv(options.env, {
38
+ compilerTransforms: options.compilerTransforms,
39
+ }),
33
40
  lookupConfigurations: getLookupConfigurationsForEnv(
34
41
  options.env,
35
42
  options.framework,
43
+ { compilerTransforms: options.compilerTransforms },
36
44
  ),
37
45
  mode: options.mode,
38
46
  framework: options.framework,
39
47
  providerEnvName: options.providerEnvName,
40
48
  generateFunctionId: options.generateFunctionId,
41
49
  onServerFnsById: options.onServerFnsById,
50
+ compilerTransforms: options.compilerTransforms,
51
+ serverFnProviderModuleDirectives: options.serverFnProviderModuleDirectives,
42
52
  getKnownServerFns: options.getKnownServerFns,
43
53
  devServerFnModuleSpecifierEncoder: options.encodeModuleSpecifierInDev,
44
54
  loadModule: options.loadModule,
@@ -1,28 +1,18 @@
1
1
  import type * as babel from '@babel/core'
2
2
  import type * as t from '@babel/types'
3
- import type { CompileStartFrameworkOptions } from '../types'
3
+ import type { StartCompilerTransformContext } from '../types'
4
4
 
5
5
  /**
6
6
  * Context passed to all plugin handlers during compilation.
7
7
  * Contains both read-only input data and mutable state that handlers update.
8
8
  */
9
- export interface CompilationContext {
10
- readonly ast: t.File
11
- readonly code: string
12
- readonly id: string
13
- readonly env: 'client' | 'server'
14
- readonly envName: string
15
- readonly mode: 'dev' | 'build'
16
- readonly root: string
17
- /** The framework being used (e.g., 'react', 'solid') */
18
- readonly framework: CompileStartFrameworkOptions
19
- /** The Vite environment name for the server function provider */
20
- readonly providerEnvName: string
21
-
9
+ export interface CompilationContext extends StartCompilerTransformContext {
22
10
  /** Generate a unique function ID */
23
11
  generateFunctionId: GenerateFunctionIdFn
24
12
  /** Get known server functions from previous builds (e.g., client build) */
25
13
  getKnownServerFns: () => Record<string, ServerFn>
14
+ /** Module-level directives to add to extracted server function provider files. */
15
+ serverFnProviderModuleDirectives: ReadonlyArray<string> | undefined
26
16
 
27
17
  /**
28
18
  * Callback when server functions are discovered.
@@ -30,6 +20,7 @@ export interface CompilationContext {
30
20
  */
31
21
  onServerFnsById: ((d: Record<string, ServerFn>) => void) | undefined
32
22
  }
23
+
33
24
  /**
34
25
  * Batched plugin handler signature.
35
26
  * Receives ALL candidates of a specific kind in one call.
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type * as babel from '@babel/core'
2
+ import type * as t from '@babel/types'
1
3
  import type { TanStackStartOutputConfig } from './schema'
2
4
 
3
5
  export type CompileStartFrameworkOptions = 'react' | 'solid' | 'vue'
@@ -20,6 +22,46 @@ export type SerializationAdapterConfig =
20
22
  | SerializationAdapterModuleRef
21
23
  | SerializationAdapterByRuntime
22
24
 
25
+ export type StartCompilerEnvironment = 'client' | 'server'
26
+
27
+ export interface StartCompilerImportTransformImport {
28
+ libName: string
29
+ rootExport: string
30
+ }
31
+
32
+ export interface StartCompilerTransformCandidate {
33
+ path: babel.NodePath<t.CallExpression>
34
+ }
35
+
36
+ export interface StartCompilerTransformContext {
37
+ readonly ast: t.File
38
+ readonly code: string
39
+ readonly id: string
40
+ readonly env: StartCompilerEnvironment
41
+ readonly envName: string
42
+ readonly mode: 'dev' | 'build'
43
+ readonly root: string
44
+ readonly framework: CompileStartFrameworkOptions
45
+ readonly providerEnvName: string
46
+ readonly types: typeof t
47
+ parseExpression: (code: string) => t.Expression
48
+ }
49
+
50
+ export interface StartCompilerImportTransform {
51
+ name: string
52
+ environment?:
53
+ | StartCompilerEnvironment
54
+ | Array<StartCompilerEnvironment>
55
+ | undefined
56
+ imports: Array<StartCompilerImportTransformImport>
57
+ detect: RegExp
58
+ order?: 'pre' | 'post' | undefined
59
+ transform: (
60
+ candidates: Array<StartCompilerTransformCandidate>,
61
+ context: StartCompilerTransformContext,
62
+ ) => void
63
+ }
64
+
23
65
  export interface NormalizedBasePaths {
24
66
  publicBase: string
25
67
  assetBase: {
@@ -60,6 +102,8 @@ export interface TanStackStartCoreOptions {
60
102
  providerEnvironmentName: string
61
103
  ssrIsProvider: boolean
62
104
  serializationAdapters?: Array<SerializationAdapterConfig> | undefined
105
+ compilerTransforms?: Array<StartCompilerImportTransform> | undefined
106
+ serverFnProviderModuleDirectives?: ReadonlyArray<string> | undefined
63
107
  }
64
108
 
65
109
  export interface ResolvedStartConfig {
package/src/vite/index.ts CHANGED
@@ -3,6 +3,11 @@ export type {
3
3
  ViteRscForwardSsrResolverStrategy,
4
4
  } from './types'
5
5
  export type { TanStackStartViteInputConfig } from './schema'
6
+ export type {
7
+ StartCompilerImportTransform,
8
+ StartCompilerTransformCandidate,
9
+ StartCompilerTransformContext,
10
+ } from '../types'
6
11
  export { START_ENVIRONMENT_NAMES, VITE_ENVIRONMENT_NAMES } from '../constants'
7
12
  export { createVirtualModule } from './createVirtualModule'
8
13
  export { tanStackStartVite } from './plugin'
@@ -217,6 +217,9 @@ export function tanStackStartVite(
217
217
  environments,
218
218
  generateFunctionId:
219
219
  normalizedStartPluginOpts.serverFns?.generateFunctionId,
220
+ compilerTransforms: corePluginOpts.compilerTransforms,
221
+ serverFnProviderModuleDirectives:
222
+ corePluginOpts.serverFnProviderModuleDirectives,
220
223
  providerEnvName: serverFnProviderEnv,
221
224
  }),
222
225
  importProtectionPlugin({
@@ -20,7 +20,10 @@ import {
20
20
  decodeViteDevServerModuleSpecifier,
21
21
  } from './module-specifier'
22
22
  import { mergeHotUpdateModules } from './hot-update'
23
- import type { CompileStartFrameworkOptions } from '../../types'
23
+ import type {
24
+ CompileStartFrameworkOptions,
25
+ StartCompilerImportTransform,
26
+ } from '../../types'
24
27
  import type {
25
28
  GenerateFunctionIdFnOptional,
26
29
  ServerFn,
@@ -166,6 +169,8 @@ export interface StartCompilerPluginOptions {
166
169
  * Custom function ID generator (optional).
167
170
  */
168
171
  generateFunctionId?: GenerateFunctionIdFnOptional
172
+ compilerTransforms?: Array<StartCompilerImportTransform> | undefined
173
+ serverFnProviderModuleDirectives?: ReadonlyArray<string> | undefined
169
174
  /**
170
175
  * The Vite environment name for the server function provider.
171
176
  */
@@ -202,8 +207,18 @@ export function startCompilerPlugin(
202
207
  name: string
203
208
  type: 'client' | 'server'
204
209
  }): PluginOption {
210
+ const compilerTransforms =
211
+ environment.name === opts.providerEnvName
212
+ ? opts.compilerTransforms
213
+ : undefined
214
+ const serverFnProviderModuleDirectives =
215
+ environment.name === opts.providerEnvName
216
+ ? opts.serverFnProviderModuleDirectives
217
+ : undefined
205
218
  // Derive transform code filter from KindDetectionPatterns (single source of truth)
206
- const transformCodeFilter = getTransformCodeFilterForEnv(environment.type)
219
+ const transformCodeFilter = getTransformCodeFilterForEnv(environment.type, {
220
+ compilerTransforms,
221
+ })
207
222
  return {
208
223
  name: `tanstack-start-core::server-fn:${environment.name}`,
209
224
  enforce: 'pre',
@@ -238,6 +253,8 @@ export function startCompilerPlugin(
238
253
  framework: opts.framework,
239
254
  providerEnvName: opts.providerEnvName,
240
255
  generateFunctionId: opts.generateFunctionId,
256
+ compilerTransforms,
257
+ serverFnProviderModuleDirectives,
241
258
  onServerFnsById,
242
259
  getKnownServerFns: () => serverFnsById,
243
260
  encodeModuleSpecifierInDev:
@@ -281,7 +298,9 @@ export function startCompilerPlugin(
281
298
  }
282
299
 
283
300
  // Detect which kinds are present in this file before parsing
284
- const detectedKinds = detectKindsInCode(code, environment.type)
301
+ const detectedKinds = detectKindsInCode(code, environment.type, {
302
+ compilerTransforms,
303
+ })
285
304
 
286
305
  const result = await compiler.compile({
287
306
  id,