generated from yunsii/starter-vite-react-library
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.docs.config.ts
60 lines (55 loc) · 2.08 KB
/
vite.docs.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import path from 'path'
import { mergeConfig } from 'vite'
import pages, { DefaultPageStrategy } from 'vite-plugin-react-pages'
import baseConfig from './vite.base.config'
import type { UserConfig } from 'vite'
// https://vitejs.dev/config/
export default mergeConfig(baseConfig, {
plugins: [
pages({
pagesDir: path.join(__dirname, 'docs/pages'),
pageStrategy: new DefaultPageStrategy({
extraFindPages: async (pagesDir, helpers) => {
const srcPath = path.join(__dirname, './src')
// show all component demos
helpers.watchFiles(
srcPath,
'**/demos/**/*.{[tj]sx,md?(x)}',
async (file, api) => {
const { relative, path: absolute } = file
const match = relative.match(/(.*)\/demos\/(.*)\.([tj]sx|mdx?)$/)
if (!match) {
throw new Error(`unexpected file: ${absolute}`)
}
const [, componentName, demoName] = match
const pageId = `/${componentName}`
// set page data
const runtimeDataPaths = api.getRuntimeData(pageId)
// the ?demo query will wrap the module with useful demoInfo
runtimeDataPaths[demoName] = `${absolute}?demo`
},
)
// find all component README
helpers.watchFiles(srcPath, '**/README.md?(x)', async (file, api) => {
const { relative, path: absolute } = file
const match = relative.match(/(.*)\/README\.mdx?$/)
if (!match) {
throw new Error(`unexpected file: ${absolute}`)
}
const [, componentName] = match
const pageId = `/${componentName}`
// set page data
const runtimeDataPaths = api.getRuntimeData(pageId)
runtimeDataPaths.main = absolute
// set page staticData
const staticData = api.getStaticData(pageId)
staticData.main = await helpers.extractStaticData(file)
})
},
}),
}),
],
build: {
outDir: 'dist-docs',
},
} as UserConfig)