fix: React Refresh has trouble inferring router exports

This commit is contained in:
cha0s 2024-02-20 05:13:14 -06:00
parent c5befc3965
commit 07b64dc217
2 changed files with 3 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import {resolve} from 'path'; import {join, resolve} from 'path';
import {register} from 'react-refresh/runtime'; import {register} from 'react-refresh/runtime';
@ -83,8 +83,7 @@ export async function createRoutesFromFiletree({importer, paths, resolver}) {
const last = parts[i + 1]; const last = parts[i + 1];
// Non-index is a sibling, create if necessary. // Non-index is a sibling, create if necessary.
if ('index' !== last) { if ('index' !== last) {
const offset = '/' === parts[i] ? 1 : 0; const nestedPath = join(...segments.slice(i));
const nestedPath = segments.slice(i + offset).join('/');
route = walk.find(({path}) => nestedPath === path); route = walk.find(({path}) => nestedPath === path);
if (!route) { if (!route) {
route = {path: nestedPath}; route = {path: nestedPath};

View File

@ -5,7 +5,7 @@ import {createRoutesFromContext} from '@flecks/react/router';
it('does not nest siblings', async () => { it('does not nest siblings', async () => {
const routes = await createRoutesFromContext(require.context('./filetree/siblings')); const routes = await createRoutesFromContext(require.context('./filetree/siblings'));
expect(routes) expect(routes)
.to.deep.equal([{path: '/', name: 'index'}, {path: 'test', name: 'sibling'}]); .to.deep.equal([{path: '/', name: 'index'}, {path: '/test', name: 'sibling'}]);
}); });
it('does nest siblings under parent', async () => { it('does nest siblings under parent', async () => {