-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontext.ts
354 lines (290 loc) · 10.6 KB
/
context.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import path from 'node:path'
import process from 'node:process'
import type { FSWatcher } from 'chokidar'
import type { Logger, ViteDevServer } from 'vite'
import { normalizePath } from 'vite'
import { loadConfig } from 'unconfig'
import { slash } from '@antfu/utils'
import dbg from 'debug'
import { platform } from '@uni-helper/uni-env'
import type { PagesConfig } from './config/types'
import type { PageMetaDatum, PagePath, ResolvedOptions, SubPageMetaDatum, UserOptions } from './types'
import { writeDeclaration } from './declaration'
import {
debug,
invalidatePagesModule,
isTargetFile,
mergePageMetaDataArray,
useCachedPages,
} from './utils'
import { resolveOptions } from './options'
import { checkPagesJsonFile, getPageFiles, writeFileSync } from './files'
import { getRouteBlock, getRouteSfcBlock } from './customBlock'
import { OUTPUT_NAME } from './constant'
let lsatPagesJson = ''
const { setCache, hasChanged } = useCachedPages()
export class PageContext {
private _server: ViteDevServer | undefined
pagesGlobConfig: PagesConfig | undefined
pagesConfigSourcePaths: string[] = []
pagesPath: PagePath[] = []
subPagesPath: Record<string, PagePath[]> = {}
pageMetaData: PageMetaDatum[] = []
subPageMetaData: SubPageMetaDatum[] = []
resolvedPagesJSONPath = ''
rawOptions: UserOptions
root: string
options: ResolvedOptions
logger?: Logger
withUniPlatform = false
constructor(userOptions: UserOptions, viteRoot: string = process.cwd()) {
this.rawOptions = userOptions
this.root = slash(viteRoot)
debug.options('root', this.root)
this.options = resolveOptions(userOptions, this.root)
// debug logic
const debugOption = this.options.debug
if (debugOption) {
const prefix = 'vite-plugin-uni-pages:'
const suffix = typeof debugOption === 'boolean' ? '*' : debugOption
dbg.enable(`${prefix}${suffix}`)
}
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME)
debug.options(this.options)
}
setLogger(logger: Logger) {
this.logger = logger
}
async loadUserPagesConfig() {
const configSource = this.options.configSource
const { config, sources } = await loadConfig<PagesConfig>({ cwd: this.root, sources: configSource, defaults: {} })
this.pagesGlobConfig = config
this.pagesConfigSourcePaths = sources
debug.options(config)
}
async scanPages() {
const pageDirFiles = this.options.dirs.map((dir) => {
return { dir, files: getPagePaths(dir, this.options) }
})
this.pagesPath = pageDirFiles.map(page => page.files).flat()
debug.pages(this.pagesPath)
}
async scanSubPages() {
const subPagesPath: Record<string, PagePath[]> = {}
for (const dir of this.options.subPackages) {
const pagePaths = getPagePaths(dir, this.options)
subPagesPath[dir] = pagePaths
}
this.subPagesPath = subPagesPath
debug.subPages(this.subPagesPath)
}
setupViteServer(server: ViteDevServer) {
if (this._server === server)
return
this._server = server
this.setupWatcher(server.watcher)
}
async setupWatcher(watcher: FSWatcher) {
watcher.add(this.pagesConfigSourcePaths)
const targetDirs = [...this.options.dirs, ...this.options.subPackages].map(v => slash(path.resolve(this.root, v)))
const isInTargetDirs = (filePath: string) => targetDirs.some(v => slash(path.resolve(this.root, filePath)).startsWith(v))
watcher.on('add', async (path) => {
path = slash(path)
if (!isTargetFile(path))
return
if (!isInTargetDirs(path))
return
debug.pages(`File added: ${path}`)
if (await this.updatePagesJSON())
this.onUpdate()
})
watcher.on('change', async (path) => {
path = slash(path)
if (!isTargetFile(path))
return
if (!isInTargetDirs(path))
return
debug.pages(`File changed: ${path}`)
debug.pages(targetDirs)
debug.pages(isInTargetDirs(path))
if (await this.updatePagesJSON(path))
this.onUpdate()
})
watcher.on('change', async (path) => {
if (this.pagesConfigSourcePaths.includes(path)) {
debug.pages(`Config source changed: ${path}`)
if (await this.updatePagesJSON())
this.onUpdate()
}
})
watcher.on('unlink', async (path) => {
path = slash(path)
if (!isTargetFile(path))
return
if (!isInTargetDirs(path))
return
debug.pages(`File removed: ${path}`)
if (await this.updatePagesJSON())
this.onUpdate()
})
}
onUpdate() {
if (!this._server)
return
invalidatePagesModule(this._server)
debug.hmr('Reload generated pages.')
this._server.ws.send({
type: 'full-reload',
})
}
async parsePage(page: PagePath): Promise<PageMetaDatum> {
const { relativePath, absolutePath } = page
const routeSfcBlock = await getRouteSfcBlock(absolutePath)
const routeBlock = await getRouteBlock(absolutePath, routeSfcBlock, this.options)
setCache(absolutePath, routeSfcBlock)
const relativePathWithFileName = relativePath.replace(path.extname(relativePath), '')
const pageMetaDatum: PageMetaDatum = {
path: normalizePath(relativePathWithFileName),
type: routeBlock?.attr.type ?? 'page',
}
if (routeBlock)
Object.assign(pageMetaDatum, routeBlock.content)
return pageMetaDatum
}
/**
* parse pages rules && set page type
* @param pages page path array
* @param type page type
* @param overrides custom page config
* @returns pages rules
*/
async parsePages(pages: PagePath[], type: 'main' | 'sub', overrides?: PageMetaDatum[]) {
const generatedPageMetaData = await Promise.all(pages.map(async page => await this.parsePage(page)))
const customPageMetaData = overrides || []
const result = customPageMetaData.length
? mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData))
: generatedPageMetaData
return type === 'main' ? this.setHomePage(result) : result
}
/**
* set home page
* @param result pages rules array
* @returns pages rules
*/
setHomePage(result: PageMetaDatum[]): PageMetaDatum[] {
const hasHome = result.some(({ type }) => type === 'home')
if (!hasHome) {
const isFoundHome = result.some((item) => {
const isFound = this.options.homePage.find(v => (v === item.path))
if (isFound)
item.type = 'home'
return isFound
})
if (!isFoundHome) {
this.logger?.warn('No home page found, check the configuration of pages.config.ts, or add the `homePage` option to UniPages in vite.config.js, or add `type="home"` to the routeBlock of your vue page.', {
timestamp: true,
})
}
}
result.sort(page => (page.type === 'home' ? -1 : 0))
return result
}
async mergePageMetaData() {
const pageMetaData = await this.parsePages(this.pagesPath, 'main', this.pagesGlobConfig?.pages)
this.pageMetaData = pageMetaData
debug.pages(this.pageMetaData)
}
async mergeSubPageMetaData() {
const subPageMaps: Record<string, PageMetaDatum[]> = {}
const subPackages = this.pagesGlobConfig?.subPackages || []
for (const [dir, pages] of Object.entries(this.subPagesPath)) {
const basePath = slash(path.join(this.options.root, this.options.outDir))
const root = slash(path.relative(basePath, path.join(this.options.root, dir)))
const globPackage = subPackages?.find(v => v.root === root)
subPageMaps[root] = await this.parsePages(pages, 'sub', globPackage?.pages)
subPageMaps[root] = subPageMaps[root].map(page => ({ ...page, path: slash(path.relative(root, page.path)) }))
}
// Inherit subPackages that do not exist in the config
for (const { root, pages } of subPackages) {
if (root && !subPageMaps[root])
subPageMaps[root] = pages || []
}
const subPageMetaData = Object.keys(subPageMaps).map(root => ({ root, pages: subPageMaps[root] }))
this.subPageMetaData = subPageMetaData
debug.subPages(this.subPageMetaData)
}
async updatePagesJSON(filepath?: string) {
if (filepath) {
if (!await hasChanged(filepath)) {
debug.cache(`The route block on page ${filepath} did not send any changes, skipping`)
return false
}
}
checkPagesJsonFile(this.resolvedPagesJSONPath)
this.options.onBeforeLoadUserConfig(this)
await this.loadUserPagesConfig()
this.options.onAfterLoadUserConfig(this)
if (this.options.mergePages) {
this.options.onBeforeScanPages(this)
await this.scanPages()
await this.scanSubPages()
this.options.onAfterScanPages(this)
}
this.options.onBeforeMergePageMetaData(this)
await this.mergePageMetaData()
await this.mergeSubPageMetaData()
this.options.onAfterMergePageMetaData(this)
const pagesMap = new Map()
const pages = this.withUniPlatform
? this.pageMetaData.filter(v => !/\..*$/.test(v.path) || v.path.includes(platform)).map(v => ({ ...v, path: v.path.replace(/\..*$/, '') }))
: this.pageMetaData
pages.forEach(v => pagesMap.set(v.path, v))
this.pageMetaData = [...pagesMap.values()]
this.options.onBeforeWriteFile(this)
const data = {
...this.pagesGlobConfig,
pages: this.pageMetaData,
subPackages: this.subPageMetaData,
}
const pagesJson = JSON.stringify(data, null, this.options.minify ? undefined : 2)
this.generateDeclaration()
if (lsatPagesJson === pagesJson) {
debug.pages('PagesJson Not have change')
return false
}
writeFileSync(this.resolvedPagesJSONPath, pagesJson)
lsatPagesJson = pagesJson
this.options.onAfterWriteFile(this)
return true
}
virtualModule() {
const pages = `export const pages = ${this.resolveRoutes()};`
const subPackages = `export const subPackages = ${this.resolveSubRoutes()};`
return [pages, subPackages].join('\n')
}
resolveRoutes() {
return JSON.stringify(this.pageMetaData, null, 2)
}
resolveSubRoutes() {
return JSON.stringify(this.subPageMetaData, null, 2)
}
generateDeclaration() {
if (!this.options.dts)
return
debug.declaration('generating')
return writeDeclaration(this, this.options.dts)
}
}
function getPagePaths(dir: string, options: ResolvedOptions) {
const pagesDirPath = slash(path.resolve(options.root, dir))
const basePath = slash(path.join(options.root, options.outDir))
const files = getPageFiles(pagesDirPath, options)
debug.pages(dir, files)
const pagePaths = files
.map(file => slash(file))
.map(file => ({
relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
absolutePath: slash(path.resolve(pagesDirPath, file)),
}))
return pagePaths
}