Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat server fn plugin #2993

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pnpm-lock.yaml
node_modules

**/test-results
**/tests/generator/file-modification/routes/(test)/*
**/tests/generator/file-modification/routes/(test)/*
/.nx/workspace-data
4 changes: 2 additions & 2 deletions examples/react/start-basic/app/routes/deferred.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { createServerFn } from '@tanstack/start'
import { Suspense, useState } from 'react'

const personServerFn = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(({ data: name }) => {
return { name, randomNumber: Math.floor(Math.random() * 100) }
})

const slowServerFn = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data: name }) => {
await new Promise((r) => setTimeout(r, 1000))
return { name, randomNumber: Math.floor(Math.random() * 100) }
Expand Down
2 changes: 1 addition & 1 deletion examples/react/start-basic/app/utils/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type PostType = {

export const fetchPost = createServerFn({ method: 'GET' })
.middleware([logMiddleware])
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data }) => {
console.info(`Fetching post with id ${data}...`)
const post = await axios
Expand Down
2 changes: 1 addition & 1 deletion examples/react/start-supabase-basic/app/utils/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type PostType = {
}

export const fetchPost = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data: postId }) => {
console.info(`Fetching post with id ${postId}...`)
const post = await axios
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@
"@tanstack/arktype-adapter": "workspace:*",
"@tanstack/start": "workspace:*",
"@tanstack/start-vite-plugin": "workspace:*",
"@tanstack/eslint-plugin-router": "workspace:*",
"temp-react": "0.0.0-experimental-035a41c4e-20230704",
"temp-react-dom": "0.0.0-experimental-035a41c4e-20230704"
"@tanstack/eslint-plugin-router": "workspace:*"
}
}
}
2 changes: 2 additions & 0 deletions packages/router-plugin/src/core/code-splitter/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export function compileCodeSplitReferenceRoute(opts: ParseAstOptions) {
return generate(ast, {
sourceMaps: true,
sourceFileName: opts.filename,
minified: process.env.NODE_ENV === 'production',
})
}

Expand Down Expand Up @@ -543,6 +544,7 @@ export function compileCodeSplitVirtualRoute(opts: ParseAstOptions) {
return generate(ast, {
sourceMaps: true,
sourceFileName: opts.filename,
minified: process.env.NODE_ENV === 'production',
})
}

Expand Down
5 changes: 5 additions & 0 deletions packages/server-functions-vite-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<img src="https://static.scarf.sh/a.png?x-pxid=d988eb79-b0fc-4a2b-8514-6a1ab932d188" />

# TanStack Start Vite Plugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this was copy / pasted and needs to be updated


See https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing
5 changes: 5 additions & 0 deletions packages/server-functions-vite-plugin/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-check

import rootConfig from '../../eslint.config.js'

export default [...rootConfig]
89 changes: 89 additions & 0 deletions packages/server-functions-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"name": "@tanstack/directive-functions-plugin",
"version": "1.87.3",
"description": "Modern and scalable routing for React applications",
"author": "Tanner Linsley",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/TanStack/router.git",
"directory": "packages/directive-functions-plugin"
},
"homepage": "https://tanstack.com/start",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"keywords": [
"react",
"location",
"router",
"routing",
"async",
"async router",
"typescript"
],
"scripts": {
"clean": "rimraf ./dist && rimraf ./coverage",
"test": "pnpm test:eslint && pnpm test:types && pnpm test:build && pnpm test:unit",
"test:unit": "vitest",
"test:unit:dev": "vitest --watch",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js",
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js",
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js",
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js",
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js",
"test:types:ts57": "tsc",
"test:build": "publint --strict && attw --ignore-rules no-resolution --pack .",
"build": "vite build"
},
"type": "module",
"types": "dist/esm/index.d.ts",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"dist",
"src"
],
"engines": {
"node": ">=12"
},
"dependencies": {
"@babel/code-frame": "7.26.2",
"@babel/core": "^7.26.0",
"@babel/generator": "^7.26.3",
"@babel/parser": "^7.26.3",
"@babel/plugin-syntax-jsx": "^7.25.9",
"@babel/plugin-syntax-typescript": "^7.25.9",
"@babel/template": "^7.25.9",
"@babel/traverse": "^7.26.4",
"@babel/types": "^7.26.3",
"@types/babel__code-frame": "^7.0.6",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__template": "^7.4.4",
"@types/babel__traverse": "^7.20.6",
"@types/diff": "^6.0.0",
"babel-dead-code-elimination": "^1.0.6",
"chalk": "^5.3.0",
"dedent": "^1.5.3",
"diff": "^7.0.0",
"tiny-invariant": "^1.3.3"
}
}
20 changes: 20 additions & 0 deletions packages/server-functions-vite-plugin/src/ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { parse } from '@babel/parser'
import type { ParseResult } from '@babel/parser'

export type ParseAstOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should really move this thing into a common package. we have this everywhere...

code: string
filename: string
root: string
}

export function parseAst(opts: ParseAstOptions): ParseResult<babel.types.File> {
return parse(opts.code, {
plugins: ['jsx', 'typescript'],
sourceType: 'module',
...{
root: opts.root,
filename: opts.filename,
sourceMaps: true,
},
})
}
Loading
Loading