Skip to content

Commit

Permalink
feat: support ModuleHooks type export
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 17, 2022
1 parent 9cd8282 commit 269ce20
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -24,13 +24,19 @@ This is the entrypoint for module definition.

A default export using `defineNuxtModule` and `ModuleOptions` type export is expected.

You could also optionally export `ModuleHooks` to annotate any custom hooks module uses.

```js [src/module.ts]
import { defineNuxtModule } from '@nuxt/kit'

export interface ModuleOptions {
apiKey: string
}

export interface ModuleHooks {
'my-module:init': any
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'my-module',
Expand Down
4 changes: 4 additions & 0 deletions example/src/module.ts
Expand Up @@ -4,6 +4,10 @@ export interface ModuleOptions {
apiKey: string
}

export interface ModuleHooks {
'my-module:init': any
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'my-module',
Expand Down
5 changes: 5 additions & 0 deletions src/build.ts
Expand Up @@ -83,6 +83,7 @@ async function writeTypes (distDir: string, meta: ModuleMeta) {
const isStub = moduleTypes.includes('export *')

const hasModuleOptions = isStub || typeExports.find(exp => exp.names.includes('ModuleOptions'))
const hasModuleHooks = isStub || typeExports.find(exp => exp.names.includes('ModuleHooks'))

const schemaShims = []
const moduleImports = []
Expand All @@ -92,6 +93,10 @@ async function writeTypes (distDir: string, meta: ModuleMeta) {
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`)
schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`)
}
if (hasModuleHooks) {
moduleImports.push('ModuleHooks')
schemaShims.push(' interface NuxtHooks extends ModuleHooks {}')
}

const dtsContents = `
import { ${moduleImports.join(', ')} } from './module'
Expand Down

0 comments on commit 269ce20

Please sign in to comment.