Skip to content

Commit

Permalink
fix: should not create Route for excluded directory
Browse files Browse the repository at this point in the history
  • Loading branch information
KAROTT7 committed Nov 22, 2022
1 parent f8a4b1e commit 72debdd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions __tests__/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
3 changes: 3 additions & 0 deletions example/src/pages/components/button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Button() {
return 'button'
}
3 changes: 2 additions & 1 deletion example/src/pages/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default function Layout() {
<Link className="utils" to="/utils">/utils</Link>{' '}
<Link className="foo" to="/foo">/foo</Link>{' '}
<Link className="foo-bar" to="/foo/bar">/foo/bar</Link>{' '}
<Link className="foo-type" to="/foo/a">/foo/:type</Link>
<Link className="foo-type" to="/foo/a">/foo/:type</Link>{' '}
<Link className="excluded-components" to="/components">/excluded/components</Link>
</div>
<div className="layout">layout</div>
<Outlet />
Expand Down
2 changes: 1 addition & 1 deletion example/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 3 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 72debdd

Please sign in to comment.