forked from posva/unplugin-vue-router
/
index.ts
204 lines (180 loc) · 5.23 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
193
194
195
196
197
198
199
200
201
202
203
204
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'
import { Options, resolveOptions } from './options'
import { createViteContext } from './core/vite'
import { createFilter } from '@rollup/pluginutils'
import { join } from 'pathe'
export { Options }
export default createUnplugin<Options>((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() {
return ctx.scanPages()
},
buildEnd() {
if (options.logs) {
console.log('🛑 stopping watcher')
}
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)
},
// TODO: is it worth having a loadInclude?
load(id) {
// 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()
}
// remove the <route> block as it's parsed by the plugin
if (id === ROUTE_BLOCK_ID) {
return {
code: `export default {}`,
map: null,
}
}
},
// improves DX
vite: {
configureServer(server) {
ctx.setServerContext(createViteContext(server))
},
},
}
})
export { createRoutesContext }
export { getFileBasedRouteName, getPascalCaseRouteName } from './core/utils'
export type {
_RouteMapGeneric,
RouteRecordInfo,
} from './codegen/generateRouteMap'
export type {
// TODO: mark all of these as internals since the dynamically exposed versions are fully typed, these are just helpers
// to generate the convenient types
RouteLocationAsRelativeTyped,
RouteLocationAsRelativeTypedList,
RouteLocationAsPathTyped,
RouteLocationAsPathTypedList,
RouteLocationAsString,
RouteLocationTyped,
RouteLocationTypedList,
RouteLocationResolvedTyped,
RouteLocationResolvedTypedList,
RouteLocationNormalizedTyped,
RouteLocationNormalizedTypedList,
RouteLocationNormalizedLoadedTyped,
RouteLocationNormalizedLoadedTypedList,
} from './typeExtensions/routeLocation'
export type { NavigationGuard } from './typeExtensions/navigationGuards'
export type { _RouterTyped } from './typeExtensions/router'
export type {
RouterLinkTyped,
UseLinkFnTyped,
_UseLinkReturnTyped,
} from './typeExtensions/RouterLink'
export type {
ParamValue,
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
} from './codegen/generateRouteParams'
export { TreeNode, createPrefixTree } from './core/tree'
export {
createTreeNodeValue,
TreeNodeValueParam,
TreeNodeValueStatic,
} from './core/treeNodeValue'
// expose for generated type extensions
export type {
DefineLoaderOptions as _DefineLoaderOptions,
DataLoader as _DataLoader,
} from './data-fetching/defineLoader'
/**
* @deprecated use `VueRouterAutoImports` instead
*/
export const VueRouterExports: Array<string | [string, string]> = [
'useRoute',
'useRouter',
'defineLoader',
'definePage',
]
/**
* Adds useful auto imports to the AutoImport config:
* @example
* ```js
* import { VueRouterAutoImports } from 'unplugin-vue-router'
*
* AutoImport({
* imports: [VueRouterAutoImports],
* }),
* ```
*/
export const VueRouterAutoImports = {
'vue-router/auto': VueRouterExports,
}