Skip to content

Commit

Permalink
fix: fs-router example and plugin fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlaws0n committed Sep 27, 2024
1 parent f99ddb9 commit b901d57
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
38 changes: 38 additions & 0 deletions examples/10_fs-router/src/entries.gen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createPages } from 'waku';
import type { PathsForPages } from 'waku/router';

import _Layout from './pages/_layout';
import Bar from './pages/bar';
import FooIndex from './pages/foo/index';
import Index from './pages/index';
import Layout from './pages/layout';
import NestedSlugName from './pages/nested/[name]';

const _pages = createPages(async (pagesFns) => [
pagesFns.createLayout({ path: '/', component: _Layout, render: 'static' }),
pagesFns.createPage({ path: '/bar', component: Bar, render: 'dynamic' }),
pagesFns.createPage({
path: '/foo/',
component: FooIndex,
render: 'dynamic',
}),
pagesFns.createPage({ path: '/', component: Index, render: 'dynamic' }),
pagesFns.createPage({
path: '/layout',
component: Layout,
render: 'dynamic',
}),
pagesFns.createPage({
path: '/nested/[name]',
component: NestedSlugName,
render: 'dynamic',
}),
]);

declare module 'waku/router' {
interface RouteConfig {
paths: PathsForPages<typeof _pages>;
}
}

export default _pages;
11 changes: 0 additions & 11 deletions examples/10_fs-router/src/entries.tsx

This file was deleted.

5 changes: 5 additions & 0 deletions examples/10_fs-router/src/pages/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ const HomeLayout = ({ children }: { children: ReactNode }) => (
Nested / Qux
</Link>
</li>
<li>
<Link to="/layout" pending={<Pending isPending />}>
A Page named Layout
</Link>
</li>
</ul>
{children}
</div>
Expand Down
7 changes: 7 additions & 0 deletions examples/10_fs-router/src/pages/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Layout = () => (
<div>
<h2>This is a page named Layout</h2>
</div>
);

export default Layout;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin } from 'vite';
import { promises, readFileSync, existsSync } from 'node:fs';
import { readdir, writeFile } from 'node:fs/promises';
import { existsSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { SRC_ENTRIES, SRC_PAGES } from '../constants.js';
import { joinPath } from '../utils/path.js';
Expand All @@ -10,7 +11,7 @@ const srcToName = (src: string) => {
.map((part) => (part[0]!.toUpperCase() + part.slice(1)).replace('-', '_'));

if (src.endsWith('_layout.tsx')) {
return split.slice(0, -1).join('') + 'Layout';
return split.slice(0, -1).join('') + '_Layout';
} else if (src.endsWith('index.tsx')) {
return split.slice(0, -1).join('') + 'Index';
} else if (split.at(-1)?.startsWith('[...')) {
Expand Down Expand Up @@ -80,7 +81,7 @@ export const fsRouterTypegenPlugin = (opts: { srcDir: string }): Plugin => {
const collectFiles = async (dir: string): Promise<string[]> => {
if (!pagesDir) return [];
let results: string[] = [];
const files = await promises.readdir(dir, { withFileTypes: true });
const files = await readdir(dir, { withFileTypes: true });

for (const file of files) {
const fullPath = path.join(dir, file.name);
Expand Down Expand Up @@ -167,7 +168,7 @@ import type { PathsForPages } from 'waku/router';\n\n`;
if (!pagesDir || !outputFile) return;
const files = await collectFiles(pagesDir);
const formatted = await formatter(generateFile(files));
await promises.writeFile(outputFile, formatted, 'utf-8');
await writeFile(outputFile, formatted, 'utf-8');
};

server.watcher.add(opts.srcDir);
Expand Down

0 comments on commit b901d57

Please sign in to comment.