Skip to content

Commit 7e72198

Browse files
authored
feat: support writing RuntimeNuxtHooks types (#183)
1 parent 7846e2f commit 7e72198

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The entrypoint for module definition.
4242

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

45-
You could also optionally export `ModuleHooks` to annotate any custom hooks the module uses.
45+
You could also optionally export `ModuleHooks` or `RuntimeModuleHooks` to annotate any custom hooks the module uses.
4646

4747
```ts [src/module.ts]
4848
import { defineNuxtModule } from '@nuxt/kit'
@@ -55,6 +55,10 @@ export interface ModuleHooks {
5555
'my-module:init': any
5656
}
5757

58+
export interface RuntimeModuleHooks {
59+
'my-module:runtime-hook': any
60+
}
61+
5862
export interface ModulePublicRuntimeConfig {
5963
NAME: string
6064
}

example/src/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface ModuleHooks {
99
'my-module:init': any
1010
}
1111

12+
export interface RuntimeModuleHooks {
13+
'my-module:runtime-hook': any
14+
}
15+
1216
export interface ModulePublicRuntimeConfig {
1317
NAME: string
1418
}

src/commands/build.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async function writeTypes (distDir: string, meta: ModuleMeta) {
126126
)
127127
const isStub = moduleTypes.includes('export *')
128128

129+
const appShims = []
129130
const schemaShims = []
130131
const moduleImports = []
131132

@@ -140,6 +141,10 @@ async function writeTypes (distDir: string, meta: ModuleMeta) {
140141
moduleImports.push('ModuleHooks')
141142
schemaShims.push(' interface NuxtHooks extends ModuleHooks {}')
142143
}
144+
if (hasTypeExport('RuntimeModuleHooks')) {
145+
moduleImports.push('RuntimeModuleHooks')
146+
appShims.push(' interface RuntimeNuxtHooks extends RuntimeModuleHooks {}')
147+
}
143148
if (hasTypeExport('ModuleRuntimeConfig')) {
144149
moduleImports.push('ModuleRuntimeConfig')
145150
schemaShims.push(' interface RuntimeConfig extends ModuleRuntimeConfig {}')
@@ -152,6 +157,7 @@ async function writeTypes (distDir: string, meta: ModuleMeta) {
152157
const dtsContents = `
153158
import { ${moduleImports.join(', ')} } from './module'
154159
160+
${appShims.length ? `declare module '#app' {\n${appShims.join('\n')}\n}\n` : ''}
155161
${schemaShims.length ? `declare module '@nuxt/schema' {\n${schemaShims.join('\n')}\n}\n` : ''}
156162
${schemaShims.length ? `declare module 'nuxt/schema' {\n${schemaShims.join('\n')}\n}\n` : ''}
157163

test/build.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ describe('module builder', () => {
4545
const types = await readFile(join(distDir, 'types.d.ts'), 'utf-8')
4646
expect(types).toMatchInlineSnapshot(`
4747
"
48-
import { ModuleOptions, ModuleHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module'
48+
import { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module'
49+
50+
declare module '#app' {
51+
interface RuntimeNuxtHooks extends RuntimeModuleHooks {}
52+
}
4953
5054
declare module '@nuxt/schema' {
5155
interface NuxtConfig { ['myModule']?: Partial<ModuleOptions> }
@@ -64,7 +68,7 @@ describe('module builder', () => {
6468
}
6569
6670
67-
export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, ModuleRuntimeConfig, default } from './module'
71+
export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, ModuleRuntimeConfig, RuntimeModuleHooks, default } from './module'
6872
"
6973
`)
7074
})

0 commit comments

Comments
 (0)