Skip to content

Commit b16d5a5

Browse files
feat: auto generate module options from schema meta (#33)
1 parent 0946c04 commit b16d5a5

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"pathe": "^1.1.2",
4141
"pkg-types": "^1.1.1",
4242
"tsconfck": "^3.0.3",
43-
"unbuild": "^2.0.0"
43+
"unbuild": "^2.0.0",
44+
"untyped": "^1.4.2"
4445
},
4546
"peerDependencies": {
4647
"@nuxt/kit": "^3.11.2",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/build.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { anyOf, createRegExp } from 'magic-regexp'
1010
import { consola } from 'consola'
1111
import type { ModuleMeta, NuxtModule } from '@nuxt/schema'
1212
import { findExports, resolvePath } from 'mlly'
13+
import { resolveSchema, generateTypes } from 'untyped'
14+
import type { InputObject } from 'untyped'
1315
import { defineCommand } from 'citty'
16+
import { loadNuxt } from '@nuxt/kit'
1417

1518
import { name, version } from '../../package.json'
1619

@@ -169,14 +172,24 @@ export default defineCommand({
169172
await fsp.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), 'utf8')
170173

171174
// Generate types
172-
await writeTypes(ctx.options.outDir, moduleMeta)
175+
await writeTypes(ctx.options.outDir, moduleMeta, async () => {
176+
const nuxt = await loadNuxt({
177+
cwd,
178+
overrides: {
179+
modules: [resolve(cwd, './src/module')],
180+
},
181+
})
182+
const moduleOptions = await moduleFn.getOptions?.({}, nuxt) || {}
183+
await nuxt.close()
184+
return moduleOptions
185+
})
173186
},
174187
},
175188
})
176189
},
177190
})
178191

179-
async function writeTypes(distDir: string, meta: ModuleMeta) {
192+
async function writeTypes(distDir: string, meta: ModuleMeta, getOptions: () => Promise<Record<string, unknown>>) {
180193
const dtsFile = resolve(distDir, 'types.d.ts')
181194
const dtsFileMts = resolve(distDir, 'types.d.mts')
182195
if (existsSync(dtsFile)) {
@@ -206,6 +219,9 @@ async function writeTypes(distDir: string, meta: ModuleMeta) {
206219
schemaShims.push(` interface NuxtConfig { ['${meta.configKey}']?: Partial<ModuleOptions> }`)
207220
schemaShims.push(` interface NuxtOptions { ['${meta.configKey}']?: ModuleOptions }`)
208221
}
222+
else if (meta.configKey) {
223+
schemaShims.push(` ${generateTypes(await resolveSchema(await getOptions() as InputObject), { interfaceName: 'ModuleOptions' })}`)
224+
}
209225
if (hasTypeExport('ModuleHooks')) {
210226
moduleImports.push('ModuleHooks')
211227
schemaShims.push(' interface NuxtHooks extends ModuleHooks {}')

0 commit comments

Comments
 (0)