Skip to content

Commit

Permalink
fix: move vinxi config to start
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed May 14, 2024
1 parent 569a1c6 commit 00abc36
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 45 deletions.
13 changes: 12 additions & 1 deletion packages/start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
"default": "./dist/cjs/client/index.cjs"
}
},
"./config": {
"import": {
"_types": "./src/config/index.d.ts",
"default": "./src/config/index.js"
}
},
"./client": {
"import": {
"types": "./dist/esm/client/index.d.ts",
Expand Down Expand Up @@ -105,10 +111,15 @@
"dependencies": {
"@tanstack/react-cross-context": "workspace:*",
"@tanstack/react-router": "workspace:*",
"@tanstack/router-vite-plugin": "workspace:*",
"@types/jsesc": "^3.0.3",
"@vinxi/react": "0.2.2",
"@vinxi/server-functions": "^0.3.2",
"import-meta-resolve": "^4.0.0",
"jsesc": "^3.0.2",
"tiny-invariant": "^1.3.1",
"vinxi": "0.2.1"
"vinxi": "0.3.11",
"vite-tsconfig-paths": "^4.3.1"
},
"devDependencies": {
"@testing-library/react": "^15.0.2",
Expand Down
18 changes: 11 additions & 7 deletions packages/start/src/client/DehydrateRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { Context } from '@tanstack/react-cross-context'
import { useRouter } from '@tanstack/react-router'
import jsesc from 'jsesc'
import * as React from 'react'

export function DehydrateRouter() {
const router = useRouter()
Expand All @@ -10,13 +10,17 @@ export function DehydrateRouter() {
Context.get('TanStackRouterHydrationContext', {}),
)

const dehydrated = router.dehydratedData || dehydratedCtx
let stringified = ''

if (router.isServer) {
const dehydrated = router.dehydratedData || dehydratedCtx

// Use jsesc to escape the stringified JSON for use in a script tag
const stringified = jsesc(router.options.transformer.stringify(dehydrated), {
isScriptContext: true,
wrap: true,
})
// Use jsesc to escape the stringified JSON for use in a script tag
stringified = jsesc(router.options.transformer.stringify(dehydrated), {
isScriptContext: true,
wrap: true,
})
}

return (
<script
Expand Down
24 changes: 12 additions & 12 deletions packages/start/src/client/Meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ export const Meta = ({ children }: { children?: React.ReactNode }) => {
export function Html({ children, ...props }: React.HTMLProps<HTMLHtmlElement>) {
const router = useRouter()

warning(
!Object.keys(props).length,
'Passing props other than children to the Html component will be supported very soon in React 19.',
)
// warning(
// !Object.keys(props).length,
// 'Passing props other than children to the Html component will be supported very soon in React 19.',
// )

if (!router.isServer) {
return <>{children}</>
Expand All @@ -166,10 +166,10 @@ export function Html({ children, ...props }: React.HTMLProps<HTMLHtmlElement>) {
export function Head({ children, ...props }: React.HTMLProps<HTMLHeadElement>) {
const router = useRouter()

warning(
!Object.keys(props).length,
'Passing props other than children to the Head component will be supported very soon in React 19.',
)
// warning(
// !Object.keys(props).length,
// 'Passing props other than children to the Head component will be supported very soon in React 19.',
// )

if (!router.isServer) {
return <Meta>{children}</Meta>
Expand All @@ -185,10 +185,10 @@ export function Head({ children, ...props }: React.HTMLProps<HTMLHeadElement>) {
export function Body({ children, ...props }: React.HTMLProps<HTMLBodyElement>) {
const router = useRouter()

warning(
!Object.keys(props).length,
'Passing props other than children to the Body component will be supported very soon in React 19.',
)
// warning(
// !Object.keys(props).length,
// 'Passing props other than children to the Body component will be supported very soon in React 19.',
// )

if (!router.isServer) {
return children
Expand Down
99 changes: 99 additions & 0 deletions packages/start/src/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// @ts-nocheck
import path from 'path'
import { fileURLToPath } from 'url'
import { createApp } from 'vinxi'
import reactRefresh from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'
import { resolve } from 'import-meta-resolve'
import { normalize } from 'vinxi/lib/path'
import { TanStackRouterVite } from '@tanstack/router-vite-plugin'
import { serverFunctions } from '@vinxi/server-functions/plugin'
import { serverTransform } from '@vinxi/server-functions/server'
import { config } from 'vinxi/plugins/config'
// import type * as vite from 'vite'
// import { config } from 'vinxi/plugins/config'

function startVite() {
return config('start-vite', {})
}

export function defineConfig(opts) {
return createApp({
server: {
preset: 'vercel',
experimental: {
asyncStorage: true,
asyncContext: true,
},
},
routers: [
{
name: 'public',
type: 'static',
dir: './public',
base: '/',
},
{
name: 'ssr',
type: 'http',
handler: './app/server.tsx',
target: 'server',
plugins: () => [
startVite(),
opts.vite?.(),
TanStackRouterVite({
experimental: {
enableCodeSplitting: false,
},
}),
tsconfigPaths(),
serverTransform({
runtime: resolveRelativePath('../server/server-runtime.tsx'),
}),
],
link: {
client: 'client',
},
},
{
name: 'client',
type: 'client',
handler: './app/client.tsx',
target: 'browser',
base: '/_build',
build: {
sourcemap: true,
},
plugins: () => [
TanStackRouterVite({
experimental: {
enableCodeSplitting: false,
},
}),
startVite(),
opts.vite?.(),
tsconfigPaths(),
serverFunctions.client({
runtime: resolveRelativePath('../client/client-runtime.tsx'),
}),
reactRefresh(),
],
},
serverFunctions.router({
name: 'server',
plugins: () => [startVite(), opts.vite?.(), tsconfigPaths()],
handler: resolveRelativePath('../server/server-handler.tsx'),
runtime: resolveRelativePath('../server/server-runtime.tsx'),
}),
],
})
}

function resolveRelativePath(p) {
return path.relative(
process.cwd(),
resolve(p, import.meta.url)
.split('://')
.at(-1),
)
}
7 changes: 5 additions & 2 deletions packages/start/src/server-fns/fetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import {
isPlainObject,
isRedirect,
} from '@tanstack/react-router'
import { serverFnPayloadTypeHeader, serverFnReturnTypeHeader } from '../client'
import type { CompiledFetcherFnOptions } from '../client'
import {
serverFnPayloadTypeHeader,
serverFnReturnTypeHeader,
} from '@tanstack/start'
import type { CompiledFetcherFnOptions } from '@tanstack/start'

export async function fetcher<TPayload>(
base: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/start/src/server/server-runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createServerReference<TPayload, TResponse>(
}
})

setResponseHeaders(event, ogResponseHeaders)
setResponseHeaders(event, ogResponseHeaders as any)
}

return response
Expand Down
45 changes: 23 additions & 22 deletions packages/start/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ const config = defineConfig({
plugins: [
react(),
// @ts-ignore
replace({
'import.meta.env': '__import__meta__env__',
}),
(() => {
return {
name: 'replace-import-meta-env',
writeBundle(options, bundle) {
const basePath = options.dir || path.dirname(options.file || '')
// replace({
// 'import.meta.env': '__import__meta__env__',
// }),
// (() => {
// return {
// name: 'replace-import-meta-env',
// writeBundle(options, bundle) {
// const basePath = options.dir || path.dirname(options.file || '')

for (const [fileName, fileEntry] of Object.entries(bundle)) {
if (fileEntry.type === 'chunk') {
const fullPath = path.resolve(basePath, fileName)
fs.writeFileSync(
fullPath,
fs
.readFileSync(fullPath, 'utf-8')
.replace(/__import__meta__env__/g, 'import.meta.env'),
)
}
}
},
}
})(),
// for (const [fileName, fileEntry] of Object.entries(bundle)) {
// if (fileEntry.type === 'chunk') {
// const fullPath = path.resolve(basePath, fileName)
// fs.writeFileSync(
// fullPath,
// fs
// .readFileSync(fullPath, 'utf-8')
// .replace(/__import__meta__env__/g, 'import.meta.env'),
// )
// }
// }
// },
// }
// })(),
],
})

Expand All @@ -40,6 +40,7 @@ export default mergeConfig(
config,
tanstackBuildConfig({
entry: [
// './src/config/index.ts',
'./src/client/index.tsx',
'./src/server/index.tsx',
'./src/client/client-runtime.tsx',
Expand Down

0 comments on commit 00abc36

Please sign in to comment.