@tanstack/start-plugin-core 1.132.0-alpha.6 → 1.132.0-alpha.8

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 (36) hide show
  1. package/dist/esm/start-compiler-plugin/compilers.d.ts +0 -6
  2. package/dist/esm/start-compiler-plugin/compilers.js +12 -271
  3. package/dist/esm/start-compiler-plugin/compilers.js.map +1 -1
  4. package/dist/esm/start-compiler-plugin/constants.d.ts +1 -1
  5. package/dist/esm/start-compiler-plugin/constants.js +2 -2
  6. package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
  7. package/dist/esm/start-compiler-plugin/envOnly.d.ts +5 -0
  8. package/dist/esm/start-compiler-plugin/envOnly.js +41 -0
  9. package/dist/esm/start-compiler-plugin/envOnly.js.map +1 -0
  10. package/dist/esm/start-compiler-plugin/isomorphicFn.d.ts +4 -0
  11. package/dist/esm/start-compiler-plugin/isomorphicFn.js +49 -0
  12. package/dist/esm/start-compiler-plugin/isomorphicFn.js.map +1 -0
  13. package/dist/esm/start-compiler-plugin/middleware.d.ts +4 -0
  14. package/dist/esm/start-compiler-plugin/middleware.js +51 -0
  15. package/dist/esm/start-compiler-plugin/middleware.js.map +1 -0
  16. package/dist/esm/start-compiler-plugin/plugin.js +59 -26
  17. package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
  18. package/dist/esm/start-compiler-plugin/serverFileRoute.d.ts +4 -0
  19. package/dist/esm/start-compiler-plugin/serverFileRoute.js +38 -0
  20. package/dist/esm/start-compiler-plugin/serverFileRoute.js.map +1 -0
  21. package/dist/esm/start-compiler-plugin/serverFn.d.ts +4 -0
  22. package/dist/esm/start-compiler-plugin/serverFn.js +87 -0
  23. package/dist/esm/start-compiler-plugin/serverFn.js.map +1 -0
  24. package/dist/esm/start-compiler-plugin/utils.d.ts +13 -0
  25. package/dist/esm/start-compiler-plugin/utils.js +30 -0
  26. package/dist/esm/start-compiler-plugin/utils.js.map +1 -0
  27. package/package.json +5 -5
  28. package/src/start-compiler-plugin/compilers.ts +16 -462
  29. package/src/start-compiler-plugin/constants.ts +2 -2
  30. package/src/start-compiler-plugin/envOnly.ts +58 -0
  31. package/src/start-compiler-plugin/isomorphicFn.ts +78 -0
  32. package/src/start-compiler-plugin/middleware.ts +79 -0
  33. package/src/start-compiler-plugin/plugin.ts +67 -36
  34. package/src/start-compiler-plugin/serverFileRoute.ts +59 -0
  35. package/src/start-compiler-plugin/serverFn.ts +163 -0
  36. package/src/start-compiler-plugin/utils.ts +41 -0
@@ -3,8 +3,6 @@ import * as babel from '@babel/core';
3
3
  import * as t from '@babel/types';
4
4
  export type CompileStartFrameworkOptions = 'react' | 'solid';
5
5
  export declare function compileStartOutputFactory(framework: CompileStartFrameworkOptions): (opts: CompileOptions) => GeneratorResult;
6
- export declare const handleServerOnlyCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
7
- export declare const handleClientOnlyCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
8
6
  export type CompileOptions = ParseAstOptions & {
9
7
  env: 'server' | 'client';
10
8
  dce?: boolean;
@@ -15,7 +13,3 @@ export type IdentifierConfig = {
15
13
  handleCallExpression: (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
16
14
  paths: Array<babel.NodePath>;
17
15
  };
18
- export declare function handleCreateServerFnCallExpression(path: babel.NodePath<t.CallExpression>, opts: CompileOptions): void;
19
- export declare function handleCreateMiddlewareCallExpression(path: babel.NodePath<t.CallExpression>, opts: CompileOptions): void;
20
- export declare function handleCreateIsomorphicFnCallExpression(path: babel.NodePath<t.CallExpression>, opts: CompileOptions): void;
21
- export declare function getRootCallExpression(path: babel.NodePath<t.CallExpression>): babel.NodePath<t.CallExpression>;
@@ -1,9 +1,13 @@
1
1
  import * as babel from "@babel/core";
2
2
  import * as t from "@babel/types";
3
- import { codeFrameColumns } from "@babel/code-frame";
4
3
  import { findReferencedIdentifiers, deadCodeElimination } from "babel-dead-code-elimination";
5
4
  import { parseAst, generateFromAst } from "@tanstack/router-utils";
6
5
  import { transformFuncs } from "./constants.js";
6
+ import { handleCreateServerFileRouteCallExpressionFactory } from "./serverFileRoute.js";
7
+ import { handleCreateIsomorphicFnCallExpression } from "./isomorphicFn.js";
8
+ import { handleCreateMiddlewareCallExpression } from "./middleware.js";
9
+ import { handleCreateServerFnCallExpression } from "./serverFn.js";
10
+ import { handleCreateClientOnlyFnCallExpression, handleCreateServerOnlyFnCallExpression } from "./envOnly.js";
7
11
  const getIdentifiers = (framework) => ({
8
12
  createServerRootRoute: {
9
13
  name: "createServerRootRoute",
@@ -39,14 +43,14 @@ const getIdentifiers = (framework) => ({
39
43
  handleCallExpression: handleCreateMiddlewareCallExpression,
40
44
  paths: []
41
45
  },
42
- serverOnly: {
43
- name: "serverOnly",
44
- handleCallExpression: handleServerOnlyCallExpression,
46
+ createServerOnlyFn: {
47
+ name: "createServerOnlyFn",
48
+ handleCallExpression: handleCreateServerOnlyFnCallExpression,
45
49
  paths: []
46
50
  },
47
- clientOnly: {
48
- name: "clientOnly",
49
- handleCallExpression: handleClientOnlyCallExpression,
51
+ createClientOnlyFn: {
52
+ name: "createClientOnlyFn",
53
+ handleCallExpression: handleCreateClientOnlyFnCallExpression,
50
54
  paths: []
51
55
  },
52
56
  createIsomorphicFn: {
@@ -127,270 +131,7 @@ function compileStartOutputFactory(framework) {
127
131
  });
128
132
  };
129
133
  }
130
- function handleCreateServerFileRouteCallExpressionFactory(framework, method) {
131
- return function handleCreateServerFileRouteCallExpression(path, opts) {
132
- const PACKAGES = { start: `@tanstack/${framework}-start/server` };
133
- let highestParent = path;
134
- while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {
135
- highestParent = highestParent.parentPath;
136
- }
137
- const programPath = highestParent.parentPath;
138
- if (opts.env === "client") {
139
- highestParent.remove();
140
- return;
141
- }
142
- let isCreateServerFileRouteImported = false;
143
- programPath.traverse({
144
- ImportDeclaration(importPath) {
145
- const importSource = importPath.node.source.value;
146
- if (importSource === PACKAGES.start) {
147
- const specifiers = importPath.node.specifiers;
148
- isCreateServerFileRouteImported ||= specifiers.some((specifier) => {
149
- return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported) && specifier.imported.name === method;
150
- });
151
- }
152
- }
153
- });
154
- if (!isCreateServerFileRouteImported) {
155
- const importDeclaration = t.importDeclaration(
156
- [t.importSpecifier(t.identifier(method), t.identifier(method))],
157
- t.stringLiteral(PACKAGES.start)
158
- );
159
- programPath.node.body.unshift(importDeclaration);
160
- }
161
- };
162
- }
163
- const handleServerOnlyCallExpression = buildEnvOnlyCallExpressionHandler("server");
164
- const handleClientOnlyCallExpression = buildEnvOnlyCallExpressionHandler("client");
165
- function handleCreateServerFnCallExpression(path, opts) {
166
- const calledOptions = path.node.arguments[0] ? path.get("arguments.0") : null;
167
- const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {
168
- return t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "validateClient" && t.isBooleanLiteral(prop.value) && prop.value.value === true;
169
- });
170
- const callExpressionPaths = {
171
- middleware: null,
172
- validator: null,
173
- handler: null
174
- };
175
- const validMethods = Object.keys(callExpressionPaths);
176
- const rootCallExpression = getRootCallExpression(path);
177
- if (!rootCallExpression.parentPath.isVariableDeclarator()) {
178
- throw new Error("createServerFn must be assigned to a variable!");
179
- }
180
- const variableDeclarator = rootCallExpression.parentPath.node;
181
- const existingVariableName = variableDeclarator.id.name;
182
- rootCallExpression.traverse({
183
- MemberExpression(memberExpressionPath) {
184
- if (t.isIdentifier(memberExpressionPath.node.property)) {
185
- const name = memberExpressionPath.node.property.name;
186
- if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
187
- callExpressionPaths[name] = memberExpressionPath.parentPath;
188
- }
189
- }
190
- }
191
- });
192
- if (callExpressionPaths.validator) {
193
- const innerInputExpression = callExpressionPaths.validator.node.arguments[0];
194
- if (!innerInputExpression) {
195
- throw new Error(
196
- "createServerFn().validator() must be called with a validator!"
197
- );
198
- }
199
- if (opts.env === "client" && !shouldValidateClient && t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
200
- callExpressionPaths.validator.replaceWith(
201
- callExpressionPaths.validator.node.callee.object
202
- );
203
- }
204
- }
205
- const handlerFnPath = callExpressionPaths.handler?.get(
206
- "arguments.0"
207
- );
208
- if (!callExpressionPaths.handler || !handlerFnPath.node) {
209
- throw codeFrameError(
210
- opts.code,
211
- path.node.callee.loc,
212
- `createServerFn must be called with a "handler" property!`
213
- );
214
- }
215
- const handlerFn = handlerFnPath.node;
216
- if (t.isIdentifier(handlerFn)) {
217
- if (opts.env === "client") {
218
- const binding = handlerFnPath.scope.getBinding(handlerFn.name);
219
- if (binding) {
220
- binding.path.remove();
221
- }
222
- }
223
- }
224
- handlerFnPath.replaceWith(
225
- t.arrowFunctionExpression(
226
- [t.identifier("opts"), t.identifier("signal")],
227
- t.blockStatement(
228
- // Everything in here is server-only, since the client
229
- // will strip out anything in the 'use server' directive.
230
- [
231
- t.returnStatement(
232
- t.callExpression(
233
- t.identifier(`${existingVariableName}.__executeServer`),
234
- [t.identifier("opts"), t.identifier("signal")]
235
- )
236
- )
237
- ],
238
- [t.directive(t.directiveLiteral("use server"))]
239
- )
240
- )
241
- );
242
- if (opts.env === "server") {
243
- callExpressionPaths.handler.node.arguments.push(handlerFn);
244
- }
245
- }
246
- function handleCreateMiddlewareCallExpression(path, opts) {
247
- const rootCallExpression = getRootCallExpression(path);
248
- const callExpressionPaths = {
249
- middleware: null,
250
- validator: null,
251
- client: null,
252
- server: null
253
- };
254
- const validMethods = Object.keys(callExpressionPaths);
255
- rootCallExpression.traverse({
256
- MemberExpression(memberExpressionPath) {
257
- if (t.isIdentifier(memberExpressionPath.node.property)) {
258
- const name = memberExpressionPath.node.property.name;
259
- if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
260
- callExpressionPaths[name] = memberExpressionPath.parentPath;
261
- }
262
- }
263
- }
264
- });
265
- if (callExpressionPaths.validator) {
266
- const innerInputExpression = callExpressionPaths.validator.node.arguments[0];
267
- if (!innerInputExpression) {
268
- throw new Error(
269
- "createMiddleware().validator() must be called with a validator!"
270
- );
271
- }
272
- if (opts.env === "client") {
273
- if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
274
- callExpressionPaths.validator.replaceWith(
275
- callExpressionPaths.validator.node.callee.object
276
- );
277
- }
278
- }
279
- }
280
- const serverFnPath = callExpressionPaths.server?.get(
281
- "arguments.0"
282
- );
283
- if (callExpressionPaths.server && serverFnPath.node && opts.env === "client") {
284
- if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {
285
- callExpressionPaths.server.replaceWith(
286
- callExpressionPaths.server.node.callee.object
287
- );
288
- }
289
- }
290
- }
291
- function buildEnvOnlyCallExpressionHandler(env) {
292
- return function envOnlyCallExpressionHandler(path, opts) {
293
- const isEnvMatch = env === "client" ? opts.env === "client" : opts.env === "server";
294
- if (isEnvMatch) {
295
- const innerInputExpression = path.node.arguments[0];
296
- if (!t.isExpression(innerInputExpression)) {
297
- throw new Error(
298
- `${env}Only() functions must be called with a function!`
299
- );
300
- }
301
- path.replaceWith(innerInputExpression);
302
- return;
303
- }
304
- path.replaceWith(
305
- t.arrowFunctionExpression(
306
- [],
307
- t.blockStatement([
308
- t.throwStatement(
309
- t.newExpression(t.identifier("Error"), [
310
- t.stringLiteral(
311
- `${env}Only() functions can only be called on the ${env}!`
312
- )
313
- ])
314
- )
315
- ])
316
- )
317
- );
318
- };
319
- }
320
- function handleCreateIsomorphicFnCallExpression(path, opts) {
321
- const rootCallExpression = getRootCallExpression(path);
322
- const callExpressionPaths = {
323
- client: null,
324
- server: null
325
- };
326
- const validMethods = Object.keys(callExpressionPaths);
327
- rootCallExpression.traverse({
328
- MemberExpression(memberExpressionPath) {
329
- if (t.isIdentifier(memberExpressionPath.node.property)) {
330
- const name = memberExpressionPath.node.property.name;
331
- if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
332
- callExpressionPaths[name] = memberExpressionPath.parentPath;
333
- }
334
- }
335
- }
336
- });
337
- if (validMethods.every(
338
- (method) => !callExpressionPaths[method]
339
- )) {
340
- const variableId = rootCallExpression.parentPath.isVariableDeclarator() ? rootCallExpression.parentPath.node.id : null;
341
- console.warn(
342
- "createIsomorphicFn called without a client or server implementation!",
343
- "This will result in a no-op function.",
344
- "Variable name:",
345
- t.isIdentifier(variableId) ? variableId.name : "unknown"
346
- );
347
- }
348
- const envCallExpression = callExpressionPaths[opts.env];
349
- if (!envCallExpression) {
350
- rootCallExpression.replaceWith(
351
- t.arrowFunctionExpression([], t.blockStatement([]))
352
- );
353
- return;
354
- }
355
- const innerInputExpression = envCallExpression.node.arguments[0];
356
- if (!t.isExpression(innerInputExpression)) {
357
- throw new Error(
358
- `createIsomorphicFn().${opts.env}(func) must be called with a function!`
359
- );
360
- }
361
- rootCallExpression.replaceWith(innerInputExpression);
362
- }
363
- function getRootCallExpression(path) {
364
- let rootCallExpression = path;
365
- while (rootCallExpression.parentPath.isMemberExpression()) {
366
- const parent = rootCallExpression.parentPath;
367
- if (parent.parentPath.isCallExpression()) {
368
- rootCallExpression = parent.parentPath;
369
- }
370
- }
371
- return rootCallExpression;
372
- }
373
- function codeFrameError(code, loc, message) {
374
- const frame = codeFrameColumns(
375
- code,
376
- {
377
- start: loc.start,
378
- end: loc.end
379
- },
380
- {
381
- highlightCode: true,
382
- message
383
- }
384
- );
385
- return new Error(frame);
386
- }
387
134
  export {
388
- compileStartOutputFactory,
389
- getRootCallExpression,
390
- handleClientOnlyCallExpression,
391
- handleCreateIsomorphicFnCallExpression,
392
- handleCreateMiddlewareCallExpression,
393
- handleCreateServerFnCallExpression,
394
- handleServerOnlyCallExpression
135
+ compileStartOutputFactory
395
136
  };
396
137
  //# sourceMappingURL=compilers.js.map
@@ -1 +1 @@
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'\nimport { codeFrameColumns } from '@babel/code-frame'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport { transformFuncs } from './constants'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\nconst getIdentifiers = (\n framework: CompileStartFrameworkOptions,\n): Identifiers => ({\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\n serverOnly: {\n name: 'serverOnly',\n handleCallExpression: handleServerOnlyCallExpression,\n paths: [],\n },\n clientOnly: {\n name: 'clientOnly',\n handleCallExpression: handleClientOnlyCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n})\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\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 babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers = getIdentifiers(framework)\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\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\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 // 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 === identifiers[identifierKey].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(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\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 === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].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\nfunction handleCreateServerFileRouteCallExpressionFactory(\n framework: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${framework}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n\n// build these once and reuse them\nexport const handleServerOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleClientOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\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\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // The function is the 'fn' property of the object passed to createServerFn\n\n // const firstArg = path.node.arguments[0]\n // if (t.isObjectExpression(firstArg)) {\n // // Was called with some options\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 calledOptions = path.node.arguments[0]\n ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)\n : null\n\n const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {\n return (\n t.isObjectProperty(prop) &&\n t.isIdentifier(prop.key) &&\n prop.key.name === 'validateClient' &&\n t.isBooleanLiteral(prop.value) &&\n prop.value.value === true\n )\n })\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n handler: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\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\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 (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, and we're not validating the client, remove the validator call expression\n if (\n opts.env === 'client' &&\n !shouldValidateClient &&\n t.isMemberExpression(callExpressionPaths.validator.node.callee)\n ) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\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\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\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 (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().validator() 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 (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\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 `${env}Only() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\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\nexport function getRootCallExpression(path: babel.NodePath<t.CallExpression>) {\n // Find the highest callExpression parent\n let rootCallExpression: babel.NodePath<t.CallExpression> = path\n\n // Traverse up the chain of CallExpressions\n while (rootCallExpression.parentPath.isMemberExpression()) {\n const parent = rootCallExpression.parentPath\n if (parent.parentPath.isCallExpression()) {\n rootCallExpression = parent.parentPath\n }\n }\n\n return rootCallExpression\n}\n\nfunction codeFrameError(\n code: string,\n loc: {\n start: { line: number; column: number }\n end: { line: number; column: number }\n },\n message: string,\n) {\n const frame = codeFrameColumns(\n code,\n {\n start: loc.start,\n end: loc.end,\n },\n {\n highlightCode: true,\n message,\n },\n )\n\n return new Error(frame)\n}\n"],"names":[],"mappings":";;;;;;AAeA,MAAM,iBAAiB,CACrB,eACiB;AAAA,EACjB,uBAAuB;AAAA,IACrB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,mBAAmB;AAAA,IACjB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,uBAAuB;AAAA,IACrB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,gBAAgB;AAAA,IACd,MAAM;AAAA,IACN,sBAAsB;AAAA,IACtB,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,kBAAkB;AAAA,IAChB,MAAM;AAAA,IACN,sBAAsB;AAAA,IACtB,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,YAAY;AAAA,IACV,MAAM;AAAA,IAC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A,IAAA;AAAA,IAEX;AAAA,MACE,eAAe;AAAA,MACf;AAAA,IAAA;AAAA,EACF;AAGF,SAAO,IAAI,MAAM,KAAK;AACxB;"}
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 { handleCreateServerFileRouteCallExpressionFactory } from './serverFileRoute'\nimport { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'\nimport { handleCreateMiddlewareCallExpression } from './middleware'\nimport { handleCreateServerFnCallExpression } from './serverFn'\nimport {\n handleCreateClientOnlyFnCallExpression,\n handleCreateServerOnlyFnCallExpression,\n} from './envOnly'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\ntype Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }\nconst getIdentifiers = (\n framework: CompileStartFrameworkOptions,\n): Identifiers => ({\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\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\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\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 babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers = getIdentifiers(framework)\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\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\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 // 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 === identifiers[identifierKey].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(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].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 === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n transformFuncs.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].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":";;;;;;;;;;AAsBA,MAAM,iBAAiB,CACrB,eACiB;AAAA,EACjB,uBAAuB;AAAA,IACrB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,mBAAmB;AAAA,IACjB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,uBAAuB;AAAA,IACrB,MAAM;AAAA,IACN,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,OAAO,CAAA;AAAA,EAAC;AAAA,EAEV,gBAAgB;AAAA,IACd,MAAM;AAAA,IACN,sBAAsB;AAAA,I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", "clientOnly", "createIsomorphicFn", "createServerRoute", "createServerFileRoute", "createServerRootRoute"];
1
+ export declare const transformFuncs: readonly ["createServerFn", "createMiddleware", "createServerOnlyFn", "createClientOnlyFn", "createIsomorphicFn", "createServerRoute", "createServerFileRoute", "createServerRootRoute"];
@@ -1,8 +1,8 @@
1
1
  const transformFuncs = [
2
2
  "createServerFn",
3
3
  "createMiddleware",
4
- "serverOnly",
5
- "clientOnly",
4
+ "createServerOnlyFn",
5
+ "createClientOnlyFn",
6
6
  "createIsomorphicFn",
7
7
  "createServerRoute",
8
8
  "createServerFileRoute",
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerFn',\n 'createMiddleware',\n 'serverOnly',\n 'clientOnly',\n 'createIsomorphicFn',\n 'createServerRoute',\n 'createServerFileRoute',\n 'createServerRootRoute',\n] as const\n"],"names":[],"mappings":"AAAO,MAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;"}
1
+ {"version":3,"file":"constants.js","sources":["../../../src/start-compiler-plugin/constants.ts"],"sourcesContent":["export const transformFuncs = [\n 'createServerFn',\n 'createMiddleware',\n 'createServerOnlyFn',\n 'createClientOnlyFn',\n 'createIsomorphicFn',\n 'createServerRoute',\n 'createServerFileRoute',\n 'createServerRootRoute',\n] as const\n"],"names":[],"mappings":"AAAO,MAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;"}
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1,41 @@
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
@@ -0,0 +1 @@
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;"}
@@ -0,0 +1,4 @@
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;
@@ -0,0 +1,49 @@
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
@@ -0,0 +1 @@
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;"}
@@ -0,0 +1,4 @@
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 handleCreateMiddlewareCallExpression(path: babel.NodePath<t.CallExpression>, opts: CompileOptions): void;
@@ -0,0 +1,51 @@
1
+ import * as t from "@babel/types";
2
+ import { getRootCallExpression } from "./utils.js";
3
+ function handleCreateMiddlewareCallExpression(path, opts) {
4
+ const rootCallExpression = getRootCallExpression(path);
5
+ const callExpressionPaths = {
6
+ middleware: null,
7
+ validator: null,
8
+ client: null,
9
+ server: null
10
+ };
11
+ const validMethods = Object.keys(callExpressionPaths);
12
+ rootCallExpression.traverse({
13
+ MemberExpression(memberExpressionPath) {
14
+ if (t.isIdentifier(memberExpressionPath.node.property)) {
15
+ const name = memberExpressionPath.node.property.name;
16
+ if (validMethods.includes(name) && memberExpressionPath.parentPath.isCallExpression()) {
17
+ callExpressionPaths[name] = memberExpressionPath.parentPath;
18
+ }
19
+ }
20
+ }
21
+ });
22
+ if (callExpressionPaths.validator) {
23
+ const innerInputExpression = callExpressionPaths.validator.node.arguments[0];
24
+ if (!innerInputExpression) {
25
+ throw new Error(
26
+ "createMiddleware().validator() must be called with a validator!"
27
+ );
28
+ }
29
+ if (opts.env === "client") {
30
+ if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {
31
+ callExpressionPaths.validator.replaceWith(
32
+ callExpressionPaths.validator.node.callee.object
33
+ );
34
+ }
35
+ }
36
+ }
37
+ const serverFnPath = callExpressionPaths.server?.get(
38
+ "arguments.0"
39
+ );
40
+ if (callExpressionPaths.server && serverFnPath.node && opts.env === "client") {
41
+ if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {
42
+ callExpressionPaths.server.replaceWith(
43
+ callExpressionPaths.server.node.callee.object
44
+ );
45
+ }
46
+ }
47
+ }
48
+ export {
49
+ handleCreateMiddlewareCallExpression
50
+ };
51
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sources":["../../../src/start-compiler-plugin/middleware.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 handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\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 (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().validator() 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 (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n"],"names":[],"mappings":";;AAMO,SAAS,qCACd,MACA,MACA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,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,MAAI,oBAAoB,WAAW;AACjC,UAAM,uBAAuB,oBAAoB,UAAU,KAAK,UAAU,CAAC;AAE3E,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QAAI,KAAK,QAAQ,UAAU;AACzB,UAAI,EAAE,mBAAmB,oBAAoB,UAAU,KAAK,MAAM,GAAG;AACnE,4BAAoB,UAAU;AAAA,UAC5B,oBAAoB,UAAU,KAAK,OAAO;AAAA,QAAA;AAAA,MAE9C;AAAA,IACF;AAAA,EACF;AAEA,QAAM,eAAe,oBAAoB,QAAQ;AAAA,IAC/C;AAAA,EAAA;AAGF,MACE,oBAAoB,UACpB,aAAa,QACb,KAAK,QAAQ,UACb;AAEA,QAAI,EAAE,mBAAmB,oBAAoB,OAAO,KAAK,MAAM,GAAG;AAChE,0BAAoB,OAAO;AAAA,QACzB,oBAAoB,OAAO,KAAK,OAAO;AAAA,MAAA;AAAA,IAE3C;AAAA,EACF;AACF;"}