Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate middleware types #796

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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