Skip to content

Commit db88461

Browse files
committed
Refactor generateFunctionId for enhanced flexibility and consistency. Update related types, remove legacy currentId, and streamline deduplication logic.
directive-functions-plugin does not generate functionIds anymore. This is the responsibility of the server-functions-plugin
1 parent 391aa5f commit db88461

File tree

5 files changed

+41
-48
lines changed

5 files changed

+41
-48
lines changed

e2e/react-start/server-functions/vite.config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { tanstackStart } from '@tanstack/react-start/plugin/vite'
44
import viteReact from '@vitejs/plugin-react'
55

66
const FUNCTIONS_WITH_CONSTANT_ID = [
7-
'src/routes/submit-post-formdata.tsx--greetUser_createServerFn_handler',
8-
'src/routes/formdata-redirect/index.tsx--greetUser_createServerFn_handler',
7+
'src/routes/submit-post-formdata.tsx/greetUser_createServerFn_handler',
8+
'src/routes/formdata-redirect/index.tsx/greetUser_createServerFn_handler',
99
]
1010

1111
export default defineConfig({
@@ -15,9 +15,9 @@ export default defineConfig({
1515
}),
1616
tanstackStart({
1717
serverFns: {
18-
functionId: (opts) => {
19-
if (FUNCTIONS_WITH_CONSTANT_ID.includes(opts.currentId))
20-
return 'constant_id'
18+
generateFunctionId: (opts) => {
19+
const id = `${opts.filename}/${opts.functionName}`
20+
if (FUNCTIONS_WITH_CONSTANT_ID.includes(id)) return 'constant_id'
2121
else return undefined
2222
},
2323
},

packages/directive-functions-plugin/src/compilers.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export type SupportedFunctionPath =
2424
| babel.NodePath<babel.types.ArrowFunctionExpression>
2525

2626
export type GenerateFunctionIdFn = (opts: {
27-
currentId: string
28-
}) => string | undefined
27+
filename: string
28+
functionName: string
29+
}) => string
2930

3031
export type ReplacerFn = (opts: {
3132
fn: string
@@ -43,7 +44,7 @@ export type CompileDirectivesOpts = ParseAstOptions & {
4344
getRuntimeCode?: (opts: {
4445
directiveFnsById: Record<string, DirectiveFn>
4546
}) => string
46-
generateFunctionId?: GenerateFunctionIdFn
47+
generateFunctionId: GenerateFunctionIdFn
4748
replacer: ReplacerFn
4849
// devSplitImporter: string
4950
filename: string
@@ -219,7 +220,7 @@ export function findDirectives(
219220
directive: string
220221
directiveLabel: string
221222
replacer?: ReplacerFn
222-
generateFunctionId?: GenerateFunctionIdFn
223+
generateFunctionId: GenerateFunctionIdFn
223224
directiveSplitParam: string
224225
filename: string
225226
root: string
@@ -471,22 +472,10 @@ export function findDirectives(
471472
// Relative to have constant functionId regardless of the machine
472473
// that we are executing
473474
const relativeFilename = path.relative(opts.root, baseFilename)
474-
let functionId = `${relativeFilename}--${functionName}`
475-
if (opts.generateFunctionId) {
476-
functionId =
477-
opts.generateFunctionId({ currentId: functionId }) ?? functionId
478-
// Handle cases in which the returned id conflicts with
479-
// one of the already defined ids
480-
if (functionId in directiveFnsById) {
481-
let deduplicatedId
482-
let iteration = 0
483-
do {
484-
deduplicatedId = `${functionId}_${++iteration}`
485-
} while (deduplicatedId in directiveFnsById)
486-
functionId = deduplicatedId
487-
}
488-
}
489-
475+
const functionId = opts.generateFunctionId({
476+
filename: relativeFilename,
477+
functionName: functionName,
478+
})
490479
// If a replacer is provided, replace the function with the replacer
491480
if (opts.replacer) {
492481
const replacer = opts.replacer({

packages/directive-functions-plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type DirectiveFunctionsViteOptions = Pick<
3333
> &
3434
DirectiveFunctionsViteEnvOptions & {
3535
onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
36-
generateFunctionId?: GenerateFunctionIdFn
36+
generateFunctionId: GenerateFunctionIdFn
3737
}
3838

3939
const createDirectiveRx = (directive: string) =>
@@ -67,7 +67,7 @@ export type DirectiveFunctionsVitePluginEnvOptions = Pick<
6767
server: DirectiveFunctionsViteEnvOptions & { envName?: string }
6868
}
6969
onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
70-
generateFunctionId?: GenerateFunctionIdFn
70+
generateFunctionId: GenerateFunctionIdFn
7171
}
7272

7373
export function TanStackDirectiveFunctionsPluginEnv(

packages/server-functions-plugin/src/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ import type {
1212

1313
export type CreateRpcFn = (functionId: string, splitImportFn?: string) => any
1414

15+
export type GenerateFunctionIdFnOptional = (
16+
opts: Parameters<GenerateFunctionIdFn>[0],
17+
) => string | undefined
18+
1519
export type ServerFnPluginOpts = {
1620
/**
1721
* The virtual import ID that will be used to import the server function manifest.
1822
* This virtual import ID will be used in the server build to import the manifest
1923
* and its modules.
2024
*/
2125
manifestVirtualImportId: string
22-
generateFunctionId?: GenerateFunctionIdFn
26+
generateFunctionId?: GenerateFunctionIdFnOptional
2327
client: ServerFnPluginEnvOpts
2428
ssr: ServerFnPluginEnvOpts
2529
server: ServerFnPluginEnvOpts
@@ -282,39 +286,38 @@ function buildGenerateFunctionId(
282286
next: (dev: boolean, value?: string) => string,
283287
) => string,
284288
): GenerateFunctionIdFn {
285-
const currentIdToGeneratedId = new Map<string, string>()
286-
const generatedIds = new Set<string>()
289+
const entryIdToFunctionId = new Map<string, string>()
290+
const functionIds = new Set<string>()
287291
return (opts) => {
288-
// Keep the previous id in case we already generated it. This is for consistency
289-
// between client / server builds and hot reload
290-
let generatedId = currentIdToGeneratedId.get(opts.currentId)
291-
if (generatedId === undefined) {
292-
generatedId = delegate(opts, (dev, newId) => {
292+
const entryId = `${opts.filename}--${opts.functionName}`
293+
let functionId = entryIdToFunctionId.get(entryId)
294+
if (functionId === undefined) {
295+
functionId = delegate(opts, (dev, updatedFunctionId) => {
293296
// If no value provided, then return the url-safe currentId on development
294297
// and SHA256 using the currentId as seed on production
295-
if (newId === undefined) {
296-
if (dev) newId = makeFunctionIdUrlSafe(opts.currentId)
298+
if (updatedFunctionId === undefined) {
299+
if (dev) updatedFunctionId = makeFunctionIdUrlSafe(entryId)
297300
else
298-
newId = crypto
301+
updatedFunctionId = crypto
299302
.createHash('sha256')
300-
.update(opts.currentId)
303+
.update(entryId)
301304
.digest('hex')
302305
}
303-
return newId
306+
return updatedFunctionId
304307
})
305308
// Deduplicate in case the generated id conflicts with an existing id
306-
if (generatedIds.has(generatedId)) {
309+
if (functionIds.has(functionId)) {
307310
let deduplicatedId
308311
let iteration = 0
309312
do {
310-
deduplicatedId = `${generatedId}_${++iteration}`
311-
} while (generatedIds.has(deduplicatedId))
312-
generatedId = deduplicatedId
313+
deduplicatedId = `${functionId}_${++iteration}`
314+
} while (functionIds.has(deduplicatedId))
315+
functionId = deduplicatedId
313316
}
314-
currentIdToGeneratedId.set(opts.currentId, generatedId)
315-
generatedIds.add(generatedId)
317+
entryIdToFunctionId.set(entryId, functionId)
318+
functionIds.add(functionId)
316319
}
317-
return generatedId
320+
return functionId
318321
}
319322
}
320323

packages/start-plugin-core/src/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ const tanstackStartOptionsSchema = z
163163
.function()
164164
.args(
165165
z.object({
166-
currentId: z.string(),
166+
filename: z.string(),
167+
functionName: z.string(),
167168
}),
168169
)
169170
.returns(z.string().optional())

0 commit comments

Comments
 (0)