Warning
Work in progress!
A collection of utility functions for Nuxt module authors.
npm i nuxt-module-utilsWhile Nuxt provides the typescript.hoist option to generate aliases for nested dependencies within pnpm monorepos, it is processed before modules are set up.
The hoistDependencies utility allows you to hoist dependencies from within your module's setup function. It works by resolving the paths of the specified packages and adding them to Nuxt's TypeScript configuration, ensuring they are included in the generated tsconfig.json.
// src/module.ts
import { defineNuxtModule } from '@nuxt/kit'
import { hoistDependencies } from 'nuxt-module-utils'
export default defineNuxtModule({
async setup() {
await hoistDependencies(['my-lib', 'my-other-lib'])
}
})Get module options from a given Nuxt layer.
This takes into account both inline module options specified in the modules array and options specified in the layer's config under a specific key. It returns the merged options (keyed config taking precedence) if both are configured, or the first available option.
// src/module.ts
import { defineNuxtModule } from '@nuxt/kit'
import { getLayerModuleOptions } from 'nuxt-module-utils'
export interface ModuleOptions {
myOption?: string
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'my-module',
configKey: 'myModule', // key in nuxt.config
},
async setup(options, nuxt) {
for (const layer of nuxt.options._layers) {
const layerModuleOptions = getLayerModuleOptions(
layer,
'myModule', // key in nuxt.config
'my-module' // name in modules array
)
// ...
}
}
})