-
-
Notifications
You must be signed in to change notification settings - Fork 103
perf: use hook filters #631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
4c2d3eb
8b88203
62d9945
39d1a81
d6c8214
c4562ab
5645643
7d96498
aeb7fe5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import { | |
routeBlockQueryRE, | ||
ROUTE_BLOCK_ID, | ||
ROUTES_LAST_LOAD_TIME, | ||
VIRTUAL_PREFIX, | ||
exactRegex, | ||
} from './core/moduleConstants' | ||
import { | ||
Options, | ||
|
@@ -50,15 +52,18 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => { | |
mergeAllExtensions(options) | ||
) | ||
|
||
const IDS_TO_INCLUDE = options.routesFolder.flatMap((routeOption) => | ||
pageFilePattern.map((pattern) => join(routeOption.src, pattern)) | ||
) | ||
const DEFINE_PAGE_QUERY_RE = /\?.*\bdefinePage\&vue\b/ | ||
|
||
// this is a larger filter that includes a bit too many files | ||
// the RouteFolderWatcher will filter it down to the actual files | ||
const filterPageComponents = createFilter( | ||
[ | ||
...options.routesFolder.flatMap((routeOption) => | ||
pageFilePattern.map((pattern) => join(routeOption.src, pattern)) | ||
), | ||
...IDS_TO_INCLUDE, | ||
// importing the definePage block | ||
/\?.*\bdefinePage\&vue\b/, | ||
DEFINE_PAGE_QUERY_RE, | ||
], | ||
options.exclude | ||
) | ||
|
@@ -68,25 +73,36 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => { | |
name: 'unplugin-vue-router', | ||
enforce: 'pre', | ||
|
||
resolveId(id) { | ||
if ( | ||
// vue-router/auto-routes | ||
id === MODULE_ROUTES_PATH || | ||
// 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 | ||
id === MODULE_VUE_ROUTER_AUTO | ||
) { | ||
// virtual module | ||
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 | ||
} | ||
|
||
// nothing to do, just for TS | ||
return | ||
resolveId: { | ||
filter: { | ||
id: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't this just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see in the docs https://rolldown.rs/guide/plugin-development#plugin-hook-filters that they need to be regexp although it feels counter intuitive. I wonder if unplugin just aligned with this, the other way around, or simply something els. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if they aren't regexps they are treated as globs. And as you mentioned, rolldown doesn't allow that for |
||
include: [ | ||
exactRegex(MODULE_ROUTES_PATH), | ||
exactRegex(MODULE_VUE_ROUTER_AUTO), | ||
routeBlockQueryRE, | ||
], | ||
}, | ||
}, | ||
handler(id) { | ||
if ( | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// vue-router/auto-routes | ||
id === MODULE_ROUTES_PATH || | ||
// 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 | ||
id === MODULE_VUE_ROUTER_AUTO | ||
) { | ||
// virtual module | ||
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 | ||
} | ||
|
||
// nothing to do, just for TS | ||
return | ||
}, | ||
}, | ||
|
||
buildStart() { | ||
|
@@ -103,10 +119,17 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => { | |
return filterPageComponents(id) | ||
}, | ||
|
||
transform(code, id) { | ||
// console.log('👋 Transforming', id) | ||
// remove the `definePage()` from the file or isolate it | ||
return ctx.definePageTransform(code, id) | ||
transform: { | ||
filter: { | ||
id: { | ||
include: [...IDS_TO_INCLUDE, DEFINE_PAGE_QUERY_RE], | ||
}, | ||
}, | ||
handler(code, id) { | ||
// console.log('👋 Transforming', id) | ||
// remove the `definePage()` from the file or isolate it | ||
return ctx.definePageTransform(code, id) | ||
}, | ||
}, | ||
|
||
// loadInclude is necessary for webpack | ||
|
@@ -119,32 +142,45 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => { | |
) | ||
}, | ||
|
||
load(id) { | ||
// remove the <route> block as it's parsed by the plugin | ||
// stub it with an empty module | ||
if (id === ROUTE_BLOCK_ID) { | ||
return { | ||
code: `export default {}`, | ||
map: null, | ||
load: { | ||
filter: { | ||
id: { | ||
include: [ | ||
exactRegex(ROUTE_BLOCK_ID), | ||
exactRegex(MODULE_ROUTES_PATH), | ||
exactRegex(MODULE_VUE_ROUTER_AUTO), | ||
exactRegex(`${VIRTUAL_PREFIX}${MODULE_ROUTES_PATH}`), | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exactRegex(`${VIRTUAL_PREFIX}${MODULE_VUE_ROUTER_AUTO}`), | ||
], | ||
}, | ||
}, | ||
handler(id) { | ||
// remove the <route> block as it's parsed by the plugin | ||
// stub it with an empty module | ||
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) | ||
// 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) { | ||
ROUTES_LAST_LOAD_TIME.update() | ||
return ctx.generateRoutes() | ||
} | ||
// vue-router/auto-routes | ||
if (resolvedId === MODULE_ROUTES_PATH) { | ||
ROUTES_LAST_LOAD_TIME.update() | ||
return ctx.generateRoutes() | ||
} | ||
|
||
// vue-router/auto | ||
if (resolvedId === MODULE_VUE_ROUTER_AUTO) { | ||
return ctx.generateVueRouterProxy() | ||
} | ||
// vue-router/auto | ||
if (resolvedId === MODULE_VUE_ROUTER_AUTO) { | ||
return ctx.generateVueRouterProxy() | ||
} | ||
|
||
return // ok TS... | ||
return // ok TS... | ||
}, | ||
}, | ||
|
||
// improves DX | ||
|
Uh oh!
There was an error while loading. Please reload this page.