Skip to content

Commit

Permalink
feat: add convention config (#46)
Browse files Browse the repository at this point in the history
* feat: add convention config

* chore: code style

* fix: convention default value
  • Loading branch information
xiaohuoni authored Nov 14, 2024
1 parent fe9d4d9 commit 4c531df
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-queens-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: add convention config
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false }`: The router configuration.
- `router: { defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false; convention?: [@tanstack/router-generator](https://github.com/TanStack/router/blob/main/packages/router-generator/src/config.ts#L22C14-L22C26).Config }`: The router configuration.
- `tailwindcss: boolean`: Turn on/off tailwindcss. Need to be used in conjunction with `src/tailwind.css` and `tailwind.config.js`.

## FAQ
Expand Down
3 changes: 3 additions & 0 deletions examples/normal/.tnfrc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export default {
router: {
defaultPreload: 'intent',
convention: {
routeFileIgnorePattern: 'components',
},
},
};
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/normal/src/pages/components/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createFileRoute } from '@umijs/tnf/router';
import styles from './index.module.less';

function Home() {
return (
<div className={styles.foo}>
<h3>Welcome Home!</h3>
</div>
);
}

export default Home;
10 changes: 1 addition & 9 deletions examples/normal/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { createFileRoute } from '@umijs/tnf/router';
import styles from './index.module.less';
import Home from './components';

export const Route = createFileRoute('/')({
component: Home,
});

function Home() {
return (
<div className={styles.foo}>
<h3>Welcome Home!</h3>
</div>
);
}
28 changes: 28 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { z } from 'zod';
import { PluginSchema } from '../plugin/types';

// why no use { configSchema } from '@tanstack/router-generator';
// configSchema has default
const RouterGeneratorConfig = z
.object({
routeFileIgnorePrefix: z.string().optional(),
routeFileIgnorePattern: z.string().optional(),
routesDirectory: z.string().optional(),
generatedRouteTree: z.string().optional(),
quoteStyle: z.enum(['single', 'double']).optional(),
semicolons: z.boolean().optional(),
disableTypes: z.boolean().optional(),
addExtensions: z.boolean().optional(),
disableLogging: z.boolean().optional(),
disableManifestGeneration: z.boolean().optional(),
apiBase: z.string().optional(),
routeTreeFileHeader: z.array(z.string()).optional(),
routeTreeFileFooter: z.array(z.string()).optional(),
indexToken: z.string().optional(),
routeToken: z.string().optional(),
experimental: z
.object({
enableCodeSplitting: z.boolean().optional(),
})
.optional(),
})
.optional();

export const ConfigSchema = z.object({
alias: z.array(z.tuple([z.string(), z.string()])).optional(),
bundler: z.enum(['webpack', 'mako']).optional(),
Expand Down Expand Up @@ -44,6 +71,7 @@ export const ConfigSchema = z.object({
z.literal(false),
])
.optional(),
convention: RouterGeneratorConfig,
})
.optional(),
tailwindcss: z.boolean().optional(),
Expand Down
4 changes: 4 additions & 0 deletions src/sync/write_route_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import type { SyncOptions } from './sync';
export async function writeRouteTree({ context }: SyncOptions) {
const {
cwd,
config,
paths: { tmpPath },
} = context;
const { router } = config;

await generator({
routeFileIgnorePrefix: '-',
routesDirectory: path.join(cwd, 'src/pages'),
Expand All @@ -30,5 +33,6 @@ export async function writeRouteTree({ context }: SyncOptions) {
experimental: {
enableCodeSplitting: true,
},
...(router?.convention || {}),
} as Config);
}

0 comments on commit 4c531df

Please sign in to comment.