forked from ckangwen/vite-plugin-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
150 lines (128 loc) · 4.13 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { resolve } from 'path'
import type { Plugin, ResolvedConfig, ModuleNode } from 'vite'
import { Route, ResolvedOptions, UserOptions, PageDirOptions } from './types'
import { getFilesFromPath } from './files'
import { generateRoutes, generateClientCode, updateRouteFromHMR } from './generate'
import { debug, normalizePath } from './utils'
import { parseVueRequest } from './query'
const ID = 'pages-generated'
function resolveOptions(userOptions: UserOptions): ResolvedOptions {
const {
pagesDir = ['src/pages'],
extensions = ['vue', 'js'],
importMode = 'async',
routeBlockLang = 'json5',
exclude = [],
syncIndex = true,
} = userOptions
const root = process.cwd()
let pagesDirOptions: PageDirOptions[] = []
if (typeof pagesDir === 'string') {
pagesDirOptions = pagesDirOptions.concat({ dir: pagesDir, baseRoute: '' })
}
else {
for (const dir of pagesDir) {
if (typeof dir === 'string')
pagesDirOptions = pagesDirOptions.concat({ dir, baseRoute: '' })
else if (dir as PageDirOptions)
pagesDirOptions = pagesDirOptions.concat(dir)
}
}
return Object.assign(
{},
{
routeBlockLang,
root,
pagesDir,
pagesDirOptions,
extensions,
importMode,
exclude,
syncIndex,
},
userOptions,
)
}
function routePlugin(userOptions: UserOptions = {}): Plugin {
let config: ResolvedConfig | undefined
let filesPath: string[] = []
let generatedRoutes: Route[] | null | undefined
let pagesDirPaths: string[] = []
const options: ResolvedOptions = resolveOptions(userOptions)
return {
name: 'vite-plugin-pages',
enforce: 'pre',
configResolved(_config) {
config = _config
options.root = config.root
},
resolveId(id) {
if (id === ID)
return ID
},
async load(id) {
if (id === ID) {
debug('Loading...')
debug('Loading Gen Routes: %O', generatedRoutes)
if (!generatedRoutes) {
generatedRoutes = []
filesPath = []
pagesDirPaths = []
for (const pageDir of options.pagesDirOptions) {
const pageDirPath = normalizePath(resolve(options.root, pageDir.dir))
pagesDirPaths = pagesDirPaths.concat(pageDirPath)
debug('Loading PageDir: %O', pageDirPath)
const files = await getFilesFromPath(pageDirPath, options)
filesPath = filesPath.concat(files.map(f => `${pageDirPath}/${f}`))
debug('FilesPath: %O', files)
const routes = generateRoutes(files, pageDir, pageDirPath, options)
generatedRoutes = generatedRoutes.concat(routes)
// debug('Routes: %O', generatedRoutes)
}
}
const clientCode = generateClientCode(generatedRoutes, options)
// debug('Client code: %O', clientCode)
return clientCode
}
},
async transform(code: string, id: string) {
const { query } = parseVueRequest(id)
if (query && query.vue && query.type === 'route') {
return {
code: 'export default {}',
map: null,
}
}
return {
code,
map: null,
}
},
async handleHotUpdate({ file, server, read }) {
const extensionsRE = new RegExp(`\\.(${options.extensions.join('|')})$`)
const matchedPath = (pagesDirPaths).find(p => file.startsWith(`${p}/`))
// Handle pages HMR
if (matchedPath && extensionsRE.test(file)) {
let needReload = false
// HMR on new file created
// Otherwise, handle HMR from custom block
if (!filesPath.includes(file)) {
generatedRoutes = null
needReload = true
}
else if (generatedRoutes) {
const content = await read()
needReload = updateRouteFromHMR(content, file, generatedRoutes, options)
}
if (needReload) {
const { moduleGraph } = server
const module = moduleGraph.getModuleById(ID)
debug('Reload for file: %s', file.replace(options.root, ''))
return [module] as ModuleNode[]
}
}
},
}
}
export * from './types'
export default routePlugin