Skip to content

Commit

Permalink
fix: rename server$ to serverFn$, update docs with coming soon utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Feb 28, 2023
1 parent b900b3c commit f91e43a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions examples/astro-basic/src/app/root.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -14,9 +14,9 @@ export function App() {
<Scripts />
</body>
</html>
);
)
}

function Scripts() {
return <script type="module" src="/src/app/entry-client.tsx"></script>;
return <script type="module" src="/src/app/entry-client.tsx"></script>
}
16 changes: 8 additions & 8 deletions packages/bling/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
}
Expand Down Expand Up @@ -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()
)
Expand Down
5 changes: 4 additions & 1 deletion packages/bling/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ const serverMethods: CreateClientFetcherMethods = {
},
}

export const server$: ClientServerFn = Object.assign(serverImpl, serverMethods)
export const serverFn$: ClientServerFn = Object.assign(
serverImpl,
serverMethods
)
4 changes: 2 additions & 2 deletions packages/bling/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion packages/bling/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit f91e43a

Please sign in to comment.