8
8
routeBlockQueryRE ,
9
9
ROUTE_BLOCK_ID ,
10
10
ROUTES_LAST_LOAD_TIME ,
11
+ VIRTUAL_PREFIX ,
12
+ DEFINE_PAGE_QUERY_RE ,
11
13
} from './core/moduleConstants'
12
14
import {
13
15
Options ,
@@ -16,7 +18,6 @@ import {
16
18
mergeAllExtensions ,
17
19
} from './options'
18
20
import { createViteContext } from './core/vite'
19
- import { createFilter } from 'unplugin-utils'
20
21
import { join } from 'pathe'
21
22
import { appendExtensionListToPattern } from './core/utils'
22
23
import { MACRO_DEFINE_PAGE_QUERY } from './core/definePage'
@@ -50,43 +51,37 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
50
51
mergeAllExtensions ( options )
51
52
)
52
53
53
- // this is a larger filter that includes a bit too many files
54
- // the RouteFolderWatcher will filter it down to the actual files
55
- const filterPageComponents = createFilter (
56
- [
57
- ...options . routesFolder . flatMap ( ( routeOption ) =>
58
- pageFilePattern . map ( ( pattern ) => join ( routeOption . src , pattern ) )
59
- ) ,
60
- // importing the definePage block
61
- / \? .* \b d e f i n e P a g e \& v u e \b / ,
62
- ] ,
63
- options . exclude
54
+ const IDS_TO_INCLUDE = options . routesFolder . flatMap ( ( routeOption ) =>
55
+ pageFilePattern . map ( ( pattern ) => join ( routeOption . src , pattern ) )
64
56
)
65
57
66
58
const plugins : UnpluginOptions [ ] = [
67
59
{
68
60
name : 'unplugin-vue-router' ,
69
61
enforce : 'pre' ,
70
62
71
- resolveId ( id ) {
72
- if (
63
+ resolveId : {
64
+ filter : {
65
+ id : {
66
+ include : [
67
+ new RegExp ( `^${ MODULE_VUE_ROUTER_AUTO } $` ) ,
68
+ new RegExp ( `^${ MODULE_ROUTES_PATH } $` ) ,
69
+ routeBlockQueryRE ,
70
+ ] ,
71
+ } ,
72
+ } ,
73
+ handler ( id ) {
74
+ // vue-router/auto
73
75
// vue-router/auto-routes
74
- id === MODULE_ROUTES_PATH ||
75
- // NOTE: it wasn't possible to override or add new exports to vue-router
76
- // so we need to override it with a different package name
77
- id === MODULE_VUE_ROUTER_AUTO
78
- ) {
79
- // virtual module
80
- return asVirtualId ( id )
81
- }
82
-
83
- // this allows us to skip the route block module as a whole since we already parse it
84
- if ( routeBlockQueryRE . test ( id ) ) {
85
- return ROUTE_BLOCK_ID
86
- }
76
+ if ( id === MODULE_ROUTES_PATH || id === MODULE_VUE_ROUTER_AUTO ) {
77
+ // must be a virtual module
78
+ return asVirtualId ( id )
79
+ }
87
80
88
- // nothing to do, just for TS
89
- return
81
+ // otherwisse we know it matched the routeBlockQueryRE
82
+ // this allows us to skip the route block module as a whole since we already parse it
83
+ return ROUTE_BLOCK_ID
84
+ } ,
90
85
} ,
91
86
92
87
buildStart ( ) {
@@ -97,54 +92,57 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
97
92
ctx . stopWatcher ( )
98
93
} ,
99
94
100
- // we only need to transform page components
101
- transformInclude ( id ) {
102
- // console.log('filtering ' + id, filterPageComponents(id) ? '✅' : '❌')
103
- return filterPageComponents ( id )
104
- } ,
105
-
106
- transform ( code , id ) {
107
- // console.log('👋 Transforming', id)
108
- // remove the `definePage()` from the file or isolate it
109
- return ctx . definePageTransform ( code , id )
110
- } ,
111
-
112
- // loadInclude is necessary for webpack
113
- loadInclude ( id ) {
114
- if ( id === ROUTE_BLOCK_ID ) return true
115
- const resolvedId = getVirtualId ( id )
116
- return (
117
- resolvedId === MODULE_ROUTES_PATH ||
118
- resolvedId === MODULE_VUE_ROUTER_AUTO
119
- )
95
+ transform : {
96
+ filter : {
97
+ id : {
98
+ include : [ ...IDS_TO_INCLUDE , DEFINE_PAGE_QUERY_RE ] ,
99
+ exclude : options . exclude ,
100
+ } ,
101
+ } ,
102
+ handler ( code , id ) {
103
+ // remove the `definePage()` from the file or isolate it
104
+ return ctx . definePageTransform ( code , id )
105
+ } ,
120
106
} ,
121
107
122
- load ( id ) {
123
- // remove the <route> block as it's parsed by the plugin
124
- // stub it with an empty module
125
- if ( id === ROUTE_BLOCK_ID ) {
126
- return {
127
- code : `export default {}` ,
128
- map : null ,
108
+ load : {
109
+ filter : {
110
+ id : {
111
+ include : [
112
+ // virtualized ids only
113
+ new RegExp ( `^${ ROUTE_BLOCK_ID } $` ) ,
114
+ new RegExp ( `^${ VIRTUAL_PREFIX } ${ MODULE_VUE_ROUTER_AUTO } $` ) ,
115
+ new RegExp ( `^${ VIRTUAL_PREFIX } ${ MODULE_ROUTES_PATH } $` ) ,
116
+ ] ,
117
+ } ,
118
+ } ,
119
+ handler ( id ) {
120
+ // remove the <route> block as it's parsed by the plugin
121
+ // stub it with an empty module
122
+ if ( id === ROUTE_BLOCK_ID ) {
123
+ return {
124
+ code : `export default {}` ,
125
+ map : null ,
126
+ }
129
127
}
130
- }
131
128
132
- // we need to use a virtual module so that vite resolves the vue-router/auto-routes
133
- // dependency correctly
134
- const resolvedId = getVirtualId ( id )
129
+ // we need to use a virtual module so that vite resolves the vue-router/auto-routes
130
+ // dependency correctly
131
+ const resolvedId = getVirtualId ( id )
135
132
136
- // vue-router/auto-routes
137
- if ( resolvedId === MODULE_ROUTES_PATH ) {
138
- ROUTES_LAST_LOAD_TIME . update ( )
139
- return ctx . generateRoutes ( )
140
- }
133
+ // vue-router/auto-routes
134
+ if ( resolvedId === MODULE_ROUTES_PATH ) {
135
+ ROUTES_LAST_LOAD_TIME . update ( )
136
+ return ctx . generateRoutes ( )
137
+ }
141
138
142
- // vue-router/auto
143
- if ( resolvedId === MODULE_VUE_ROUTER_AUTO ) {
144
- return ctx . generateVueRouterProxy ( )
145
- }
139
+ // vue-router/auto
140
+ if ( resolvedId === MODULE_VUE_ROUTER_AUTO ) {
141
+ return ctx . generateVueRouterProxy ( )
142
+ }
146
143
147
- return // ok TS...
144
+ return // ok TS...
145
+ } ,
148
146
} ,
149
147
150
148
// improves DX
@@ -195,7 +193,10 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
195
193
if ( options . experimental . autoExportsDataLoaders ) {
196
194
plugins . push (
197
195
createAutoExportPlugin ( {
198
- filterPageComponents,
196
+ transformFilter : {
197
+ include : IDS_TO_INCLUDE ,
198
+ exclude : options . exclude ,
199
+ } ,
199
200
loadersPathsGlobs : options . experimental . autoExportsDataLoaders ,
200
201
root : options . root ,
201
202
} )
0 commit comments