diff --git a/src/core/context.ts b/src/core/context.ts index 2850ff40b..897d2a9da 100644 --- a/src/core/context.ts +++ b/src/core/context.ts @@ -3,7 +3,7 @@ import { TreeNode, PrefixTree } from './tree' import { promises as fs } from 'fs' import { asRoutePath, ImportsMap, logTree, throttle } from './utils' import { generateRouteNamedMap } from '../codegen/generateRouteMap' -import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER } from './moduleConstants' +import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER_AUTO } from './moduleConstants' import { generateRouteRecord } from '../codegen/generateRouteRecords' import fg from 'fast-glob' import { relative, resolve } from 'pathe' @@ -202,7 +202,7 @@ export function createRoutesContext(options: ResolvedOptions) { function generateDTS(): string { return _generateDTS({ - vueRouterModule: MODULE_VUE_ROUTER, + vueRouterModule: MODULE_VUE_ROUTER_AUTO, routesModule: MODULE_ROUTES_PATH, routeNamedMap: generateRouteNamedMap(routeTree), }) diff --git a/src/core/moduleConstants.ts b/src/core/moduleConstants.ts index f23d75bec..63305c863 100644 --- a/src/core/moduleConstants.ts +++ b/src/core/moduleConstants.ts @@ -1,6 +1,6 @@ -export const MODULE_VUE_ROUTER = 'vue-router/auto' +export const MODULE_VUE_ROUTER_AUTO = 'vue-router/auto' // vue-router/auto/routes was more natural but didn't work well with TS -export const MODULE_ROUTES_PATH = `${MODULE_VUE_ROUTER}-routes` +export const MODULE_ROUTES_PATH = `${MODULE_VUE_ROUTER_AUTO}-routes` // NOTE: not sure if needed. Used for HMR the virtual routes let time = Date.now() @@ -21,7 +21,7 @@ export const VIRTUAL_PREFIX = 'virtual:' // allows removing the route block from the code export const ROUTE_BLOCK_ID = `${VIRTUAL_PREFIX}/vue-router/auto/route-block` -export const MODULES_ID_LIST = [MODULE_VUE_ROUTER, MODULE_ROUTES_PATH] +export const MODULES_ID_LIST = [MODULE_VUE_ROUTER_AUTO, MODULE_ROUTES_PATH] export function getVirtualId(id: string) { return id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : null diff --git a/src/index.ts b/src/index.ts index 14bc6b863..2ba340b2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin' import { createRoutesContext } from './core/context' import { MODULE_ROUTES_PATH, - MODULE_VUE_ROUTER, + MODULE_VUE_ROUTER_AUTO, getVirtualId as _getVirtualId, asVirtualId as _asVirtualId, routeBlockQueryRE, @@ -64,21 +64,24 @@ export default createUnplugin((opt = {}, _meta) => { enforce: 'pre', resolveId(id) { - if (id === MODULE_ROUTES_PATH) { + 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) } - // 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 } - return undefined // ok TS... + + // nothing to do, just for TS + return }, buildStart() { @@ -106,12 +109,14 @@ export default createUnplugin((opt = {}, _meta) => { if (id === ROUTE_BLOCK_ID) return true const resolvedId = getVirtualId(id) return ( - resolvedId === MODULE_ROUTES_PATH || resolvedId === MODULE_VUE_ROUTER + resolvedId === MODULE_ROUTES_PATH || + resolvedId === MODULE_VUE_ROUTER_AUTO ) }, load(id) { // remove the block as it's parsed by the plugin + // stub it with an empty module if (id === ROUTE_BLOCK_ID) { return { code: `export default {}`, @@ -130,7 +135,7 @@ export default createUnplugin((opt = {}, _meta) => { } // vue-router/auto - if (resolvedId === MODULE_VUE_ROUTER) { + if (resolvedId === MODULE_VUE_ROUTER_AUTO) { return ctx.generateVueRouterProxy() }