-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.ts
192 lines (169 loc) · 5.07 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
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
import { createUnplugin } from 'unplugin'
import { createRoutesContext } from './core/context'
import {
MODULE_ROUTES_PATH,
MODULE_VUE_ROUTER,
getVirtualId as _getVirtualId,
asVirtualId as _asVirtualId,
routeBlockQueryRE,
ROUTE_BLOCK_ID,
} from './core/moduleConstants'
// TODO: export standalone createRoutesContext that resolves partial options
import { Options, resolveOptions, DEFAULT_OPTIONS } from './options'
import { createViteContext } from './core/vite'
import { createFilter } from '@rollup/pluginutils'
import { join } from 'pathe'
export * from './types'
export { DEFAULT_OPTIONS }
export default createUnplugin<Options | undefined>((opt = {}, meta) => {
const options = resolveOptions(opt)
const ctx = createRoutesContext(options)
function getVirtualId(id: string) {
if (options._inspect) return id
return _getVirtualId(id)
}
function asVirtualId(id: string) {
// for inspection
if (options._inspect) return id
return _asVirtualId(id)
}
// create the transform filter to detect `definePage()` inside page component
const pageFilePattern =
`**/*` +
(options.extensions.length === 1
? options.extensions[0]
: `.{${options.extensions
.map((extension) => extension.replace('.', ''))
.join(',')}}`)
const filterPageComponents = createFilter(
[
...options.routesFolder.map((routeOption) =>
join(routeOption.src, pageFilePattern)
),
// importing the definePage block
/definePage\&vue$/,
],
options.exclude
)
return {
name: 'unplugin-vue-router',
enforce: 'pre',
resolveId(id) {
if (id === MODULE_ROUTES_PATH) {
// virtual module
return asVirtualId(id)
}
// NOTE: it wasn't possible to override or add new exports to vue-router
// so we need to override it with a different package name
if (id === MODULE_VUE_ROUTER) {
return asVirtualId(id)
}
// this allows us to skip the route block module as a whole since we already parse it
if (routeBlockQueryRE.test(id)) {
return ROUTE_BLOCK_ID
}
},
buildStart() {
// TODO: how do we properly check if we are in dev mode?
return ctx.scanPages(true)
},
buildEnd() {
ctx.stopWatcher()
},
// we only need to transform page components
transformInclude(id) {
// console.log('filtering ' + id, filterPageComponents(id) ? '✅' : '❌')
return filterPageComponents(id)
},
transform(code, id) {
// console.log('👋 ', id)
return ctx.definePageTransform(code, id)
},
// loadInclude is necessary for webpack
loadInclude(id) {
if (id === ROUTE_BLOCK_ID) return true
const resolvedId = getVirtualId(id)
return (
resolvedId === MODULE_ROUTES_PATH || resolvedId === MODULE_VUE_ROUTER
)
},
load(id) {
// remove the <route> block as it's parsed by the plugin
if (id === ROUTE_BLOCK_ID) {
return {
code: `export default {}`,
map: null,
}
}
// we need to use a virtual module so that vite resolves the vue-router/auto-routes
// dependency correctly
const resolvedId = getVirtualId(id)
// vue-router/auto-routes
if (resolvedId === MODULE_ROUTES_PATH) {
return ctx.generateRoutes()
}
// vue-router/auto
if (resolvedId === MODULE_VUE_ROUTER) {
return ctx.generateVueRouterProxy()
}
},
// improves DX
vite: {
configureServer(server) {
ctx.setServerContext(createViteContext(server))
},
},
}
})
export { createRoutesContext }
export { getFileBasedRouteName, getPascalCaseRouteName } from './core/utils'
// Route Tree and edition
// FIXME: deprecated, remove in next major
export { createPrefixTree } from './core/tree'
export { createTreeNodeValue } from './core/treeNodeValue'
export { EditableTreeNode } from './core/extendRoutes'
// FIXME: deprecated, remove in next major
/**
* @deprecated use `VueRouterAutoImports` instead
*/
export const VueRouterExports: Array<
string | [importName: string, alias: string]
> = [
'useRoute',
'useRouter',
'defineBasicLoader',
'onBeforeRouteUpdate',
'onBeforeRouteLeave',
// NOTE: the typing seems broken locally, so instead we export it directly from unplugin-vue-router/runtime
// 'definePage',
]
/**
* Adds useful auto imports to the AutoImport config:
* @example
* ```js
* import { VueRouterAutoImports } from 'unplugin-vue-router'
*
* AutoImport({
* imports: [VueRouterAutoImports],
* }),
* ```
*/
export const VueRouterAutoImports: Record<
string,
Array<string | [importName: string, alias: string]>
> = {
'vue-router/auto': [
'useRoute',
'useRouter',
'defineBasicLoader',
'onBeforeRouteUpdate',
'onBeforeRouteLeave',
// NOTE: the typing seems broken locally, so instead we export it directly from unplugin-vue-router/runtime
// 'definePage',
],
'unplugin-vue-router/runtime': [
['definePage', 'definePage'],
// FIXME: remove after deprecation
['_defineLoader', 'defineLoader'],
],
}