Skip to content

Commit

Permalink
feat: react-router-config support
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed May 4, 2021
1 parent 9f47510 commit 0c86290
Show file tree
Hide file tree
Showing 31 changed files with 871 additions and 260 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "@antfu/eslint-config",
"ignorePatterns": ["dist", "**/__*__/**"]
"ignorePatterns": ["dist", "**/__*__/**"],
"rules": {
"no-use-before-define": "off"
}
}
8 changes: 6 additions & 2 deletions client.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
declare module 'pages-generated' {
// eslint-disable-next-line import/no-duplicates
import { RouteRecordRaw } from 'vue-router'
const routes: RouteRecordRaw[]
// eslint-disable-next-line import/no-duplicates
import { RouteConfig } from 'react-router-config'
const routes: RouteRecordRaw[] | RouteConfig[]
export default routes
}

declare module 'virtual:generated-pages' {
// eslint-disable-next-line import/no-duplicates
import { RouteRecordRaw } from 'vue-router'
const routes: RouteRecordRaw[]
// eslint-disable-next-line import/no-duplicates
import { RouteConfig } from 'react-router-config'
const routes: RouteRecordRaw[] | RouteConfig[]
export default routes
}
2 changes: 1 addition & 1 deletion examples/nuxt-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cross-env": "^7.0.3",
"nodemon": "^2.0.7",
"typescript": "^4.2.4",
"vite": "^2.2.2",
"vite": "^2.2.4",
"vite-plugin-md": "^0.6.3",
"vite-plugin-pages": "workspace:*",
"vite-plugin-restart": "^0.0.2"
Expand Down
5 changes: 5 additions & 0 deletions examples/react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.DS_Store
dist
dist-ssr
*.local
13 changes: 13 additions & 0 deletions examples/react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions examples/react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "react",
"version": "0.0.0",
"scripts": {
"dev": "nodemon --watch ../../dist/*.js -x 'cross-env DEBUG=vite-plugin-pages:* vite'",
"build": "cross-env DEBUG=vite-plugin-pages vite build",
"serve": "vite preview"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router": "^5.2.0",
"react-router-config": "^5.1.1",
"react-router-dom": "^5.2.0"
},
"devDependencies": {
"@types/react": "^17.0.4",
"@types/react-dom": "^17.0.3",
"@types/react-router-config": "^5.0.2",
"@vitejs/plugin-react-refresh": "^1.3.3",
"cross-env": "^7.0.3",
"nodemon": "^2.0.7",
"typescript": "^4.2.4",
"vite": "^2.2.4",
"vite-plugin-pages": "workspace:*"
}
}
15 changes: 15 additions & 0 deletions examples/react/src/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/react/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
7 changes: 7 additions & 0 deletions examples/react/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions examples/react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react'
import ReactDOM from 'react-dom'
import {
BrowserRouter as Router,
} from 'react-router-dom'
import { renderRoutes } from 'react-router-config'
import './index.css'
import routes from 'virtual:generated-pages'

console.log(routes)

ReactDOM.render(
<Router>
{/* kick it all off with the root route */}
{renderRoutes(routes)}
</Router>,
document.getElementById('root'),
)

export {
routes,
}
9 changes: 9 additions & 0 deletions examples/react/src/pages/[...all].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const all: React.FC = () => {
return (
<p>...all route</p>
)
}

export default all
9 changes: 9 additions & 0 deletions examples/react/src/pages/[sensor]/current.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const current: React.FC = () => {
return (
<p>/[sensor]/current.vue</p>
)
}

export default current
9 changes: 9 additions & 0 deletions examples/react/src/pages/__test__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const index: React.FC = () => {
return (
<p>test default exclude</p>
)
}

export default index
14 changes: 14 additions & 0 deletions examples/react/src/pages/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import { renderRoutes, RouteConfigComponentProps } from 'react-router-config'

const component: React.FC<RouteConfigComponentProps> = ({ route }) => {
return (
<>
<p>nested about view:</p>
{renderRoutes(route?.routes)}
{console.log(route)}
</>
)
}

export default component
9 changes: 9 additions & 0 deletions examples/react/src/pages/about/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const index: React.FC = () => {
return (
<p>about/index.vue</p>
)
}

export default index
13 changes: 13 additions & 0 deletions examples/react/src/pages/blog/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { matchRoutes, RouteConfigComponentProps } from 'react-router-config'
import routes from 'virtual:generated-pages'

const components: React.FC<RouteConfigComponentProps> = ({ location }) => {
return (
<>
<p>blog/[id].vue: { matchRoutes<{ id: string }>(routes, location.pathname)[0].match.params.id }</p>
</>
)
}

export default components
18 changes: 18 additions & 0 deletions examples/react/src/pages/blog/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import { Link } from 'react-router-dom'

const components: React.FC = () => {
return (
<>
<p>blog/index.vue</p>
<Link to="/blog/1b234bk12b3">
id: 1b234bk12b3
</Link> |
<Link to="/blog/today">
today
</Link>
</>
)
}

export default components
9 changes: 9 additions & 0 deletions examples/react/src/pages/blog/today/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const index: React.FC = () => {
return (
<p>blog/today/index.vue</p>
)
}

export default index
9 changes: 9 additions & 0 deletions examples/react/src/pages/components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const components: React.FC = () => {
return (
<h1>Test Components name</h1>
)
}

export default components
27 changes: 27 additions & 0 deletions examples/react/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import { Link } from 'react-router-dom'

const index: React.FC = () => {
return (
<div>
<p>index.vue</p>
<Link to="/blog">
blog
</Link> |
<Link to="/about">
about
</Link> |
<Link to="/components">
components
</Link> |
<Link to="/markdown">
markdown
</Link> |
<Link to="/xxx">
not exits
</Link>
</div>
)
}

export default index
20 changes: 20 additions & 0 deletions examples/react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["./src"]
}
21 changes: 21 additions & 0 deletions examples/react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import Pages from 'vite-plugin-pages'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
reactRefresh(),
Pages({
pagesDir: [
{ dir: 'src/pages', baseRoute: '' },
{ dir: 'src/features/admin/pages', baseRoute: 'admin' },
],
extensions: ['tsx'],
syncIndex: false,
importMode: 'sync',
replaceSquareBrackets: true,
react: true,
}),
],
})
2 changes: 1 addition & 1 deletion examples/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cross-env": "^7.0.3",
"nodemon": "^2.0.7",
"typescript": "^4.2.4",
"vite": "^2.2.2",
"vite": "^2.2.4",
"vite-plugin-md": "^0.6.3",
"vite-plugin-pages": "workspace:*",
"vite-plugin-restart": "^0.0.2"
Expand Down
4 changes: 2 additions & 2 deletions examples/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@types/compression": "^1.7.0",
"@types/express": "^4.17.11",
"@types/node": "^14.14.41",
"@types/node": "^14.14.43",
"@vitejs/plugin-vue": "^1.2.2",
"@vue/compiler-sfc": "^3.0.11",
"@vue/server-renderer": "^3.0.11",
Expand All @@ -29,7 +29,7 @@
"nodemon": "^2.0.7",
"serve-static": "^1.14.1",
"typescript": "^4.2.4",
"vite": "^2.2.2",
"vite": "^2.2.4",
"vite-plugin-pages": "workspace:*",
"vite-plugin-restart": "^0.0.2"
}
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"deep-equal": "^2.0.5",
"fast-glob": "^3.2.5",
"json5": "^2.2.0",
"react-router-dom": "^5.2.0",
"yaml": "^2.0.0-5"
},
"peerDependencies": {
Expand All @@ -48,17 +49,18 @@
"@types/debug": "^4.1.5",
"@types/deep-equal": "^1.0.1",
"@types/glob-to-regexp": "^0.4.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.41",
"@types/jest": "^26.0.23",
"@types/node": "^14.14.43",
"@types/react-router-dom": "^5.1.7",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"eslint": "^7.25.0",
"jest": "^26.6.3",
"jest-esbuild": "^0.1.6",
"rollup": "^2.45.2",
"rollup": "^2.47.0",
"ts-node": "^9.1.1",
"tsup": "^4.9.2",
"tsup": "^4.10.1",
"typescript": "^4.2.4",
"vite": "^2.2.2",
"vite": "^2.2.4",
"vue-router": "^4.0.6"
}
}
Loading

0 comments on commit 0c86290

Please sign in to comment.