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(kit): provide type support for module options in installModule #26744

Merged
merged 12 commits into from
Jun 10, 2024
7 changes: 5 additions & 2 deletions packages/kit/src/module/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, promises as fsp, lstatSync } from 'node:fs'
import type { ModuleMeta, Nuxt, NuxtModule } from '@nuxt/schema'
import type { ModuleMeta, Nuxt, NuxtConfig, NuxtModule } from '@nuxt/schema'
import { dirname, isAbsolute, join, resolve } from 'pathe'
import { defu } from 'defu'
import { isNuxt2 } from '../compatibility'
Expand All @@ -12,7 +12,10 @@ import { logger } from '../logger'
const NODE_MODULES_RE = /[/\\]node_modules[/\\]/

/** Installs a module on a Nuxt instance. */
export async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt: Nuxt = useNuxt()) {
export async function installModule<
T extends string | NuxtModule,
Config extends Extract<NonNullable<NuxtConfig['modules']>[number], [T, any]>,
> (moduleToInstall: T, inlineOptions?: [Config] extends [never] ? any : Config[1], nuxt: Nuxt = useNuxt()) {
const { nuxtModule, buildTimeModuleMeta } = await loadNuxtModuleInstance(moduleToInstall, nuxt)

const localLayerModuleDirs = new Set<string>()
Expand Down
11 changes: 10 additions & 1 deletion test/fixtures/basic-types/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addTypeTemplate } from 'nuxt/kit'
import { addTypeTemplate, installModule } from 'nuxt/kit'

export default defineNuxtConfig({
experimental: {
Expand Down Expand Up @@ -41,6 +41,15 @@ export default defineNuxtConfig({
filename: 'test.d.ts',
getContents: () => 'declare type Fromage = "cheese"',
})
function _test () {
installModule('~/modules/example', {
typeTest (val) {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error module type defines val as boolean
const b: string = val
return !!b
},
})
}
},
'./modules/test',
[
Expand Down