Skip to content

Commit

Permalink
refactor(start): update app directory handling in defineConfig functi…
Browse files Browse the repository at this point in the history
…on (#2617)

Co-authored-by: Sean Cassiere <[email protected]>
  • Loading branch information
logan-anderson and SeanCassiere authored Oct 24, 2024
1 parent 40c1541 commit caae48c
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 12 deletions.
24 changes: 24 additions & 0 deletions e2e/start/basic-tsr-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
node_modules
package-lock.json
yarn.lock

.DS_Store
.cache
.env
.vercel
.output
.vinxi

/build/
/api/
/server/build
/public/build
.vinxi
# Sentry Config File
.env.sentry-build-plugin
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

count.txt
4 changes: 4 additions & 0 deletions e2e/start/basic-tsr-config/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/build
**/public
pnpm-lock.yaml
routeTree.gen.ts
72 changes: 72 additions & 0 deletions e2e/start/basic-tsr-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Welcome to TanStack.com!

This site is built with TanStack Router!

- [TanStack Router Docs](https://tanstack.com/router)

It's deployed automagically with Vercel!

- [Vercel](https://vercel.com/)

## Development

From your terminal:

```sh
pnpm install
pnpm dev
```

This starts your app in development mode, rebuilding assets on file changes.

## Editing and previewing the docs of TanStack projects locally

The documentations for all TanStack projects except for `React Charts` are hosted on [https://tanstack.com](https://tanstack.com), powered by this TanStack Router app.
In production, the markdown doc pages are fetched from the GitHub repos of the projects, but in development they are read from the local file system.

Follow these steps if you want to edit the doc pages of a project (in these steps we'll assume it's [`TanStack/form`](https://github.com/tanstack/form)) and preview them locally :

1. Create a new directory called `tanstack`.

```sh
mkdir tanstack
```

2. Enter the directory and clone this repo and the repo of the project there.

```sh
cd tanstack
git clone [email protected]:TanStack/tanstack.com.git
git clone [email protected]:TanStack/form.git
```

> [!NOTE]
> Your `tanstack` directory should look like this:
>
> ```
> tanstack/
> |
> +-- form/
> |
> +-- tanstack.com/
> ```
> [!WARNING]
> Make sure the name of the directory in your local file system matches the name of the project's repo. For example, `tanstack/form` must be cloned into `form` (this is the default) instead of `some-other-name`, because that way, the doc pages won't be found.
3. Enter the `tanstack/tanstack.com` directory, install the dependencies and run the app in dev mode:
```sh
cd tanstack.com
pnpm i
# The app will run on https://localhost:3000 by default
pnpm dev
```
4. Now you can visit http://localhost:3000/form/latest/docs/overview in the browser and see the changes you make in `tanstack/form/docs`.

> [!NOTE]
> The updated pages need to be manually reloaded in the browser.
> [!WARNING]
> You will need to update the `docs/config.json` file (in the project's repo) if you add a new doc page!
8 changes: 8 additions & 0 deletions e2e/start/basic-tsr-config/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// app.config.ts
import { defineConfig } from '@tanstack/start/config'

export default defineConfig({
tsr: {
appDirectory: './src/app',
},
})
25 changes: 25 additions & 0 deletions e2e/start/basic-tsr-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "tanstack-start-e2e-basic-tsr-config",
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"dev": "vinxi dev",
"build": "vinxi build",
"start": "vinxi start",
"test:e2e": "playwright test --project=chromium"
},
"dependencies": {
"@tanstack/react-router": "^1.75.0",
"@tanstack/start": "^1.76.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vinxi": "0.4.3"
},
"devDependencies": {
"@types/node": "^22.5.4",
"@types/react": "^18.2.65",
"@types/react-dom": "^18.2.21",
"typescript": "^5.6.2"
}
}
30 changes: 30 additions & 0 deletions e2e/start/basic-tsr-config/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defineConfig, devices } from '@playwright/test'

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',

reporter: [['line']],

use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://localhost:3000/',
},

webServer: {
// TODO: build && start seems broken, use that if it's working
command: 'pnpm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
stdout: 'pipe',
},

projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
})
9 changes: 9 additions & 0 deletions e2e/start/basic-tsr-config/src/app/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// app/client.tsx
/// <reference types="vinxi/types/client" />
import { hydrateRoot } from 'react-dom/client'
import { StartClient } from '@tanstack/start'
import { createRouter } from './router'

const router = createRouter()

hydrateRoot(document.getElementById('root')!, <StartClient router={router} />)
90 changes: 90 additions & 0 deletions e2e/start/basic-tsr-config/src/app/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* prettier-ignore-start */

/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file is auto-generated by TanStack Router

// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as IndexImport } from './routes/index'

// Create/Update Routes

const IndexRoute = IndexImport.update({
id: '/',
path: '/',
getParentRoute: () => rootRoute,
} as any)

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
preLoaderRoute: typeof IndexImport
parentRoute: typeof rootRoute
}
}
}

// Create and export the route tree

export interface FileRoutesByFullPath {
'/': typeof IndexRoute
}

export interface FileRoutesByTo {
'/': typeof IndexRoute
}

export interface FileRoutesById {
__root__: typeof rootRoute
'/': typeof IndexRoute
}

export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/'
fileRoutesByTo: FileRoutesByTo
to: '/'
id: '__root__' | '/'
fileRoutesById: FileRoutesById
}

export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
}

const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
}

export const routeTree = rootRoute
._addFileChildren(rootRouteChildren)
._addFileTypes<FileRouteTypes>()

/* prettier-ignore-end */

/* ROUTE_MANIFEST_START
{
"routes": {
"__root__": {
"filePath": "__root.tsx",
"children": [
"/"
]
},
"/": {
"filePath": "index.tsx"
}
}
}
ROUTE_MANIFEST_END */
17 changes: 17 additions & 0 deletions e2e/start/basic-tsr-config/src/app/router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// app/router.tsx
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'

export function createRouter() {
const router = createTanStackRouter({
routeTree,
})

return router
}

declare module '@tanstack/react-router' {
interface Register {
router: ReturnType<typeof createRouter>
}
}
44 changes: 44 additions & 0 deletions e2e/start/basic-tsr-config/src/app/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// app/routes/__root.tsx
import { createRootRoute } from '@tanstack/react-router'
import { Outlet, ScrollRestoration } from '@tanstack/react-router'
import { Body, Head, Html, Meta, Scripts } from '@tanstack/start'
import * as React from 'react'

export const Route = createRootRoute({
meta: () => [
{
charSet: 'utf-8',
},
{
name: 'viewport',
content: 'width=device-width, initial-scale=1',
},
{
title: 'TanStack Start Starter',
},
],
component: RootComponent,
})

function RootComponent() {
return (
<RootDocument>
<Outlet />
</RootDocument>
)
}

function RootDocument({ children }: { children: React.ReactNode }) {
return (
<Html>
<Head>
<Meta />
</Head>
<Body>
{children}
<ScrollRestoration />
<Scripts />
</Body>
</Html>
)
}
35 changes: 35 additions & 0 deletions e2e/start/basic-tsr-config/src/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// app/routes/index.tsx
import { createFileRoute, useRouter } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/start'

let count = 0

const getCount = createServerFn('GET', () => {
return count
})

const updateCount = createServerFn('POST', async (addBy: number) => {
count += addBy
})

export const Route = createFileRoute('/')({
component: Home,
loader: async () => await getCount(),
})

function Home() {
const router = useRouter()
const state = Route.useLoaderData()

return (
<button
onClick={() => {
updateCount(1).then(() => {
router.invalidate()
})
}}
>
Add 1 to {state}?
</button>
)
}
14 changes: 14 additions & 0 deletions e2e/start/basic-tsr-config/src/app/ssr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// app/ssr.tsx
/// <reference types="vinxi/types/server" />
import {
createStartHandler,
defaultStreamHandler,
} from '@tanstack/start/server'
import { getRouterManifest } from '@tanstack/start/router-manifest'

import { createRouter } from './router'

export default createStartHandler({
createRouter,
getRouterManifest,
})(defaultStreamHandler)
8 changes: 8 additions & 0 deletions e2e/start/basic-tsr-config/tests/app.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from '@playwright/test'

test('opening the app', async ({ page }) => {
await page.goto('/')
await expect(page.getByText('Add 1 to 0?')).toBeTruthy()
await page.click('button')
await expect(page.getByText('Add 1 to 1?')).toBeTruthy()
})
Loading

0 comments on commit caae48c

Please sign in to comment.