Skip to content

Commit

Permalink
feat: support preload assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Jul 24, 2023
1 parent a587673 commit 10484ca
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 283 deletions.
9 changes: 8 additions & 1 deletion examples/multiple-pages/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import React from 'react'
import { Layout } from './Layout'
import './App.css'

// import { Layout } from './Layout'

const Layout = React.lazy(() => import('./Layout'))

const pages = import.meta.glob<any>('./pages/**/*.tsx')
console.log('🚀 ~ file: App.tsx:7 ~ pages:', pages)

Check warning on line 9 in examples/multiple-pages/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

const children = Object.entries(pages).map(([filepath, component]) => {
let path = filepath.split('/pages')[1]
path = path.split('.')[0].replace('index', '')
const entry = `src${filepath.slice(1)}`

if (path.endsWith('/')) {
return {
index: true,
Component: React.lazy(component),
entry,
}
}
return {
path,
Component: React.lazy(component),
entry,
}
})

Expand All @@ -26,6 +32,7 @@ export const routes = [
path: '/',
element: <Layout />,
children,
entry: 'src/Layout.tsx',
},
]

Expand Down
5 changes: 3 additions & 2 deletions examples/multiple-pages/src/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Suspense } from 'react'
import { Outlet } from 'react-router-dom'
import './layout.css'

export function Layout() {
export default function Layout() {
return (
<>
{/* <Head>
Expand All @@ -11,7 +12,7 @@ export function Layout() {
<title>head test</title>
</Head> */}
<main>
<h1>Layout</h1>
<h1 className="layout">Layout</h1>
<Suspense>
<Outlet />
</Suspense>
Expand Down
3 changes: 3 additions & 0 deletions examples/multiple-pages/src/components/a-count.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a-count {
color: pink;
}
10 changes: 10 additions & 0 deletions examples/multiple-pages/src/components/a-count.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from 'react'
import './a-count.css'

export default function ACount() {
const [count, setCount] = useState(0)

return (
<button className="a-count" onClick={() => setCount(prev => ++prev)}>{count}</button>
)
}
3 changes: 3 additions & 0 deletions examples/multiple-pages/src/layout.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.layout {
color: #333;
}
4 changes: 4 additions & 0 deletions examples/multiple-pages/src/pages/a.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { lazy } from 'react'
import { useNavigate } from 'react-router-dom'
import './a.css'

const ACount = lazy(() => import('../components/a-count'))

export default function A() {
const navigate = useNavigate()
return (
<>
<h1 className="a">Page A</h1>
<button onClick={() => navigate('/')}>to index</button>
<ACount />
</>
)
}
3 changes: 3 additions & 0 deletions examples/multiple-pages/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react-swc'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
ssgOptions: {

Check failure on line 7 in examples/multiple-pages/vite.config.ts

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type '{ plugins: PluginOption[][]; ssgOptions: { formatting: string; }; }' is not assignable to parameter of type 'UserConfigExport'.
formatting: 'minify',
},
})
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,24 @@
"html5parser": "^2.0.2",
"jsdom": "^22.1.0",
"kolorist": "^1.8.0",
"prettier": "^3.0.0",
"react-helmet-async": "^1.3.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@ririd/eslint-config": "0.5.1",
"@ririd/eslint-config": "0.6.0",
"@types/fs-extra": "^11.0.1",
"@types/html-minifier": "^4.0.2",
"@types/jsdom": "^21.1.1",
"@types/node": "^18.15.11",
"@types/prettier": "^2.7.3",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"@types/react-helmet-async": "^1.0.3",
"@types/yargs": "^17.0.24",
"bumpp": "^9.1.0",
"critters": "^0.0.19",
"eslint": "^8.40.0",
"esno": "^0.16.3",
"eslint": "^8.45.0",
"esno": "^0.17.0",
"p-queue": "^7.3.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Loading

0 comments on commit 10484ca

Please sign in to comment.