Skip to content

Commit 10484ca

Browse files
feat: support preload assets
1 parent a587673 commit 10484ca

File tree

14 files changed

+454
-283
lines changed

14 files changed

+454
-283
lines changed

examples/multiple-pages/src/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import React from 'react'
2-
import { Layout } from './Layout'
32
import './App.css'
43

4+
// import { Layout } from './Layout'
5+
6+
const Layout = React.lazy(() => import('./Layout'))
7+
58
const pages = import.meta.glob<any>('./pages/**/*.tsx')
69
console.log('🚀 ~ file: App.tsx:7 ~ pages:', pages)
710

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

1216
if (path.endsWith('/')) {
1317
return {
1418
index: true,
1519
Component: React.lazy(component),
20+
entry,
1621
}
1722
}
1823
return {
1924
path,
2025
Component: React.lazy(component),
26+
entry,
2127
}
2228
})
2329

@@ -26,6 +32,7 @@ export const routes = [
2632
path: '/',
2733
element: <Layout />,
2834
children,
35+
entry: 'src/Layout.tsx',
2936
},
3037
]
3138

examples/multiple-pages/src/Layout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Suspense } from 'react'
22
import { Outlet } from 'react-router-dom'
3+
import './layout.css'
34

4-
export function Layout() {
5+
export default function Layout() {
56
return (
67
<>
78
{/* <Head>
@@ -11,7 +12,7 @@ export function Layout() {
1112
<title>head test</title>
1213
</Head> */}
1314
<main>
14-
<h1>Layout</h1>
15+
<h1 className="layout">Layout</h1>
1516
<Suspense>
1617
<Outlet />
1718
</Suspense>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a-count {
2+
color: pink;
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useState } from 'react'
2+
import './a-count.css'
3+
4+
export default function ACount() {
5+
const [count, setCount] = useState(0)
6+
7+
return (
8+
<button className="a-count" onClick={() => setCount(prev => ++prev)}>{count}</button>
9+
)
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.layout {
2+
color: #333;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import { lazy } from 'react'
12
import { useNavigate } from 'react-router-dom'
23
import './a.css'
34

5+
const ACount = lazy(() => import('../components/a-count'))
6+
47
export default function A() {
58
const navigate = useNavigate()
69
return (
710
<>
811
<h1 className="a">Page A</h1>
912
<button onClick={() => navigate('/')}>to index</button>
13+
<ACount />
1014
</>
1115
)
1216
}

examples/multiple-pages/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react-swc'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
ssgOptions: {
8+
formatting: 'minify',
9+
},
710
})

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,24 @@
8080
"html5parser": "^2.0.2",
8181
"jsdom": "^22.1.0",
8282
"kolorist": "^1.8.0",
83+
"prettier": "^3.0.0",
8384
"react-helmet-async": "^1.3.0",
8485
"yargs": "^17.7.2"
8586
},
8687
"devDependencies": {
87-
"@ririd/eslint-config": "0.5.1",
88+
"@ririd/eslint-config": "0.6.0",
8889
"@types/fs-extra": "^11.0.1",
8990
"@types/html-minifier": "^4.0.2",
9091
"@types/jsdom": "^21.1.1",
9192
"@types/node": "^18.15.11",
93+
"@types/prettier": "^2.7.3",
9294
"@types/react": "^18.2.14",
9395
"@types/react-dom": "^18.2.6",
94-
"@types/react-helmet-async": "^1.0.3",
9596
"@types/yargs": "^17.0.24",
9697
"bumpp": "^9.1.0",
9798
"critters": "^0.0.19",
98-
"eslint": "^8.40.0",
99-
"esno": "^0.16.3",
99+
"eslint": "^8.45.0",
100+
"esno": "^0.17.0",
100101
"p-queue": "^7.3.4",
101102
"react": "^18.2.0",
102103
"react-dom": "^18.2.0",

0 commit comments

Comments
 (0)