-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcontext.ts
315 lines (276 loc) · 9.68 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
import { ResolvedOptions } from '../options'
import { TreeNode, PrefixTree } from './tree'
import { promises as fs } from 'fs'
import { asRoutePath, ImportsMap, logTree, throttle } from './utils'
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
import { generateFilePathToRouteNamesMap } from '../codegen/generateFilePathToRouteNamesMap'
import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER_AUTO } from './moduleConstants'
import { generateRouteRecord } from '../codegen/generateRouteRecords'
import fg from 'fast-glob'
import { dirname, relative, resolve } from 'pathe'
import { ServerContext } from '../options'
import { getRouteBlock } from './customBlock'
import {
RoutesFolderWatcher,
HandlerContext,
resolveFolderOptions,
} from './RoutesFolderWatcher'
import { generateDTS as _generateDTS } from '../codegen/generateDTS'
import { generateVueRouterProxy as _generateVueRouterProxy } from '../codegen/vueRouterModule'
import { definePageTransform, extractDefinePageNameAndPath } from './definePage'
import { EditableTreeNode } from './extendRoutes'
import { isPackageExists as isPackageInstalled } from 'local-pkg'
import { ts } from '../utils'
export function createRoutesContext(options: ResolvedOptions) {
const { dts: preferDTS, root, routesFolder } = options
const dts =
preferDTS === false
? false
: preferDTS === true
? resolve(root, 'typed-router.d.ts')
: resolve(root, preferDTS)
const routeTree = new PrefixTree(options)
const editableRoutes = new EditableTreeNode(routeTree)
const logger = new Proxy(console, {
get(target, prop) {
const res = Reflect.get(target, prop)
if (typeof res === 'function') {
return options.logs ? res : () => {}
}
return res
},
})
// populated by the initial scan pages
const watchers: RoutesFolderWatcher[] = []
async function scanPages(startWatchers = true) {
if (options.extensions.length < 1) {
throw new Error(
'"extensions" cannot be empty. Please specify at least one extension.'
)
}
// initial scan was already done
if (watchers.length > 0) {
return
}
// get the initial list of pages
await Promise.all(
routesFolder
.map((folder) => resolveFolderOptions(options, folder))
.map((folder) => {
if (startWatchers) {
watchers.push(setupWatcher(new RoutesFolderWatcher(folder)))
}
// the ignore option must be relative to cwd or absolute
const ignorePattern = folder.exclude.map((f) =>
// if it starts with ** then it will work as expected
f.startsWith('**') ? f : relative(folder.src, f)
)
return fg(folder.pattern, {
cwd: folder.src,
// TODO: do they return the symbolic link path or the original file?
// followSymbolicLinks: false,
ignore: ignorePattern,
}).then((files) =>
Promise.all(
files
// ensure consistent files in Windows/Unix and absolute paths
.map((file) => resolve(folder.src, file))
.map((file) =>
addPage({
routePath: asRoutePath(folder, file),
filePath: file,
})
)
)
)
})
)
for (const route of editableRoutes) {
await options.extendRoute?.(route)
}
// immediately write the files without the throttle
await _writeConfigFiles()
}
async function writeRouteInfoToNode(node: TreeNode, filePath: string) {
const content = await fs.readFile(filePath, 'utf8')
// TODO: cache the result of parsing the SFC (in the extractDefinePageAndName) so the transform can reuse the parsing
node.hasDefinePage ||= content.includes('definePage')
// TODO: track if it changed and to not always trigger HMR
const definedPageNameAndPath = extractDefinePageNameAndPath(
content,
filePath
)
// TODO: track if it changed and if generateRoutes should be called again
const routeBlock = getRouteBlock(filePath, content, options)
// TODO: should warn if hasDefinePage and customRouteBlock
// if (routeBlock) logger.log(routeBlock)
node.setCustomRouteBlock(filePath, {
...routeBlock,
...definedPageNameAndPath,
})
}
async function addPage(
{ filePath, routePath }: HandlerContext,
triggerExtendRoute = false
) {
logger.log(`added "${routePath}" for "${filePath}"`)
const node = routeTree.insert(routePath, filePath)
await writeRouteInfoToNode(node, filePath)
if (triggerExtendRoute) {
await options.extendRoute?.(new EditableTreeNode(node))
}
// TODO: trigger HMR vue-router/auto
server?.updateRoutes()
}
async function updatePage({ filePath, routePath }: HandlerContext) {
logger.log(`updated "${routePath}" for "${filePath}"`)
const node = routeTree.getChild(filePath)
if (!node) {
logger.warn(`Cannot update "${filePath}": Not found.`)
return
}
await writeRouteInfoToNode(node, filePath)
await options.extendRoute?.(new EditableTreeNode(node))
// no need to manually trigger the update of vue-router/auto-routes because
// the change of the vue file will trigger HMR
}
function removePage({ filePath, routePath }: HandlerContext) {
logger.log(`remove "${routePath}" for "${filePath}"`)
routeTree.removeChild(filePath)
// TODO: HMR vue-router/auto
server?.updateRoutes()
}
function setupWatcher(watcher: RoutesFolderWatcher) {
logger.log(`🤖 Scanning files in ${watcher.src}`)
return watcher
.on('change', async (ctx) => {
await updatePage(ctx)
writeConfigFiles()
})
.on('add', async (ctx) => {
await addPage(ctx, true)
writeConfigFiles()
})
.on('unlink', (ctx) => {
removePage(ctx)
writeConfigFiles()
})
// TODO: handle folder removal: apparently chokidar only emits a raw event when deleting a folder instead of the
// unlinkDir event
}
function generateRoutes() {
const importsMap = new ImportsMap()
const routeList = `export const routes = ${generateRouteRecord(
routeTree,
options,
importsMap
)}\n`
let hmr = ts`
export function handleHotUpdate(_router, _hotUpdateCallback) {
if (import.meta.hot) {
import.meta.hot.data.router = _router
import.meta.hot.data.router_hotUpdateCallback = _hotUpdateCallback
}
}
if (import.meta.hot) {
import.meta.hot.accept((mod) => {
const router = import.meta.hot.data.router
if (!router) {
import.meta.hot.invalidate('[unplugin-vue-router:HMR] Cannot replace the routes because there is no active router. Reloading.')
return
}
router.clearRoutes()
for (const route of mod.routes) {
router.addRoute(route)
}
// call the hotUpdateCallback for custom updates
import.meta.hot.data.router_hotUpdateCallback?.(mod.routes)
const route = router.currentRoute.value
router.replace({
...route,
// NOTE: we should be able to just do ...route but the router
// currently skips resolving and can give errors with renamed routes
// so we explicitly set remove matched and name
name: undefined,
matched: undefined,
force: true
})
})
}
`
// generate the list of imports
let imports = importsMap.toString()
// add an empty line for readability
if (imports) {
imports += '\n'
}
const newAutoRoutes = `${imports}${routeList}${hmr}\n`
// prepend it to the code
return newAutoRoutes
}
function generateDTS(): string {
return _generateDTS({
vueRouterModule: MODULE_VUE_ROUTER_AUTO,
routesModule: MODULE_ROUTES_PATH,
routeNamedMap: generateRouteNamedMap(routeTree),
filePathToRouteNamesMap: generateFilePathToRouteNamesMap(routeTree, {
root,
}),
})
}
// NOTE: this code needs to be generated because otherwise it doesn't go through transforms and `vue-router/auto-routes`
// cannot be resolved.
const isPiniaColadaInstalled = isPackageInstalled('@pinia/colada')
function generateVueRouterProxy() {
return _generateVueRouterProxy(MODULE_ROUTES_PATH, options, {
addPiniaColada: isPiniaColadaInstalled,
})
}
let lastDTS: string | undefined
async function _writeConfigFiles() {
logger.time('writeConfigFiles')
if (options.beforeWriteFiles) {
await options.beforeWriteFiles(editableRoutes)
logger.timeLog('writeConfigFiles', 'beforeWriteFiles()')
}
logTree(routeTree, logger.log)
if (dts) {
const content = generateDTS()
if (lastDTS !== content) {
await fs.mkdir(dirname(dts), { recursive: true })
await fs.writeFile(dts, content, 'utf-8')
logger.timeLog('writeConfigFiles', 'wrote dts file')
lastDTS = content
}
}
logger.timeEnd('writeConfigFiles')
}
// debounce of 100ms + throttle of 500ms
// => Initially wait 100ms (renames are actually remove and add but we rather write once) (debounce)
// subsequent calls after the first execution will wait 500ms-100ms to execute (throttling)
const writeConfigFiles = throttle(_writeConfigFiles, 500, 100)
function stopWatcher() {
if (watchers.length) {
logger.log('🛑 stopping watcher')
watchers.forEach((watcher) => watcher.close())
}
}
let server: ServerContext | undefined
function setServerContext(_server: ServerContext) {
server = _server
}
return {
scanPages,
writeConfigFiles,
setServerContext,
stopWatcher,
generateRoutes,
generateVueRouterProxy,
definePageTransform(code: string, id: string) {
return definePageTransform({
code,
id,
})
},
}
}