diff --git a/__tests__/routes.spec.ts b/__tests__/routes.spec.ts index e7abdf1..975d120 100644 --- a/__tests__/routes.spec.ts +++ b/__tests__/routes.spec.ts @@ -13,6 +13,13 @@ test('Test synchronous route', async () => { }) test('Nested layout should not be imported synchronously', async () => { + await page.click('.excluded-components') + expect(await page.textContent('.no-match-content')).toBe('404') + + await page.goBack() +}) + +test('Should not create Route for excluded directory', async () => { expect((await page.content()).includes('id="foo_layout"')).toBeFalsy() }) diff --git a/example/src/pages/components/button.jsx b/example/src/pages/components/button.jsx new file mode 100644 index 0000000..8cf2a50 --- /dev/null +++ b/example/src/pages/components/button.jsx @@ -0,0 +1,3 @@ +export default function Button() { + return 'button' +} diff --git a/example/src/pages/layout.jsx b/example/src/pages/layout.jsx index a66f1db..c891af7 100644 --- a/example/src/pages/layout.jsx +++ b/example/src/pages/layout.jsx @@ -10,7 +10,8 @@ export default function Layout() { /utils{' '} /foo{' '} /foo/bar{' '} - /foo/:type + /foo/:type{' '} + /excluded/components
layout
diff --git a/example/vite.config.js b/example/vite.config.js index e5c3cdf..49c2653 100644 --- a/example/vite.config.js +++ b/example/vite.config.js @@ -8,7 +8,7 @@ export default defineConfig({ react(), router({ exclude(path) { - return path.includes('utils') + return path.includes('utils') || path.includes('components') }, sync(path) { return path.includes('sync') diff --git a/index.ts b/index.ts index d5bb4d8..70c7a3c 100644 --- a/index.ts +++ b/index.ts @@ -78,9 +78,7 @@ function VitePluginReactRouter(opts: Options = {}): PluginOption { } function readFiles(id: string, route: RouteObject, isDirectory = false, root = false) { - if (exclude?.(id)) { - return - } + if (exclude?.(id)) return const basename = id.endsWith(dir) ? '/' : path.basename(id) @@ -89,8 +87,9 @@ function VitePluginReactRouter(opts: Options = {}): PluginOption { files.forEach(file => { const nextFile = path.join(id, file) - const stat = fs.statSync(nextFile) + if (exclude?.(nextFile)) return + const stat = fs.statSync(nextFile) if (stat.isDirectory()) { const newRoute = { path: path.basename(nextFile) } as RouteObject ;(route.children || (route.children = [])).push(newRoute)