Skip to content

Commit 391aa5f

Browse files
committed
Rename functionId to generateFunctionId for clarity and consistency across packages. Adjust associated logic and type definitions.
Only the internal implementation converts to url safe. If a consumer wants to use their own generator, the responsibility of generating safe urls is on them
1 parent f5d22d1 commit 391aa5f

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

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

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

26-
export type FunctionIdFn = (opts: { currentId: string }) => string | undefined
26+
export type GenerateFunctionIdFn = (opts: {
27+
currentId: string
28+
}) => string | undefined
2729

2830
export type ReplacerFn = (opts: {
2931
fn: string
@@ -41,7 +43,7 @@ export type CompileDirectivesOpts = ParseAstOptions & {
4143
getRuntimeCode?: (opts: {
4244
directiveFnsById: Record<string, DirectiveFn>
4345
}) => string
44-
functionId?: FunctionIdFn
46+
generateFunctionId?: GenerateFunctionIdFn
4547
replacer: ReplacerFn
4648
// devSplitImporter: string
4749
filename: string
@@ -202,14 +204,6 @@ function findNearestVariableName(
202204
return nameParts.length > 0 ? nameParts.join('_') : 'anonymous'
203205
}
204206

205-
function makeFunctionIdUrlSafe(location: string): string {
206-
return location
207-
.replace(/[^a-zA-Z0-9-_]/g, '_') // Replace unsafe chars with underscore
208-
.replace(/_{2,}/g, '_') // Collapse multiple underscores
209-
.replace(/^_|_$/g, '') // Trim leading/trailing underscores
210-
.replace(/_--/g, '--') // Clean up the joiner
211-
}
212-
213207
function makeIdentifierSafe(identifier: string): string {
214208
return identifier
215209
.replace(/[^a-zA-Z0-9_$]/g, '_') // Replace unsafe chars with underscore
@@ -225,7 +219,7 @@ export function findDirectives(
225219
directive: string
226220
directiveLabel: string
227221
replacer?: ReplacerFn
228-
functionId?: FunctionIdFn
222+
generateFunctionId?: GenerateFunctionIdFn
229223
directiveSplitParam: string
230224
filename: string
231225
root: string
@@ -478,8 +472,9 @@ export function findDirectives(
478472
// that we are executing
479473
const relativeFilename = path.relative(opts.root, baseFilename)
480474
let functionId = `${relativeFilename}--${functionName}`
481-
if (opts.functionId) {
482-
functionId = opts.functionId({ currentId: functionId }) ?? functionId
475+
if (opts.generateFunctionId) {
476+
functionId =
477+
opts.generateFunctionId({ currentId: functionId }) ?? functionId
483478
// Handle cases in which the returned id conflicts with
484479
// one of the already defined ids
485480
if (functionId in directiveFnsById) {
@@ -491,7 +486,6 @@ export function findDirectives(
491486
functionId = deduplicatedId
492487
}
493488
}
494-
functionId = makeFunctionIdUrlSafe(functionId)
495489

496490
// If a replacer is provided, replace the function with the replacer
497491
if (opts.replacer) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { compileDirectives } from './compilers'
55
import type {
66
CompileDirectivesOpts,
77
DirectiveFn,
8-
FunctionIdFn,
8+
GenerateFunctionIdFn,
99
} from './compilers'
1010
import type { Plugin } from 'vite'
1111

@@ -17,7 +17,7 @@ export type {
1717
DirectiveFn,
1818
CompileDirectivesOpts,
1919
ReplacerFn,
20-
FunctionIdFn,
20+
GenerateFunctionIdFn,
2121
} from './compilers'
2222

2323
export type DirectiveFunctionsViteEnvOptions = Pick<
@@ -33,7 +33,7 @@ export type DirectiveFunctionsViteOptions = Pick<
3333
> &
3434
DirectiveFunctionsViteEnvOptions & {
3535
onDirectiveFnsById?: (directiveFnsById: Record<string, DirectiveFn>) => void
36-
functionId?: FunctionIdFn
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-
functionId?: FunctionIdFn
70+
generateFunctionId?: GenerateFunctionIdFn
7171
}
7272

7373
export function TanStackDirectiveFunctionsPluginEnv(
@@ -138,7 +138,7 @@ function transformCode({
138138
directive,
139139
directiveLabel,
140140
getRuntimeCode,
141-
functionId,
141+
generateFunctionId,
142142
replacer,
143143
onDirectiveFnsById,
144144
root,
@@ -163,7 +163,7 @@ function transformCode({
163163
directive,
164164
directiveLabel,
165165
getRuntimeCode,
166-
functionId,
166+
generateFunctionId,
167167
replacer,
168168
code,
169169
root,

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import type { DevEnvironment, Plugin, ViteDevServer } from 'vite'
77
import type {
88
DirectiveFn,
9-
FunctionIdFn,
9+
GenerateFunctionIdFn,
1010
ReplacerFn,
1111
} from '@tanstack/directive-functions-plugin'
1212

@@ -19,7 +19,7 @@ export type ServerFnPluginOpts = {
1919
* and its modules.
2020
*/
2121
manifestVirtualImportId: string
22-
functionId?: FunctionIdFn
22+
generateFunctionId?: GenerateFunctionIdFn
2323
client: ServerFnPluginEnvOpts
2424
ssr: ServerFnPluginEnvOpts
2525
server: ServerFnPluginEnvOpts
@@ -58,8 +58,12 @@ export function createTanStackServerFnPlugin(opts: ServerFnPluginOpts): {
5858
}
5959
},
6060
})
61-
const functionId = buildFunctionId((functionIdOpts, next) =>
62-
next(Boolean(viteDevServer), opts.functionId?.(functionIdOpts)),
61+
const generateFunctionId = buildGenerateFunctionId(
62+
(generateFunctionIdOpts, next) =>
63+
next(
64+
Boolean(viteDevServer),
65+
opts.generateFunctionId?.(generateFunctionIdOpts),
66+
),
6367
)
6468

6569
const directive = 'use server'
@@ -74,7 +78,7 @@ export function createTanStackServerFnPlugin(opts: ServerFnPluginOpts): {
7478
directive,
7579
directiveLabel,
7680
getRuntimeCode: opts.client.getRuntimeCode,
77-
functionId,
81+
generateFunctionId,
7882
replacer: opts.client.replacer,
7983
onDirectiveFnsById,
8084
}),
@@ -86,7 +90,7 @@ export function createTanStackServerFnPlugin(opts: ServerFnPluginOpts): {
8690
directive,
8791
directiveLabel,
8892
getRuntimeCode: opts.ssr.getRuntimeCode,
89-
functionId,
93+
generateFunctionId,
9094
replacer: opts.ssr.replacer,
9195
onDirectiveFnsById,
9296
}),
@@ -136,7 +140,7 @@ export function createTanStackServerFnPlugin(opts: ServerFnPluginOpts): {
136140
directive,
137141
directiveLabel,
138142
getRuntimeCode: opts.server.getRuntimeCode,
139-
functionId,
143+
generateFunctionId,
140144
replacer: opts.server.replacer,
141145
onDirectiveFnsById,
142146
}),
@@ -182,8 +186,12 @@ export function TanStackServerFnPluginEnv(
182186
},
183187
})
184188

185-
const functionId = buildFunctionId((functionIdOpts, next) =>
186-
next(Boolean(serverDevEnv), opts.functionId?.(functionIdOpts)),
189+
const generateFunctionId = buildGenerateFunctionId(
190+
(generateFunctionIdOpts, next) =>
191+
next(
192+
Boolean(serverDevEnv),
193+
opts.generateFunctionId?.(generateFunctionIdOpts),
194+
),
187195
)
188196

189197
const directive = 'use server'
@@ -196,7 +204,7 @@ export function TanStackServerFnPluginEnv(
196204
directive,
197205
directiveLabel,
198206
onDirectiveFnsById,
199-
functionId,
207+
generateFunctionId,
200208
environments: {
201209
client: {
202210
envLabel: 'Client',
@@ -260,12 +268,20 @@ function resolveViteId(id: string) {
260268
return `\0${id}`
261269
}
262270

263-
function buildFunctionId(
271+
function makeFunctionIdUrlSafe(location: string): string {
272+
return location
273+
.replace(/[^a-zA-Z0-9-_]/g, '_') // Replace unsafe chars with underscore
274+
.replace(/_{2,}/g, '_') // Collapse multiple underscores
275+
.replace(/^_|_$/g, '') // Trim leading/trailing underscores
276+
.replace(/_--/g, '--') // Clean up the joiner
277+
}
278+
279+
function buildGenerateFunctionId(
264280
delegate: (
265-
opts: Parameters<FunctionIdFn>[0],
281+
opts: Parameters<GenerateFunctionIdFn>[0],
266282
next: (dev: boolean, value?: string) => string,
267283
) => string,
268-
): FunctionIdFn {
284+
): GenerateFunctionIdFn {
269285
const currentIdToGeneratedId = new Map<string, string>()
270286
const generatedIds = new Set<string>()
271287
return (opts) => {
@@ -274,10 +290,10 @@ function buildFunctionId(
274290
let generatedId = currentIdToGeneratedId.get(opts.currentId)
275291
if (generatedId === undefined) {
276292
generatedId = delegate(opts, (dev, newId) => {
277-
// If no value provided, then return the currentId on development
293+
// If no value provided, then return the url-safe currentId on development
278294
// and SHA256 using the currentId as seed on production
279295
if (newId === undefined) {
280-
if (dev) newId = opts.currentId
296+
if (dev) newId = makeFunctionIdUrlSafe(opts.currentId)
281297
else
282298
newId = crypto
283299
.createHash('sha256')
@@ -286,7 +302,6 @@ function buildFunctionId(
286302
}
287303
return newId
288304
})
289-
290305
// Deduplicate in case the generated id conflicts with an existing id
291306
if (generatedIds.has(generatedId)) {
292307
let deduplicatedId

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export function TanStackStartVitePluginCore(
334334
// This is the ID that will be available to look up and import
335335
// our server function manifest and resolve its module
336336
manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,
337-
functionId: startPluginOpts?.serverFns?.functionId,
337+
generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,
338338
client: {
339339
getRuntimeCode: () =>
340340
`import { createClientRpc } from '@tanstack/${corePluginOpts.framework}-start/client-rpc'`,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const tanstackStartOptionsSchema = z
159159
serverFns: z
160160
.object({
161161
base: z.string().optional().default('/_serverFn'),
162-
functionId: z
162+
generateFunctionId: z
163163
.function()
164164
.args(
165165
z.object({

0 commit comments

Comments
 (0)