Skip to content

Commit

Permalink
fix: changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlaws0n committed Sep 27, 2024
1 parent a9cc6c7 commit f0a9659
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 72 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/01_template/src/pages/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ const getData = async () => {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
2 changes: 1 addition & 1 deletion examples/01_template/src/pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ const getData = async () => {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
2 changes: 1 addition & 1 deletion examples/01_template/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ const getData = async () => {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
38 changes: 0 additions & 38 deletions examples/10_fs-router/src/entries.gen.tsx

This file was deleted.

11 changes: 11 additions & 0 deletions examples/10_fs-router/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fsRouter } from 'waku/router/server';

declare global {
interface ImportMeta {
readonly glob: any;
}
}

export default fsRouter(import.meta.url, (file: string) =>
import.meta.glob('./pages/**/*.tsx')[`./pages/${file}`]?.(),
);
7 changes: 0 additions & 7 deletions examples/10_fs-router/src/pages/layout.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/waku/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
export const SRC_MAIN = 'main';
export const SRC_ENTRIES = 'entries';
export const SRC_PAGES = 'pages';
52 changes: 29 additions & 23 deletions packages/waku/src/lib/plugins/vite-plugin-fs-router-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import type { Plugin } from 'vite';
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 { SRC_ENTRIES } from '../constants.js';
import { joinPath } from '../utils/path.js';

const SRC_PAGES = 'pages';

const srcToName = (src: string) => {
const split = src
.split('/')
.map((part) => (part[0]!.toUpperCase() + part.slice(1)).replace('-', '_'));
.map((part) => part[0]!.toUpperCase() + part.slice(1));

if (src.endsWith('_layout.tsx')) {
if (split.at(-1) === '_layout.tsx') {
return split.slice(0, -1).join('') + '_Layout';
} else if (src.endsWith('index.tsx')) {
} else if (split.at(-1) === 'index.tsx') {
return split.slice(0, -1).join('') + 'Index';
} else if (split.at(-1)?.startsWith('[...')) {
const fileName = split
.at(-1)!
.replace('-', '_')
.replace('.tsx', '')
.replace('[...', '')
.replace(']', '');
Expand All @@ -29,6 +32,7 @@ const srcToName = (src: string) => {
} else if (split.at(-1)?.startsWith('[')) {
const fileName = split
.at(-1)!
.replace('-', '_')
.replace('.tsx', '')
.replace('[', '')
.replace(']', '');
Expand All @@ -39,7 +43,7 @@ const srcToName = (src: string) => {
fileName.slice(1)
);
} else {
const fileName = split.at(-1)!.replace('.tsx', '');
const fileName = split.at(-1)!.replace('-', '_').replace('.tsx', '');
return (
split.slice(0, -1).join('') +
fileName[0]!.toUpperCase() +
Expand All @@ -49,7 +53,7 @@ const srcToName = (src: string) => {
};

export const fsRouterTypegenPlugin = (opts: { srcDir: string }): Plugin => {
let entriesFile: string | undefined;
let entriesFilePossibilities: string[] | undefined;
let pagesDir: string | undefined;
let outputFile: string | undefined;
let formatter = (s: string): Promise<string> => Promise.resolve(s);
Expand All @@ -58,7 +62,9 @@ export const fsRouterTypegenPlugin = (opts: { srcDir: string }): Plugin => {
apply: 'serve',
async configResolved(config) {
pagesDir = joinPath(config.root, opts.srcDir, SRC_PAGES);
entriesFile = joinPath(config.root, opts.srcDir, `${SRC_ENTRIES}.tsx`);
entriesFilePossibilities = ['tsx', 'ts', 'js', 'jsx'].map((ext) =>
joinPath(config.root, opts.srcDir, `${SRC_ENTRIES}.${ext}`),
);
outputFile = joinPath(config.root, opts.srcDir, `${SRC_ENTRIES}.gen.tsx`);

try {
Expand All @@ -73,21 +79,27 @@ export const fsRouterTypegenPlugin = (opts: { srcDir: string }): Plugin => {
}
},
configureServer(server) {
if (!entriesFile || !pagesDir || !outputFile || existsSync(entriesFile)) {
if (
!entriesFilePossibilities ||
!pagesDir ||
!outputFile ||
entriesFilePossibilities.some((entriesFile) => existsSync(entriesFile))
) {
return;
}

// Recursively collect `.tsx` files in the given directory
const collectFiles = async (dir: string): Promise<string[]> => {
if (!pagesDir) return [];
let results: string[] = [];
const files = await readdir(dir, { withFileTypes: true });
const results: string[] = [];
const files = await readdir(dir, {
withFileTypes: true,
recursive: true,
});

for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
results = results.concat(await collectFiles(fullPath));
} else if (file.isFile() && fullPath.endsWith('.tsx')) {
if (fullPath.endsWith('.tsx')) {
results.push(fullPath.replace(pagesDir, ''));
}
}
Expand All @@ -111,14 +123,14 @@ export const fsRouterTypegenPlugin = (opts: { srcDir: string }): Plugin => {
const src = filePath.slice(1);
const hasGetConfig = fileExportsGetConfig(filePath);

if (filePath.endsWith('_layout.tsx')) {
if (filePath === '/_layout.tsx') {
fileInfo.push({
type: 'layout',
path: filePath.replace('_layout.tsx', ''),
src,
hasGetConfig,
});
} else if (filePath.endsWith('index.tsx')) {
} else if (filePath === '/index.tsx') {
fileInfo.push({
type: 'page',
path: filePath.replace('index.tsx', ''),
Expand Down Expand Up @@ -173,18 +185,12 @@ import type { PathsForPages } from 'waku/router';\n\n`;

server.watcher.add(opts.srcDir);
server.watcher.on('change', async (file) => {
if (file === outputFile) return;
if (!outputFile || outputFile.endsWith(file)) return;

await updateGeneratedFile();
});
server.watcher.on('add', async (file) => {
if (file === outputFile) return;

await updateGeneratedFile();
});

server.watcher.on('', async (file) => {
if (file === outputFile) return;
if (!outputFile || outputFile.endsWith(file)) return;

await updateGeneratedFile();
});
Expand Down

0 comments on commit f0a9659

Please sign in to comment.