Skip to content

Commit

Permalink
feat: add cross-plugin support for generated routes (hannoeru#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi authored Jun 17, 2022
1 parent dca799d commit 3b04697
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ function pagesPlugin(userOptions: UserOptions = {}): Plugin {
ctx.setLogger(config.logger)
await ctx.searchGlob()
},
api: {
getResolvedRoutes() {
return ctx.options.resolver.getComputedRoutes(ctx)
},
},
configureServer(server) {
ctx.setupViteServer(server)
},
Expand Down
10 changes: 9 additions & 1 deletion src/resolvers/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function prepareRoutes(
return routes
}

async function resolveReactRoutes(ctx: PageContext) {
async function computeReactRoutes(ctx: PageContext): Promise<ReactRoute[]> {
const { routeStyle, caseSensitive } = ctx.options
const nuxtStyle = routeStyle === 'nuxt'

Expand Down Expand Up @@ -107,6 +107,11 @@ async function resolveReactRoutes(ctx: PageContext) {

finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes

return finalRoutes
}

async function resolveReactRoutes(ctx: PageContext) {
const finalRoutes = await computeReactRoutes(ctx)
let client = generateClientCode(finalRoutes, ctx.options)
client = (await ctx.options.onClientGenerated?.(client)) || client
return client
Expand All @@ -123,6 +128,9 @@ export function ReactResolver(): PageResolver {
async resolveRoutes(ctx) {
return resolveReactRoutes(ctx)
},
async getComputedRoutes(ctx) {
return computeReactRoutes(ctx)
},
stringify: {
component: path => `React.createElement(${path})`,
dynamicImport: path => `React.lazy(() => import("${path}"))`,
Expand Down
10 changes: 9 additions & 1 deletion src/resolvers/solid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function prepareRoutes(
return routes
}

async function resolveSolidRoutes(ctx: PageContext) {
async function computeSolidRoutes(ctx: PageContext): Promise<SolidRoute[]> {
const { routeStyle, caseSensitive } = ctx.options
const nuxtStyle = routeStyle === 'nuxt'

Expand Down Expand Up @@ -108,6 +108,11 @@ async function resolveSolidRoutes(ctx: PageContext) {

finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes

return finalRoutes
}

async function resolveSolidRoutes(ctx: PageContext) {
const finalRoutes = await computeSolidRoutes(ctx)
let client = generateClientCode(finalRoutes, ctx.options)
client = (await ctx.options.onClientGenerated?.(client)) || client
return client
Expand All @@ -124,6 +129,9 @@ export function SolidResolver(): PageResolver {
async resolveRoutes(ctx) {
return resolveSolidRoutes(ctx)
},
async getComputedRoutes(ctx) {
return computeSolidRoutes(ctx)
},
stringify: {
dynamicImport: path => `Solid.lazy(() => import("${path}"))`,
final: code => `import * as Solid from "solid-js";\n${code}`,
Expand Down
11 changes: 10 additions & 1 deletion src/resolvers/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function prepareRoutes(
return routes
}

async function resolveVueRoutes(ctx: PageContext, customBlockMap: Map<string, CustomBlock>) {
async function computeVueRoutes(ctx: PageContext, customBlockMap: Map<string, CustomBlock>): Promise<VueRoute[]> {
const { routeStyle, caseSensitive } = ctx.options

const pageRoutes = [...ctx.pageRouteMap.values()]
Expand Down Expand Up @@ -136,6 +136,12 @@ async function resolveVueRoutes(ctx: PageContext, customBlockMap: Map<string, Cu

finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes

return finalRoutes
}

async function resolveVueRoutes(ctx: PageContext, customBlockMap: Map<string, CustomBlock>) {
const finalRoutes = await computeVueRoutes(ctx, customBlockMap)

let client = generateClientCode(finalRoutes, ctx.options)
client = (await ctx.options.onClientGenerated?.(client)) || client
return client
Expand Down Expand Up @@ -179,6 +185,9 @@ export function VueResolver(): PageResolver {
async resolveRoutes(ctx) {
return resolveVueRoutes(ctx, customBlockMap)
},
async getComputedRoutes(ctx) {
return computeVueRoutes(ctx, customBlockMap)
},
hmr: {
added: async(ctx, path) => checkCustomBlockChange(ctx, path),
changed: async(ctx, path) => checkCustomBlockChange(ctx, path),
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ReactRoute, SolidRoute, VueRoute } from './resolvers'
import type { PageContext } from './context'
import type { Awaitable } from '@antfu/utils'

Expand Down Expand Up @@ -28,6 +29,7 @@ export interface PageResolver {
resolveModuleIds: () => string[]
resolveExtensions: () => string[]
resolveRoutes: (ctx: PageContext) => Awaitable<string>
getComputedRoutes: (ctx: PageContext) => Awaitable<VueRoute[] | ReactRoute[] | SolidRoute[]>
stringify?: {
dynamicImport?: (importPath: string) => string
component?: (importName: string) => string
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/options.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports[`Options resolve > react 1`] = `
"onClientGenerated": undefined,
"onRoutesGenerated": undefined,
"resolver": {
"getComputedRoutes": [Function],
"resolveExtensions": [Function],
"resolveModuleIds": [Function],
"resolveRoutes": [Function],
Expand Down Expand Up @@ -66,6 +67,7 @@ exports[`Options resolve > solid 1`] = `
"onClientGenerated": undefined,
"onRoutesGenerated": undefined,
"resolver": {
"getComputedRoutes": [Function],
"resolveExtensions": [Function],
"resolveModuleIds": [Function],
"resolveRoutes": [Function],
Expand Down Expand Up @@ -104,6 +106,7 @@ exports[`Options resolve > vue - custom module id 1`] = `
"onClientGenerated": undefined,
"onRoutesGenerated": undefined,
"resolver": {
"getComputedRoutes": [Function],
"hmr": {
"added": [Function],
"changed": [Function],
Expand Down Expand Up @@ -145,6 +148,7 @@ exports[`Options resolve > vue 1`] = `
"onClientGenerated": undefined,
"onRoutesGenerated": undefined,
"resolver": {
"getComputedRoutes": [Function],
"hmr": {
"added": [Function],
"changed": [Function],
Expand Down

0 comments on commit 3b04697

Please sign in to comment.