Skip to content

Commit

Permalink
refactor: internal rename
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 18, 2024
1 parent 99f60a8 commit a5d1546
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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),
})
Expand Down
6 changes: 3 additions & 3 deletions src/core/moduleConstants.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand Down
25 changes: 15 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -64,21 +64,24 @@ export default createUnplugin<Options | undefined>((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() {
Expand Down Expand Up @@ -106,12 +109,14 @@ export default createUnplugin<Options | undefined>((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 <route> block as it's parsed by the plugin
// stub it with an empty module
if (id === ROUTE_BLOCK_ID) {
return {
code: `export default {}`,
Expand All @@ -130,7 +135,7 @@ export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
}

// vue-router/auto
if (resolvedId === MODULE_VUE_ROUTER) {
if (resolvedId === MODULE_VUE_ROUTER_AUTO) {
return ctx.generateVueRouterProxy()
}

Expand Down

0 comments on commit a5d1546

Please sign in to comment.