Skip to content

Commit 7bf2f33

Browse files
committed
feat: cross platform page system
1 parent 18a4647 commit 7bf2f33

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

apps/app-frontend/src/routes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Pages from '@/pages'
44
import * as Instance from '@/pages/instance'
55
import * as Library from '@/pages/library'
66
import * as Project from '@/pages/project'
7+
import { sharedRoutes } from '@modrinth/ui/pages'
78

89
/**
910
* Configures application routing. Add page to pages/index and then add to route table here.
@@ -170,6 +171,8 @@ export default new createRouter({
170171
},
171172
],
172173
},
174+
// Shared routes from @modrinth/ui
175+
...sharedRoutes,
173176
],
174177
linkActiveClass: 'router-link-active',
175178
linkExactActiveClass: 'router-link-exact-active',

apps/frontend/nuxt.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import Papa from 'papaparse'
1111
import { basename, relative, resolve } from 'pathe'
1212
import svgLoader from 'vite-svg-loader'
1313

14+
import {
15+
ServersOverviewPage,
16+
createComponentResolver,
17+
sharedRoutes,
18+
toNuxtPages,
19+
} from '@modrinth/ui/pages'
1420
import type { GeneratedState } from './src/composables/generated'
1521

1622
const STAGING_API_URL = 'https://staging-api.modrinth.com/v2/'
@@ -303,6 +309,17 @@ export default defineNuxtConfig({
303309
children: [],
304310
}),
305311
)
312+
313+
// Shared routes
314+
const uiPackagePath = resolve(__dirname, '../../packages/ui')
315+
const componentResolver = createComponentResolver(uiPackagePath)
316+
317+
componentResolver.register(ServersOverviewPage, 'src/pages/servers/manage.vue')
318+
319+
const nuxtPages = toNuxtPages(sharedRoutes, (component) =>
320+
componentResolver.resolve(component),
321+
)
322+
routes.push(...nuxtPages)
306323
},
307324
async 'vintl:extendOptions'(opts) {
308325
opts.locales ??= []

packages/ui/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
"private": true,
55
"main": "./index.ts",
66
"types": "./index.ts",
7+
"exports": {
8+
".": {
9+
"types": "./index.ts",
10+
"default": "./index.ts"
11+
},
12+
"./pages": {
13+
"types": "./src/pages/index.ts",
14+
"default": "./src/pages/index.ts"
15+
},
16+
"./src/*": "./src/*"
17+
},
718
"scripts": {
819
"lint": "eslint . && prettier --check .",
920
"fix": "eslint . --fix && prettier --write .",

packages/ui/src/pages/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { RouteRecordRaw } from 'vue-router'
2+
3+
export const ServersOverviewPage = () => import('./servers/manage.vue')
4+
export { createComponentResolver, toNuxtPages } from './route-helpers'
5+
6+
export const sharedRoutes: RouteRecordRaw[] = [
7+
{
8+
path: '/servers/manage',
9+
name: 'Servers - Modrinth',
10+
component: ServersOverviewPage,
11+
meta: {
12+
breadcrumb: [{ name: 'Servers' }],
13+
},
14+
},
15+
]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { resolve } from 'pathe'
2+
import type { RouteRecordRaw } from 'vue-router'
3+
4+
interface NuxtPage {
5+
name?: string
6+
path: string
7+
file: string
8+
children?: NuxtPage[]
9+
meta?: Record<string, any>
10+
props?: boolean | Record<string, any>
11+
}
12+
13+
export function toNuxtPages(
14+
routes: RouteRecordRaw[],
15+
componentToFilePath: (component: any) => string,
16+
): NuxtPage[] {
17+
return routes.map((route) => {
18+
const nuxtPage: NuxtPage = {
19+
name: route.name as string,
20+
path: route.path,
21+
file: componentToFilePath(route.component),
22+
meta: route.meta,
23+
props: route.props,
24+
}
25+
26+
if (route.children) {
27+
nuxtPage.children = toNuxtPages(route.children, componentToFilePath)
28+
}
29+
30+
return nuxtPage
31+
})
32+
}
33+
34+
export function createComponentResolver(baseDir: string) {
35+
const componentMap = new Map<any, string>()
36+
37+
return {
38+
register(component: any, relativePath: string) {
39+
componentMap.set(component, resolve(baseDir, relativePath))
40+
},
41+
resolve(component: any): string {
42+
const path = componentMap.get(component)
43+
if (!path) {
44+
throw new Error(`Component not registered: ${component}`)
45+
}
46+
return path
47+
},
48+
}
49+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template>Hello world!</template>

0 commit comments

Comments
 (0)