Skip to content

Commit

Permalink
feat: generate middleware types (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx committed Jun 21, 2023
1 parent 3e3cf68 commit 3c2e715
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion packages/bridge-schema/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import app from './app'
import build from './build'
import cli from './cli'
Expand Down
6 changes: 5 additions & 1 deletion packages/bridge/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useNuxt, addTemplate, resolveAlias, addWebpackPlugin, addVitePlugin, ad
import { NuxtModule } from '@nuxt/schema'
import { normalize, resolve } from 'pathe'
import { resolveImports } from 'mlly'
import { componentsTypeTemplate, schemaTemplate } from './type-templates'
import { componentsTypeTemplate, schemaTemplate, middlewareTypeTemplate } from './type-templates'
import { distDir } from './dirs'
import { VueCompat } from './vue-compat'

Expand Down Expand Up @@ -57,8 +57,12 @@ export async function setupAppBridge (_options: any) {
...componentsTypeTemplate,
options: { components, buildDir: nuxt.options.buildDir }
})

addTemplate(middlewareTypeTemplate)

nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'types/components.d.ts') })
references.push({ path: resolve(nuxt.options.buildDir, 'types/middleware.d.ts') })
})

// Augment schema with module types
Expand Down
1 change: 0 additions & 1 deletion packages/bridge/src/runtime/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import type { NuxtAppCompat } from '@nuxt/bridge-schema'
import { defineComponent, getCurrentInstance } from './composables'
export const isVue2 = true
Expand Down
25 changes: 21 additions & 4 deletions packages/bridge/src/type-templates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { isAbsolute, relative, join } from 'pathe'
import type { Component, Nuxt, NuxtApp } from '@nuxt/schema'
import { genDynamicImport, genString } from 'knitwork'
Expand All @@ -10,6 +9,11 @@ type ComponentsTemplateOptions = {
components: Component[]
}

interface TemplateContext {
nuxt: Nuxt
app: NuxtApp & { templateVars: Record<string, any> }
}

export const componentsTypeTemplate = {
filename: 'types/components.d.ts',
getContents: ({ options }: { options: ComponentsTemplateOptions }) => {
Expand All @@ -28,9 +32,22 @@ export const componentNames: string[]
}
}

interface TemplateContext {
nuxt: Nuxt
app: NuxtApp
export const middlewareTypeTemplate = {
filename: 'types/middleware.d.ts',
getContents: ({ app }: TemplateContext) => {
const middleware = app.templateVars.middleware

return [
'import type { NuxtAppCompat } from \'@nuxt/bridge-schema\'',
`export type MiddlewareKey = ${middleware.map(mw => genString(mw.name)).join(' | ') || 'string'}`,
'declare module \'vue/types/options\' {',
' export type Middleware = MiddlewareKey | ((ctx: NuxtAppCompat, cb: Function) => Promise<void> | void)',
' interface ComponentOptions<V extends Vue> {',
' middleware?: Middleware | Middleware[]',
' }',
'}'
].join('\n')
}
}

const adHocModules = ['router', 'pages', 'auto-imports', 'meta', 'components']
Expand Down

0 comments on commit 3c2e715

Please sign in to comment.