Skip to content

Commit

Permalink
feat: add router devtool
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 8, 2024
1 parent 3871721 commit 1170d32
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Config is loaded from `.tnfrc.ts` by default.
- `devServer: { port?: number; host?: string; https?: { hosts?: string[] }; ip?: string }`: The development server configuration.
- `externals: Record<string, string>`: An object that maps package names to their corresponding paths.
- `less: { modifyVars?: Record<string, string>; globalVars?: Record<string, string>; math?: 'always' | 'strict' | 'parens-division' | 'parens' | 'strict-legacy' | number; sourceMap?: any; plugins?: (string | [string, Record<string, any>])[];}`: The configuration passed to lessLoader.
- `router: { defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number }`: The router configuration.
- `router: { defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false }`: The router configuration.

## LICENSE

Expand Down
13 changes: 13 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ const ConfigSchema = z.object({
.object({
defaultPreload: z.enum(['intent', 'render', 'viewport']).optional(),
defaultPreloadDelay: z.number().optional(),
devtool: z
.union([
z.object({
options: z.object({
initialIsOpen: z.boolean().optional(),
position: z
.enum(['bottom-left', 'bottom-right', 'top-left', 'top-right'])
.optional(),
}),
}),
z.literal(false),
])
.optional(),
})
.optional(),
});
Expand Down
17 changes: 16 additions & 1 deletion src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,23 @@ declare module '@tanstack/react-router' {
router: typeof router
}
}
const TanStackRouterDevtools =
process.env.NODE_ENV === 'production'
? () => null
: React.lazy(() =>
import('@tanstack/router-devtools').then((res) => ({
default: res.TanStackRouterDevtools,
})),
)
ReactDOM.createRoot(document.getElementById('root')!).render(
<RouterProvider router={router} />
<>
<RouterProvider router={router} />
${
config?.router?.devtool !== false
? `<TanStackRouterDevtools router={router} initialIsOpen=${config?.router?.devtool?.options?.initialIsOpen || '{false}'} position=${config?.router?.devtool?.options?.position || '"bottom-left"'} />`
: ''
}
</>
);
`,
);
Expand Down

0 comments on commit 1170d32

Please sign in to comment.