Skip to content

Commit

Permalink
feat: add router config (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Nov 6, 2024
1 parent 5e86baf commit 4d142b3
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
"updateInternalDependents": "always"
},
"ignore": ["@examples/hackernews", "@examples/normal"]
"ignore": ["@examples/hackernews", "@examples/normal", "@examples/with-antd4"]
}
5 changes: 5 additions & 0 deletions .changeset/witty-balloons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: add router config
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.

## LICENSE

Expand Down
6 changes: 5 additions & 1 deletion examples/normal/.tnfrc.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export default {};
export default {
router: {
defaultPreload: 'intent',
},
};
4 changes: 1 addition & 3 deletions examples/normal/src/pages/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const Route = createRootRoute({
<Link to="/">Home</Link>
</li>
<li>
<Link to="/foo" preload="intent">
Foo
</Link>
<Link to="/foo">Foo</Link>
</li>
</ul>
<Outlet />
Expand Down
1 change: 1 addition & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function build({
await prepare({
cwd,
tmpPath,
config,
});
};

Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const ConfigSchema = z.object({
plugins: z.array(z.any()).optional(),
})
.optional(),
router: z
.object({
defaultPreload: z.enum(['intent', 'render', 'viewport']).optional(),
defaultPreloadDelay: z.number().optional(),
})
.optional(),
});

export type Config = z.infer<typeof ConfigSchema>;
Expand Down
6 changes: 5 additions & 1 deletion src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { generator } from '@tanstack/router-generator';
import type { Config } from '@tanstack/router-generator';
import fs from 'fs';
import path from 'pathe';
import type { Config as TnfConfig } from './config';

interface BaseOptions {
cwd: string;
tmpPath: string;
config?: TnfConfig;
}

interface PrepareOptions extends BaseOptions {}

export async function prepare(opts: PrepareOptions) {
const { cwd, tmpPath } = opts;
const { cwd, tmpPath, config } = opts;

fs.rmSync(tmpPath, { recursive: true, force: true });
fs.mkdirSync(tmpPath, { recursive: true });
Expand Down Expand Up @@ -55,6 +57,8 @@ import {
import { routeTree } from './routeTree.gen';
const router = createRouter({
routeTree,
defaultPreload: ${config?.router?.defaultPreload ? `'${config.router.defaultPreload}'` : 'false'},
defaultPreloadDelay: ${config?.router?.defaultPreloadDelay || 50},
});
declare module '@tanstack/react-router' {
interface Register {
Expand Down

0 comments on commit 4d142b3

Please sign in to comment.