create-tsrouter-app 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/FUNDING.yml +1 -0
  3. package/.github/ISSUE_TEMPLATE/bug_report.yml +94 -0
  4. package/.github/ISSUE_TEMPLATE/config.yml +11 -0
  5. package/.github/workflows/ci.yml +43 -0
  6. package/.nvmrc +1 -0
  7. package/.prettierignore +3 -0
  8. package/CONTRIBUTING.md +32 -0
  9. package/LICENSE +21 -0
  10. package/README.md +47 -0
  11. package/dist/index.js +140 -0
  12. package/dist/utils/getPackageManager.js +15 -0
  13. package/eslint.config.js +35 -0
  14. package/package.json +41 -0
  15. package/prettier.config.js +10 -0
  16. package/project-template/.vscode/settings.json +11 -0
  17. package/project-template/README.md.ejs +498 -0
  18. package/project-template/gitignore +5 -0
  19. package/project-template/index.html.ejs +20 -0
  20. package/project-template/package.json +28 -0
  21. package/project-template/package.ts.json +7 -0
  22. package/project-template/package.tw.json +6 -0
  23. package/project-template/public/favicon.ico +0 -0
  24. package/project-template/public/logo192.png +0 -0
  25. package/project-template/public/logo512.png +0 -0
  26. package/project-template/public/manifest.json +25 -0
  27. package/project-template/public/robots.txt +3 -0
  28. package/project-template/src/App.css +38 -0
  29. package/project-template/src/App.test.tsx.ejs +10 -0
  30. package/project-template/src/App.tsx.ejs +64 -0
  31. package/project-template/src/logo.svg +44 -0
  32. package/project-template/src/main.tsx.ejs +61 -0
  33. package/project-template/src/reportWebVitals.ts.ejs +28 -0
  34. package/project-template/src/styles.css.ejs +15 -0
  35. package/project-template/tsconfig.dev.json +10 -0
  36. package/project-template/tsconfig.json +10 -0
  37. package/project-template/vite.config.js.ejs +14 -0
  38. package/scripts/publish.js +33 -0
  39. package/src/index.ts +248 -0
  40. package/src/utils/getPackageManager.ts +22 -0
  41. package/tsconfig.json +15 -0
@@ -0,0 +1,498 @@
1
+ Welcome to your new TanStack app!
2
+
3
+ # Getting Started
4
+
5
+ To run this application:
6
+
7
+ ```bash
8
+ <%= packageManager %> install
9
+ <%= packageManager %> start
10
+ ```
11
+
12
+ # Building For Production
13
+
14
+ To build this application for production:
15
+
16
+ ```bash
17
+ <%= packageManager %> run build
18
+ ```
19
+
20
+ ## Testing
21
+
22
+ This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:
23
+
24
+ ```bash
25
+ <%= packageManager %> run test
26
+ ```
27
+
28
+ ## Styling
29
+ <% if (tailwind) { %>
30
+ This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
31
+ <% } else { %>
32
+ This project uses CSS for styling.
33
+ <% } %>
34
+ ## Routing
35
+
36
+ This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the `./src/main.<%= jsx %>` file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) guide.
37
+
38
+ ### Adding A Route
39
+
40
+ To add a new route to your application just add another `createRoute` call to the `./src/main.<%= jsx %>` file. The example below adds a new `/about`route to the root route.
41
+
42
+ ```tsx
43
+ const aboutRoute = createRoute({
44
+ getParentRoute: () => rootRoute,
45
+ path: "/about",
46
+ component: () => <h1>About</h1>,
47
+ });
48
+ ```
49
+
50
+ You will also need to add the route to the `routeTree` in the `./src/main.<%= jsx %>` file.
51
+
52
+ ```tsx
53
+ const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]);
54
+ ```
55
+
56
+ With this set up you should be able to navigate to `/about` and see the about page.
57
+
58
+ Of course you don't need to implement the About page in the `main.<%= jsx %>` file. You can create that component in another file and import it into the `main.<%= jsx %>` file, then use it in the `component` property of the `createRoute` call, like so:
59
+
60
+ ```tsx
61
+ import About from "./components/About";
62
+
63
+ const aboutRoute = createRoute({
64
+ getParentRoute: () => rootRoute,
65
+ path: "/about",
66
+ component: About,
67
+ });
68
+ ```
69
+
70
+ That is how we have the `App` component set up with the home page.
71
+
72
+ For more information on the options you have when you are creating code based routes check out the [Code Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/code-based-routing) documentation.
73
+
74
+ Now that you have two routes you can use a `Link` component to navigate between them.
75
+
76
+ ### Adding Links
77
+
78
+ To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
79
+
80
+ ```tsx
81
+ import { Link } from "@tanstack/react-router";
82
+ ```
83
+
84
+ Then anywhere in your JSX you can use it like so:
85
+
86
+ ```tsx
87
+ <Link to="/about">About</Link>
88
+ ```
89
+
90
+ This will create a link that will navigate to the `/about` route.
91
+
92
+ More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
93
+
94
+ ### Using A Layout
95
+
96
+ Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
97
+
98
+ There is already a layout in the `src/main.<%= jsx %>` file:
99
+
100
+ ```tsx
101
+ const rootRoute = createRootRoute({
102
+ component: () => (
103
+ <>
104
+ <Outlet />
105
+ <TanStackRouterDevtools />
106
+ </>
107
+ ),
108
+ });
109
+ ```
110
+
111
+ You can use the React component specified in the `component` property of the `rootRoute` to wrap the contents of the routes. The `<Outlet />` component is used to render the current route within the body of the layout. For example you could add a header to the layout like so:
112
+
113
+ ```tsx
114
+ const rootRoute = createRootRoute({
115
+ component: () => (
116
+ <>
117
+ <header>
118
+ <nav>
119
+ <Link to="/">Home</Link>
120
+ <Link to="/about">About</Link>
121
+ </nav>
122
+ </header>
123
+ <Outlet />
124
+ <TanStackRouterDevtools />
125
+ </>
126
+ ),
127
+ });
128
+ ```
129
+
130
+ The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
131
+
132
+ More information on layouts can be found in the [Layouts documentation](hthttps://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
133
+
134
+ ### Migrating To File Base Routing
135
+
136
+ First you need to add the Vite plugin for Tanstack Router:
137
+
138
+ ```bash
139
+ <%= packageManager %> install -D @tanstack/router-plugin
140
+ ```
141
+
142
+ From there you need to update your `vite.config.js` file to use the plugin:
143
+
144
+ ```ts
145
+ import { defineConfig } from "vite";
146
+ import viteReact from "@vitejs/plugin-react";
147
+ import { TanStackRouterVite } from "@tanstack/router-plugin/vite";<% if (tailwind) { %>
148
+ import tailwindcss from "@tailwindcss/vite";
149
+ <% } %>
150
+
151
+ // https://vitejs.dev/config/
152
+ export default defineConfig({
153
+ plugins: [
154
+ TanStackRouterVite(),
155
+ viteReact()<% if (tailwind) { %>,
156
+ tailwindcss()<% } %>
157
+ ],
158
+ });
159
+ ```
160
+
161
+ Now you'll need to rearrange your files a little bit. That starts with creating a `routes` directory in the `src` directory:
162
+
163
+ ```bash
164
+ mkdir src/routes
165
+ ```
166
+
167
+ Then you'll need to create a `src/routes/__root.<%= jsx %>` file with the contents of the root route that was in `main.<%= jsx %>`.
168
+
169
+ ```tsx
170
+ import { createRootRoute, Outlet } from "@tanstack/react-router";
171
+ import { TanStackRouterDevtools } from "@tanstack/router-devtools";
172
+
173
+ export const Route = createRootRoute({
174
+ component: () => (
175
+ <>
176
+ <Outlet />
177
+ <TanStackRouterDevtools />
178
+ </>
179
+ ),
180
+ });
181
+ ```
182
+
183
+ Next up you'll need to move your home route code into `src/routes/index.<%= jsx %>`
184
+
185
+ ```tsx
186
+ import { createFileRoute } from "@tanstack/react-router";
187
+
188
+ import logo from "../logo.svg";
189
+ import "../App.css";
190
+
191
+ export const Route = createFileRoute("/")({
192
+ component: App,
193
+ });
194
+
195
+ function App() {
196
+ return (<% if (tailwind) { %>
197
+ <div className="text-center">
198
+ <header className="min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]">
199
+ <img
200
+ src={logo}
201
+ className="h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]"
202
+ alt="logo"
203
+ />
204
+ <p>
205
+ Edit <code>src/App.tsx</code> and save to reload.
206
+ </p>
207
+ <a
208
+ className="text-[#61dafb] hover:underline"
209
+ href="https://reactjs.org"
210
+ target="_blank"
211
+ rel="noopener noreferrer"
212
+ >
213
+ Learn React
214
+ </a>
215
+ <a
216
+ className="text-[#61dafb] hover:underline"
217
+ href="https://tanstack.com"
218
+ target="_blank"
219
+ rel="noopener noreferrer"
220
+ >
221
+ Learn TanStack
222
+ </a>
223
+ </header>
224
+ </div>
225
+ <% } else { %>
226
+ <div className="App">
227
+ <header className="App-header">
228
+ <img src={logo} className="App-logo" alt="logo" />
229
+ <p>
230
+ Edit <code>src/App.tsx</code> and save to reload.
231
+ </p>
232
+ <a
233
+ className="App-link"
234
+ href="https://reactjs.org"
235
+ target="_blank"
236
+ rel="noopener noreferrer"
237
+ >
238
+ Learn React
239
+ </a>
240
+ <a
241
+ className="App-link"
242
+ href="https://tanstack.com"
243
+ target="_blank"
244
+ rel="noopener noreferrer"
245
+ >
246
+ Learn TanStack
247
+ </a>
248
+ </header>
249
+ </div>
250
+ <% } %> );
251
+ }
252
+ ```
253
+
254
+ At this point you can delete `src/App.<%= jsx %>`, you will no longer need it as the contents have moved into `src/routes/index.<%= jsx %>`.
255
+
256
+ The only additional code is the `createFileRoute` function that tells TanStack Router where to render the route. Helpfully the Vite plugin will keep the path argument that goes to `createFileRoute` automatically in sync with the file system.
257
+
258
+ Finally the `src/main.<%= jsx %>` file can be simplified down to this:
259
+
260
+ ```tsx
261
+ import { StrictMode } from "react";
262
+ import ReactDOM from "react-dom/client";
263
+ import { RouterProvider, createRouter } from "@tanstack/react-router";
264
+
265
+ // Import the generated route tree
266
+ import { routeTree } from "./routeTree.gen";
267
+
268
+ import "./styles.css";
269
+ import reportWebVitals from "./reportWebVitals";
270
+
271
+ // Create a new router instance
272
+ const router = createRouter({ routeTree });
273
+ <% if (typescript) { %>
274
+ // Register the router instance for type safety
275
+ declare module "@tanstack/react-router" {
276
+ interface Register {
277
+ router: typeof router;
278
+ }
279
+ }
280
+
281
+ // Render the app
282
+ const rootElement = document.getElementById("app")!;
283
+ <% } else { %>
284
+ // Render the app
285
+ const rootElement = document.getElementById("app");
286
+ <% } %>if (!rootElement.innerHTML) {
287
+ const root = ReactDOM.createRoot(rootElement);
288
+ root.render(
289
+ <StrictMode>
290
+ <RouterProvider router={router} />
291
+ </StrictMode>
292
+ );
293
+ }
294
+
295
+ // If you want to start measuring performance in your app, pass a function
296
+ // to log results (for example: reportWebVitals(console.log))
297
+ // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
298
+ reportWebVitals();
299
+ ```
300
+
301
+ Now you've got a file based routing setup in your project! Let's have some fun with it! Just create a file in `about.<%= jsx %>` in `src/routes` and it if the application is running TanStack will automatically add contents to the file and you'll have the start of your `/about` route ready to go with no additional work. You can see why folks find File Based Routing so easy to use.
302
+
303
+ You can find out everything you need to know on how to use file based routing in the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) documentation.
304
+
305
+ ## Data Fetching
306
+
307
+ There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
308
+
309
+ For example:
310
+
311
+ ```tsx
312
+ const peopleRoute = createRoute({
313
+ getParentRoute: () => rootRoute,
314
+ path: "/people",
315
+ loader: async () => {
316
+ const response = await fetch("https://swapi.dev/api/people");<% if (typescript) { %>
317
+ return response.json() as Promise<{
318
+ results: {
319
+ name: string;
320
+ }[];
321
+ }>;
322
+ <% } else { %>
323
+ return response.json();
324
+ <% } %> },
325
+ component: () => {
326
+ const data = peopleRoute.useLoaderData();
327
+ return (
328
+ <ul>
329
+ {data.results.map((person) => (
330
+ <li key={person.name}>{person.name}</li>
331
+ ))}
332
+ </ul>
333
+ );
334
+ },
335
+ });
336
+ ```
337
+
338
+ Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).
339
+
340
+ ### React-Query
341
+
342
+ React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
343
+
344
+ First add your dependencies:
345
+
346
+ ```bash
347
+ <%= packageManager %> install @tanstack/react-query @tanstack/react-query-devtools
348
+ ```
349
+
350
+ Next we'll need to creata query client and provider. We recommend putting those in `main.<%= jsx %>`.
351
+
352
+ ```tsx
353
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
354
+
355
+ // ...
356
+
357
+ const queryClient = new QueryClient();
358
+
359
+ // ...
360
+
361
+ if (!rootElement.innerHTML) {
362
+ const root = ReactDOM.createRoot(rootElement);
363
+
364
+ root.render(
365
+ <QueryClientProvider client={queryClient}>
366
+ <RouterProvider router={router} />
367
+ </QueryClientProvider>
368
+ );
369
+ }
370
+ ```
371
+
372
+ You can also add TanStack Query Devtools to the root route (optional).
373
+
374
+ ```tsx
375
+ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
376
+
377
+ const rootRoute = createRootRoute({
378
+ component: () => (
379
+ <>
380
+ <Outlet />
381
+ <ReactQueryDevtools buttonPosition="top-right" />
382
+ <TanStackRouterDevtools />
383
+ </>
384
+ ),
385
+ });
386
+ ```
387
+
388
+ Now you can use `useQuery` to fetch your data.
389
+
390
+ ```tsx
391
+ import { useQuery } from "@tanstack/react-query";
392
+
393
+ import "./App.css";
394
+
395
+ function App() {
396
+ const { data } = useQuery({
397
+ queryKey: ["people"],
398
+ queryFn: () =>
399
+ fetch("https://swapi.dev/api/people")
400
+ .then((res) => res.json())<% if (typescript) { %>
401
+ .then((data) => data.results as { name: string }[]),
402
+ <% } else { %>
403
+ .then((data) => data.results),
404
+ <% } %> initialData: [],
405
+ });
406
+
407
+ return (
408
+ <div>
409
+ <ul>
410
+ {data.map((person) => (
411
+ <li key={person.name}>{person.name}</li>
412
+ ))}
413
+ </ul>
414
+ </div>
415
+ );
416
+ }
417
+
418
+ export default App;
419
+ ```
420
+
421
+ You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
422
+
423
+ ## State Management
424
+
425
+ Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.
426
+
427
+ First you need to add TanStack Store as a dependency:
428
+
429
+ ```bash
430
+ <%= packageManager %> install @tanstack/store
431
+ ```
432
+
433
+ Now let's create a simple counter in the `src/App.<%= jsx %>` file as a demonstration.
434
+
435
+ ```tsx
436
+ import { useStore } from "@tanstack/react-store";
437
+ import { Store } from "@tanstack/store";
438
+ import "./App.css";
439
+
440
+ const countStore = new Store(0);
441
+
442
+ function App() {
443
+ const count = useStore(countStore);
444
+ return (
445
+ <div>
446
+ <button onClick={() => countStore.setState((n) => n + 1)}>
447
+ Increment - {count}
448
+ </button>
449
+ </div>
450
+ );
451
+ }
452
+
453
+ export default App;
454
+ ```
455
+
456
+ One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
457
+
458
+ Let's check this out by doubling the count using derived state.
459
+
460
+ ```tsx
461
+ import { useStore } from "@tanstack/react-store";
462
+ import { Store, Derived } from "@tanstack/store";
463
+ import "./App.css";
464
+
465
+ const countStore = new Store(0);
466
+
467
+ const doubledStore = new Derived({
468
+ fn: () => countStore.state * 2,
469
+ deps: [countStore],
470
+ });
471
+ doubledStore.mount();
472
+
473
+ function App() {
474
+ const count = useStore(countStore);
475
+ const doubledCount = useStore(doubledStore);
476
+
477
+ return (
478
+ <div>
479
+ <button onClick={() => countStore.setState((n) => n + 1)}>
480
+ Increment - {count}
481
+ </button>
482
+ <div>Doubled - {doubledCount}</div>
483
+ </div>
484
+ );
485
+ }
486
+
487
+ export default App;
488
+ ```
489
+
490
+ We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
491
+
492
+ Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
493
+
494
+ You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
495
+
496
+ # Learn More
497
+
498
+ You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).
@@ -0,0 +1,5 @@
1
+ node_modules
2
+ .DS_Store
3
+ dist
4
+ dist-ssr
5
+ *.local
@@ -0,0 +1,20 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <link rel="icon" href="/favicon.ico" />
7
+ <meta name="theme-color" content="#000000" />
8
+ <meta
9
+ name="description"
10
+ content="Web site created using create-tsrouter-app"
11
+ />
12
+ <link rel="apple-touch-icon" href="/logo192.png" />
13
+ <link rel="manifest" href="/manifest.json" />
14
+ <title>Create TanStack App - <%= projectName %></title>
15
+ </head>
16
+ <body>
17
+ <div id="app"></div>
18
+ <script type="module" src="/src/main.<%= jsx %>"></script>
19
+ </body>
20
+ </html>
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "start": "vite --port 3000",
7
+ "build": "vite build && tsc --noEmit",
8
+ "serve": "vite preview",
9
+ "test": "vitest"
10
+ },
11
+ "dependencies": {
12
+ "@tanstack/react-router": "^1.104.1",
13
+ "@tanstack/router-devtools": "^1.104.3",
14
+ "react": "^19.0.0",
15
+ "react-dom": "^19.0.0"
16
+ },
17
+ "devDependencies": {
18
+ "@testing-library/react": "^16.2.0",
19
+ "@types/react": "^19.0.8",
20
+ "@types/react-dom": "^19.0.3",
21
+ "@vitejs/plugin-react": "^4.3.4",
22
+ "jsdom": "^26.0.0",
23
+ "typescript": "^5.7.2",
24
+ "vite": "^6.1.0",
25
+ "vitest": "^3.0.5",
26
+ "web-vitals": "^4.2.4"
27
+ }
28
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "devDependencies": {
3
+ "@types/react": "^19.0.8",
4
+ "@types/react-dom": "^19.0.3",
5
+ "typescript": "^5.7.2"
6
+ }
7
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "dependencies": {
3
+ "@tailwindcss/vite": "^4.0.6",
4
+ "tailwindcss": "^4.0.6"
5
+ }
6
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "short_name": "React App",
3
+ "name": "Create React App Sample",
4
+ "icons": [
5
+ {
6
+ "src": "favicon.ico",
7
+ "sizes": "64x64 32x32 24x24 16x16",
8
+ "type": "image/x-icon"
9
+ },
10
+ {
11
+ "src": "logo192.png",
12
+ "type": "image/png",
13
+ "sizes": "192x192"
14
+ },
15
+ {
16
+ "src": "logo512.png",
17
+ "type": "image/png",
18
+ "sizes": "512x512"
19
+ }
20
+ ],
21
+ "start_url": ".",
22
+ "display": "standalone",
23
+ "theme_color": "#000000",
24
+ "background_color": "#ffffff"
25
+ }
@@ -0,0 +1,3 @@
1
+ # https://www.robotstxt.org/robotstxt.html
2
+ User-agent: *
3
+ Disallow:
@@ -0,0 +1,38 @@
1
+ .App {
2
+ text-align: center;
3
+ }
4
+
5
+ .App-logo {
6
+ height: 40vmin;
7
+ pointer-events: none;
8
+ }
9
+
10
+ @media (prefers-reduced-motion: no-preference) {
11
+ .App-logo {
12
+ animation: App-logo-spin infinite 20s linear;
13
+ }
14
+ }
15
+
16
+ .App-header {
17
+ background-color: #282c34;
18
+ min-height: 100vh;
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ justify-content: center;
23
+ font-size: calc(10px + 2vmin);
24
+ color: white;
25
+ }
26
+
27
+ .App-link {
28
+ color: #61dafb;
29
+ }
30
+
31
+ @keyframes App-logo-spin {
32
+ from {
33
+ transform: rotate(0deg);
34
+ }
35
+ to {
36
+ transform: rotate(360deg);
37
+ }
38
+ }
@@ -0,0 +1,10 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { render, screen } from "@testing-library/react";
3
+ import App from "./App";
4
+
5
+ describe("App", () => {
6
+ test("renders", () => {
7
+ render(<App />);
8
+ expect(screen.getByText("Learn React")).toBeDefined();
9
+ });
10
+ });