Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

main.tsx.ejs

159 lines5.3 KB

1<% if (addOnEnabled.start) { ignoreFile() } %><% if (codeRouter) {
2
3/// Code Router
4​
5%>import { StrictMode } from "react";
6import ReactDOM from "react-dom/client";
7import {
8 Outlet,
9 RouterProvider,
10 createRootRoute,
11 createRoute,
12 createRouter,
13} from "@tanstack/react-router";
14import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
15<% for(const route of routes) { %>import <%= route.jsName %> from "<%= relativePath(route.path) %>";
16<% } %><% if (routes.length > 0) { %>
17import Header from "./components/Header";
18<% } %><% for(const integration of integrations.filter(i => i.type === 'layout' || i.type === 'provider')) { %>
19import <%= integration.jsName %> from "<%= relativePath(integration.path) %>";
20<% } %><% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
21import * as <%= integration.jsName %> from "<%= relativePath(integration.path) %>";
22<% } %>
23​
24import "./styles.css";
25import reportWebVitals from "./reportWebVitals.<%= js %>";
26​
27import App from "./App.<%= jsx %>";
28​
29const rootRoute = createRootRoute({
30 component: () => (
31 <>
32 <% for(const integration of integrations.filter(i => i.type === 'provider')) { %>
33 <<%= integration.jsName %>>
34 <% } %>
35 <% if (routes.length > 0) { %><Header /><% } %>
36 <Outlet />
37 <TanStackRouterDevtools />
38 <% for(const integration of integrations.filter(i => i.type === 'layout')) { %>
39 <<%= integration.jsName %> />
40 <% } %>
41 <% for(const integration of integrations.filter(i => i.type === 'provider').reverse()) { %>
42 </<%= integration.jsName %>>
43 <% } %>
44 </>
45 ),
46});
47​
48const indexRoute = createRoute({
49 getParentRoute: () => rootRoute,
50 path: "/",
51 component: App,
52});
53​
54const routeTree = rootRoute.addChildren([indexRoute<%= routes.map(route => `, ${route.jsName}(rootRoute)`).join('') %>]);
55​
56<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
57const <%= integration.jsName %>Context = <%= integration.jsName %>.getContext();
58<% } %>const router = createRouter({
59 routeTree,
60 context: {
61 <% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
62 ...<%= integration.jsName %>Context,
63 <% } %>
64 },
65 defaultPreload: "intent",
66 scrollRestoration: true,
67 defaultStructuralSharing: true,
68 defaultPreloadStaleTime: 0,
69});
70<% if (typescript) { %>
71declare module "@tanstack/react-router" {
72 interface Register {
73 router: typeof router;
74 }
76​
77const rootElement = document.getElementById("app");
78<% } else { %>
79const rootElement = document.getElementById("app");
80<% } %>if (rootElement && !rootElement.innerHTML) {
81 const root = ReactDOM.createRoot(rootElement);
82 root.render(
83 <StrictMode>
84 <% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
85 <<%= integration.jsName %>.Provider {...<%= integration.jsName %>Context}>
86 <% } %>
87 <RouterProvider router={router} />
88 <% for(const integration of integrations.filter(i => i.type === 'root-provider').reverse()) { %>
89 </<%= integration.jsName %>.Provider>
90 <% } %>
91 </StrictMode>
92 );
94​
95// If you want to start measuring performance in your app, pass a function
96// to log results (for example: reportWebVitals(console.log))
97// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
98reportWebVitals();
99<% } else {
101/* File Router */
103%>import { StrictMode } from "react";
104import ReactDOM from "react-dom/client";
105import { RouterProvider, createRouter } from "@tanstack/react-router";
106<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
107import * as <%= integration.jsName %> from "<%= relativePath(integration.path) %>";
108<% } %>
109​
110// Import the generated route tree
111import { routeTree } from "./routeTree.gen";
112​
113import "./styles.css";
114import reportWebVitals from "./reportWebVitals.<%= js %>";
115​
116// Create a new router instance
117<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
118const <%= integration.jsName %>Context = <%= integration.jsName %>.getContext();
119<% } %>const router = createRouter({
120 routeTree,
121 context: {
122 <% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
123 ...<%= integration.jsName %>Context,
124 <% } %>
125 },
126 defaultPreload: "intent",
127 scrollRestoration: true,
128 defaultStructuralSharing: true,
129 defaultPreloadStaleTime: 0,
130});
131​
132// Register the router instance for type safety
133declare module "@tanstack/react-router" {
134 interface Register {
135 router: typeof router;
136 }
138​
139// Render the app
140const rootElement = document.getElementById("app");
141if (rootElement && !rootElement.innerHTML) {
142 const root = ReactDOM.createRoot(rootElement);
143 root.render(
144 <StrictMode>
145 <% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
146 <<%= integration.jsName %>.Provider {...<%= integration.jsName %>Context}>
147 <% } %>
148 <RouterProvider router={router} />
149 <% for(const integration of integrations.filter(i => i.type === 'root-provider').reverse()) { %>
150 </<%= integration.jsName %>.Provider>
151 <% } %>
152 </StrictMode>
153 );
155​
156// If you want to start measuring performance in your app, pass a function
157// to log results (for example: reportWebVitals(console.log))
158// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
159reportWebVitals();<% } %>