Skip to content

BobbieGoede/nuxt-module-utils

Repository files navigation

Nuxt Module Utils

Warning

Work in progress!

A collection of utility functions for Nuxt module authors.

Installation

npm i nuxt-module-utils

Usage

getLayerModuleOptions<T>(layer: NuxtConfigLayer, configKey: string, name: string)

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
      )
      // ...
    }
  }
})

hoistDependencies(hoist: string[])

Warning

Deprecated and will be removed in v1 - Nuxt 4.0.0 supports modules adding to typescript.hoist

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'])
  }
})

Sponsors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published