From f91e43ab7ecdaa6a3f5f38f1d146a337fe2125b5 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Tue, 28 Feb 2023 13:31:22 -0700 Subject: [PATCH] fix: rename server$ to serverFn$, update docs with coming soon utils --- examples/astro-basic/src/app/root.tsx | 8 ++++---- packages/bling/src/babel.ts | 16 ++++++++-------- packages/bling/src/client.ts | 5 ++++- packages/bling/src/server.ts | 4 ++-- packages/bling/src/vite.ts | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/astro-basic/src/app/root.tsx b/examples/astro-basic/src/app/root.tsx index 33c6c50..3cb5598 100644 --- a/examples/astro-basic/src/app/root.tsx +++ b/examples/astro-basic/src/app/root.tsx @@ -1,6 +1,6 @@ -import { server$ } from "@tanstack/bling"; +import { serverFn$ } from '@tanstack/bling' -const sayHello = server$(() => console.log("Hello world")); +const sayHello = serverFn$(() => console.log('Hello world')) export function App() { return ( @@ -14,9 +14,9 @@ export function App() { - ); + ) } function Scripts() { - return ; + return } diff --git a/packages/bling/src/babel.ts b/packages/bling/src/babel.ts index 2b3e466..1d3c960 100644 --- a/packages/bling/src/babel.ts +++ b/packages/bling/src/babel.ts @@ -2,7 +2,7 @@ // All credit for this work goes to the amazing Next.js team. // https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts -// This is adapted to work with any server$() calls and transpile it into multiple api function for a file. +// This is adapted to work with any serverFn$() calls and transpile it into multiple api function for a file. import crypto from 'crypto' import nodePath from 'path' @@ -113,7 +113,7 @@ function transformServer({ types: t, template }) { CallExpression: (path) => { if ( path.node.callee.type === 'Identifier' && - path.node.callee.name === 'server$' + path.node.callee.name === 'serverFn$' ) { const serverFn = path.get('arguments')[0] const serverFnOpts = path.get('arguments')[1] @@ -140,7 +140,7 @@ function transformServer({ types: t, template }) { let obj = path.get('object') if ( obj.node.type === 'Identifier' && - obj.node.name === 'server$' + obj.node.name === 'serverFn$' ) { obj.replaceWith(t.identifier('$$ctx')) return @@ -197,8 +197,8 @@ function transformServer({ types: t, template }) { if (state.opts.ssr) { statement.insertBefore( template(` - const $$server_module${serverIndex} = server$.createHandler(%%source%%, "${pathname}", %%options%%); - server$.registerHandler("${pathname}", $$server_module${serverIndex}); + const $$server_module${serverIndex} = serverFn$.createHandler(%%source%%, "${pathname}", %%options%%); + serverFn$.registerHandler("${pathname}", $$server_module${serverIndex}); `)({ source: serverFn.node, options: @@ -211,10 +211,10 @@ function transformServer({ types: t, template }) { ` ${ process.env.TEST_ENV === 'client' - ? `server$.registerHandler("${pathname}", server$.createHandler(%%source%%, "${pathname}", %%options%%));` + ? `serverFn$.registerHandler("${pathname}", serverFn$.createHandler(%%source%%, "${pathname}", %%options%%));` : `` } - const $$server_module${serverIndex} = server$.createFetcher("${pathname}", %%options%%);`, + const $$server_module${serverIndex} = serverFn$.createFetcher("${pathname}", %%options%%);`, { syntacticPlaceholders: true, } @@ -242,7 +242,7 @@ function transformServer({ types: t, template }) { ArrowFunctionExpression: markFunction, ImportSpecifier: function (path, state) { // Rewrite imports to `@tanstack/bling` to `@tanstack/bling/server` during SSR - if (state.opts.ssr && path.node.imported.name === 'server$') { + if (state.opts.ssr && path.node.imported.name === 'serverFn$') { const importDeclaration = path.findParent((p) => p.isImportDeclaration() ) diff --git a/packages/bling/src/client.ts b/packages/bling/src/client.ts index c40d68a..b29dd86 100644 --- a/packages/bling/src/client.ts +++ b/packages/bling/src/client.ts @@ -103,4 +103,7 @@ const serverMethods: CreateClientFetcherMethods = { }, } -export const server$: ClientServerFn = Object.assign(serverImpl, serverMethods) +export const serverFn$: ClientServerFn = Object.assign( + serverImpl, + serverMethods +) diff --git a/packages/bling/src/server.ts b/packages/bling/src/server.ts index 58b5429..59fafb2 100644 --- a/packages/bling/src/server.ts +++ b/packages/bling/src/server.ts @@ -116,7 +116,7 @@ const serverMethods: ServerFetcherMethods = { }, } -export const server$: ServerFn = Object.assign(serverImpl, serverMethods) +export const serverFn$: ServerFn = Object.assign(serverImpl, serverMethods) export async function handleEvent(ctx: ServerFnCtxWithRequest) { if (!ctx.request) { @@ -286,4 +286,4 @@ export function hasHandler(pathname: string) { // used to fetch from an API route on the server or client, without falling into // fetch problems on the server -// server$.fetch = fetch +// serverFn$.fetch = fetch diff --git a/packages/bling/src/vite.ts b/packages/bling/src/vite.ts index 22dae25..a90ac88 100644 --- a/packages/bling/src/vite.ts +++ b/packages/bling/src/vite.ts @@ -47,7 +47,7 @@ export function bling(opts?: { babel?: Options['babel'] }): Plugin { let ssr = process.env.TEST_ENV === 'client' ? false : isSsr - if (code.includes('server$(')) { + if (code.includes('serverFn$(')) { return compiler( code, id.replace(/\.ts$/, '.tsx').replace(/\.js$/, '.jsx'),