Skip to content

Commit 2cac72a

Browse files
committed
feat: code router now supports add ons with routes
1 parent 33b5740 commit 2cac72a

File tree

17 files changed

+158
-51
lines changed

17 files changed

+158
-51
lines changed

src/create-app.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function createTemplateFile(
5050
templateDir: string,
5151
file: string,
5252
targetFileName?: string,
53+
extraTemplateValues?: Record<string, any>,
5354
) {
5455
const templateValues = {
5556
packageManager: options.packageManager,
@@ -68,6 +69,7 @@ function createTemplateFile(
6869
{},
6970
),
7071
addOns: options.chosenAddOns,
72+
...extraTemplateValues,
7173
}
7274

7375
const template = await readFile(resolve(templateDir, file), 'utf-8')
@@ -300,11 +302,6 @@ export async function createApp(options: Required<Options>) {
300302

301303
// Setup the app component. There are four variations, typescript/javascript and tailwind/non-tailwind.
302304
if (options.mode === FILE_ROUTER) {
303-
await templateFile(
304-
templateDirRouter,
305-
'./src/components/Header.tsx.ejs',
306-
'./src/components/Header.tsx',
307-
)
308305
await templateFile(
309306
templateDirRouter,
310307
'./src/routes/__root.tsx.ejs',
@@ -330,19 +327,6 @@ export async function createApp(options: Required<Options>) {
330327
}
331328
}
332329

333-
// Create the main entry point
334-
if (!isAddOnEnabled('start')) {
335-
if (options.typescript) {
336-
await templateFile(templateDirRouter, './src/main.tsx.ejs')
337-
} else {
338-
await templateFile(
339-
templateDirRouter,
340-
'./src/main.tsx.ejs',
341-
'./src/main.jsx',
342-
)
343-
}
344-
}
345-
346330
// Setup the main, reportWebVitals and index.html files
347331
if (!isAddOnEnabled('start') && options.framework === 'react') {
348332
if (options.typescript) {
@@ -426,6 +410,54 @@ export async function createApp(options: Required<Options>) {
426410
}
427411
}
428412

413+
const routes: Array<{
414+
path: string
415+
name: string
416+
}> = []
417+
if (existsSync(resolve(targetDir, 'src/routes'))) {
418+
for (const file of readdirSync(resolve(targetDir, 'src/routes'))) {
419+
const name = file.replace(/\.tsx?|\.jsx?/, '')
420+
routes.push({
421+
path: `./routes/${name}`,
422+
name: name
423+
.split('.')
424+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
425+
.join(''),
426+
})
427+
}
428+
}
429+
430+
// Create the main entry point
431+
if (!isAddOnEnabled('start')) {
432+
if (options.typescript) {
433+
await templateFile(
434+
templateDirRouter,
435+
'./src/main.tsx.ejs',
436+
'./src/main.tsx',
437+
{
438+
routes,
439+
},
440+
)
441+
} else {
442+
await templateFile(
443+
templateDirRouter,
444+
'./src/main.jsx.ejs',
445+
'./src/main.jsx',
446+
{
447+
routes,
448+
},
449+
)
450+
}
451+
}
452+
453+
if (routes.length > 0) {
454+
await templateFile(
455+
templateDirBase,
456+
'./src/components/Header.tsx.ejs',
457+
'./src/components/Header.tsx',
458+
)
459+
}
460+
429461
const warnings: Array<string> = []
430462
for (const addOn of options.chosenAddOns) {
431463
if (addOn.warning) {

src/options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export async function promptForOptions(
9999
options.tailwind = true
100100
}
101101

102+
if (cliOptions.addOns) {
103+
options.typescript = true
104+
}
105+
102106
if (!cliOptions.projectName) {
103107
const value = await text({
104108
message: 'What would you like to name your project?',

templates/react/add-on/form/assets/src/routes/demo.form.tsx renamed to templates/react/add-on/form/assets/src/routes/demo.form.tsx.ejs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { createFileRoute } from '@tanstack/react-router'
1+
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/react-router'
22
import { useForm } from '@tanstack/react-form'
33

4+
<% if (codeRouter) { %>
5+
import type { RootRoute } from '@tanstack/react-router'
6+
<% } else { %>
47
export const Route = createFileRoute('/demo/form')({
5-
component: App,
8+
component: FormDemo,
69
})
10+
<% } %>
711

8-
export default function App() {
12+
function FormDemo() {
913
const form = useForm({
1014
defaultValues: {
1115
fullName: '',
@@ -48,3 +52,11 @@ export default function App() {
4852
</div>
4953
)
5054
}
55+
56+
<% if (codeRouter) { %>
57+
export default (parentRoute: RootRoute) => createRoute({
58+
path: '/demo/form',
59+
component: FormDemo,
60+
getParentRoute: () => parentRoute,
61+
})
62+
<% } %>

templates/react/add-on/form/info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Form",
33
"description": "TansStack Form",
44
"phase": "add-on",
5-
"templates": ["file-router"],
5+
"templates": ["file-router", "code-router"],
66
"link": "https://tanstack.com/form/latest",
77
"routes": [
88
{

templates/react/add-on/sentry/info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"phase": "setup",
44
"description": "Add Sentry for error monitoring, tracing, and session replays (requires Start).",
55
"link": "https://sentry.com/",
6-
"templates": ["file-router"],
6+
"templates": ["file-router", "code-router"],
77
"routes": [
88
{
99
"url": "/demo/sentry/testing",

templates/react/add-on/store/assets/src/routes/demo.store.page1.tsx renamed to templates/react/add-on/store/assets/src/routes/demo.store.page1.tsx.ejs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { createFileRoute } from '@tanstack/react-router'
1+
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/react-router'
22
import { useStore } from '@tanstack/react-store'
33

44
import { store } from '../lib/demo-store'
5-
5+
<% if (codeRouter) { %>
6+
import type { RootRoute } from '@tanstack/react-router'
7+
<% } else { %>
68
export const Route = createFileRoute('/demo/store/page1')({
7-
component: App,
9+
component: StorePage1,
810
})
9-
10-
function App() {
11+
<% } %>
12+
function StorePage1() {
1113
const count = useStore(store, (state) => state.count)
1214

1315
return (
@@ -27,4 +29,10 @@ function App() {
2729
)
2830
}
2931

30-
export default App
32+
<% if (codeRouter) { %>
33+
export default (parentRoute: RootRoute) => createRoute({
34+
path: '/demo/store/page1',
35+
component: StorePage1,
36+
getParentRoute: () => parentRoute,
37+
})
38+
<% } %>

templates/react/add-on/store/assets/src/routes/demo.store.page2.tsx renamed to templates/react/add-on/store/assets/src/routes/demo.store.page2.tsx.ejs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { createFileRoute } from '@tanstack/react-router'
1+
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/react-router'
22
import { useStore } from '@tanstack/react-store'
33

44
import { store } from '../lib/demo-store'
5-
5+
<% if (codeRouter) { %>
6+
import type { RootRoute } from '@tanstack/react-router'
7+
<% } else { %>
68
export const Route = createFileRoute('/demo/store/page2')({
7-
component: App,
9+
component: StorePage2,
810
})
9-
10-
function App() {
11+
<% } %>
12+
function StorePage2() {
1113
const count = useStore(store, (state) => state.count)
1214

1315
return (
@@ -27,4 +29,11 @@ function App() {
2729
)
2830
}
2931

30-
export default App
32+
<% if (codeRouter) { %>
33+
export default (parentRoute: RootRoute) => createRoute({
34+
path: '/demo/store/page2',
35+
component: StorePage2,
36+
getParentRoute: () => parentRoute,
37+
})
38+
<% } %>
39+

templates/react/add-on/store/info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Add TanStack Store to your application.",
44
"phase": "add-on",
55
"link": "https://tanstack.com/store/latest",
6-
"templates": ["file-router"],
6+
"templates": ["file-router", "code-router"],
77
"routes": [
88
{
99
"url": "/demo/store/page1",

templates/react/code-router/src/main.tsx.ejs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
createRouter,
99
} from "@tanstack/react-router";
1010
import { TanStackRouterDevtools } from "@tanstack/router-devtools";
11+
<% for(const route of routes) { %>import <%= route.name %> from "<%= route.path %>";
12+
<% } %><% if (routes.length > 0) { %>
13+
import Header from "./components/Header";
14+
<% } %>
1115

1216
import "./styles.css";
1317
import reportWebVitals from "./reportWebVitals.<%= js %>";
@@ -17,6 +21,7 @@ import App from "./App.<%= jsx %>";
1721
const rootRoute = createRootRoute({
1822
component: () => (
1923
<>
24+
<% if (routes.length > 0) { %><Header /><% } %>
2025
<Outlet />
2126
<TanStackRouterDevtools />
2227
</>
@@ -29,7 +34,7 @@ const indexRoute = createRoute({
2934
component: App,
3035
});
3136

32-
const routeTree = rootRoute.addChildren([indexRoute]);
37+
const routeTree = rootRoute.addChildren([indexRoute<%= routes.map(route => `, ${route.name}(rootRoute)`).join('') %>]);
3338

3439
const router = createRouter({
3540
routeTree,

0 commit comments

Comments
 (0)