Skip to content

Commit

Permalink
fix: remove router requirement from stonecrop
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan Bansal committed Dec 13, 2024
1 parent 5a13335 commit b47cb3b
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion nuxt/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default defineNuxtConfig({
modules: ['../src/module'],
stonecrop: {},
devtools: { enabled: true },
compatibilityDate: '2024-12-03',
compatibilityDate: '2024-12-13',
})
4 changes: 1 addition & 3 deletions nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export default defineNuxtModule({
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
addPlugin(resolve('./runtime/plugin'))

extendPages(pages => {
console.log('pages', pages)
})
extendPages(pages => {})
},
})
4 changes: 2 additions & 2 deletions nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRouter } from 'nuxt/app'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(_nuxtApp => {
const router = useRouter()
console.log('in plugin')
const app = _nuxtApp.vueApp
app.use(Stonecrop, { router })
app.use(Stonecrop)
})
2 changes: 2 additions & 0 deletions stonecrop/src/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function useStonecrop(registry?: Registry): StonecropReturn {
const isReady = ref(false)

onBeforeMount(async () => {
if (!registry.router) return

const route = registry.router.currentRoute.value
const doctypeSlug = route.params.records?.toString().toLowerCase()
const recordId = route.params.record?.toString().toLowerCase()
Expand Down
9 changes: 5 additions & 4 deletions stonecrop/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { App, type Plugin } from 'vue'

import Registry from '@/registry'
import router from '@/router'
import { pinia } from '@/stores'
import type { InstallOptions } from '@/types'

const plugin: Plugin = {
install: (app: App, options?: InstallOptions) => {
const appRouter = options?.router || router
const registry = new Registry(appRouter, options?.getMeta)
const registry = new Registry(options?.router, options?.getMeta)

if (options?.router) {
app.use(options.router)
}

app.use(appRouter)
app.use(pinia)
app.provide('$registry', registry)

Expand Down
8 changes: 4 additions & 4 deletions stonecrop/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import DoctypeMeta from '@/doctype'
export default class Registry {
static _root: Registry
name: string
router: Router
router?: Router
registry: Record<string, DoctypeMeta>
getMeta?: (doctype: string) => DoctypeMeta | Promise<DoctypeMeta>

constructor(router: Router, getMeta?: (doctype: string) => DoctypeMeta | Promise<DoctypeMeta>) {
constructor(router?: Router, getMeta?: (doctype: string) => DoctypeMeta | Promise<DoctypeMeta>) {
if (Registry._root) {
return Registry._root
}
Expand All @@ -24,8 +24,8 @@ export default class Registry {
if (!(doctype.doctype in Object.keys(this.registry))) {
this.registry[doctype.slug] = doctype
}
if (!this.router.hasRoute(doctype.doctype)) {
this.router.addRoute({
if (!this.router?.hasRoute(doctype.doctype)) {
this.router?.addRoute({
path: `/${doctype.slug}`,
name: doctype.slug,
component: doctype.component,
Expand Down
8 changes: 0 additions & 8 deletions stonecrop/src/router.ts

This file was deleted.

0 comments on commit b47cb3b

Please sign in to comment.