Skip to content

Commit

Permalink
fix: prevent route block transform by other plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed Mar 4, 2022
1 parent a9d47c1 commit 03cf0c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const MODULE_ID_VIRTUAL = '/@vite-plugin-pages/generated-pages'
export const MODULE_IDS = [
'~pages',
'~react-pages',
Expand All @@ -8,7 +7,10 @@ export const MODULE_IDS = [
'virtual:generated-pages-react',
]

export const routeBlockQueryRE = /\.vue\?vue&type=route/
export const MODULE_ID_VIRTUAL = '/@vite-plugin-pages/generated-pages'
export const ROUTE_BLOCK_ID_VIRTUAL = '/@vite-plugin-pages/route-block'

export const routeBlockQueryRE = /\?vue&type=route/

export const dynamicRouteRE = /^\[(.+)\]$/
export const cacheAllRouteRE = /^\[\.{3}(.*)\]$/
Expand Down
40 changes: 17 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MODULE_IDS, MODULE_ID_VIRTUAL, routeBlockQueryRE } from './constants'
import { MODULE_IDS, MODULE_ID_VIRTUAL, ROUTE_BLOCK_ID_VIRTUAL, routeBlockQueryRE } from './constants'
import { PageContext } from './context'

import type { UserOptions } from './types'
Expand Down Expand Up @@ -28,37 +28,31 @@ function pagesPlugin(userOptions: UserOptions = {}): Plugin {
ctx = new PageContext(userOptions, config.root)
ctx.setLogger(config.logger)
await ctx.searchGlob()

// hijack vite json plugin to avoid tranform custom block
const jsonPlugin = config.plugins.find(p => p.name === 'vite:json')
if (jsonPlugin) {
const jsonTransform = jsonPlugin.transform // backup @rollup/plugin-json
jsonPlugin.transform = async function(code: string, id: string) {
if (routeBlockQueryRE.test(id))
return

return jsonTransform!.apply(this, [code, id])
}
}
},
configureServer(server) {
ctx.setupViteServer(server)
},
resolveId(id) {
return MODULE_IDS.includes(id) ? MODULE_ID_VIRTUAL : null
if (MODULE_IDS.includes(id))
return MODULE_ID_VIRTUAL

if (routeBlockQueryRE.test(id))
return ROUTE_BLOCK_ID_VIRTUAL

return null
},
async load(id) {
if (id !== MODULE_ID_VIRTUAL)
return
if (id === MODULE_ID_VIRTUAL)
return ctx.resolveRoutes()

return ctx.resolveRoutes()
},
async transform(_code, id) {
if (!routeBlockQueryRE.test(id)) return
return {
code: 'export default {};',
map: null,
if (id === ROUTE_BLOCK_ID_VIRTUAL) {
return {
code: 'export default {};',
map: null,
}
}

return null
},
}
}
Expand Down

0 comments on commit 03cf0c0

Please sign in to comment.