Skip to content

Commit

Permalink
feat: add solid-router integration
Browse files Browse the repository at this point in the history
  • Loading branch information
oedotme committed Nov 5, 2022
1 parent 87ed454 commit b9ec72b
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"./react-router": {
"types": "./src/react-router.tsx",
"import": "./src/react-router.tsx"
},
"./solid-router": {
"types": "./src/solid-router.tsx",
"import": "./src/solid-router.tsx"
}
},
"typesVersions": {
Expand All @@ -40,6 +44,9 @@
],
"react-router": [
"./src/react-router.tsx"
],
"solid-router": [
"./src/solid-router.tsx"
]
}
},
Expand All @@ -51,11 +58,13 @@
"format": "prettier --write 'src/**/*.{ts,tsx}'"
},
"devDependencies": {
"@solidjs/router": "^0.5.0",
"@tanstack/react-location": "^3.7.4",
"@types/react": "^18.0.20",
"@types/react-dom": "^18.0.6",
"prettier": "^2.7.1",
"react-router-dom": "^6.4.1",
"solid-js": "^1.6.1",
"typescript": "^4.8.3",
"vite": "^3.1.2"
},
Expand Down
177 changes: 176 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/solid-router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** @jsxImportSource solid-js */
import { Component, lazy, ParentProps } from 'solid-js'
import { RouteDataFunc, RouteDataFuncArgs, RouteDefinition, Router, useRoutes } from '@solidjs/router'

import { generatePreservedRoutes, generateRegularRoutes } from './core'

type Module = { default: Component; Loader: RouteDataFunc }
type Route = { path?: string; component?: Component; children?: Route[] }

const PRESERVED = import.meta.glob<Module>('/src/pages/(_app|404).{jsx,tsx}', { eager: true })
const ROUTES = import.meta.glob<Module>(['/src/pages/**/[\\w[]*.{jsx,tsx}', '!**/(_app|404).*'])

const preservedRoutes = generatePreservedRoutes<Component>(PRESERVED)

const regularRoutes = generateRegularRoutes<Route, () => Promise<Module>>(ROUTES, (module) => ({
component: lazy(module),
data: async (args: RouteDataFuncArgs) => module().then((mod) => mod?.Loader?.(args)),
}))

const Fragment = (props: ParentProps) => <>{props.children}</>
const App = preservedRoutes?.['_app'] || Fragment
const NotFound = preservedRoutes?.['404'] || Fragment

const routes = [...regularRoutes, { path: '*', component: <NotFound /> }] as RouteDefinition[]

export const Routes = () => {
const Routes = useRoutes(routes)

return (
<Router>
<App>
<Routes />
</App>
</Router>
)
}

0 comments on commit b9ec72b

Please sign in to comment.