nuxt-module-utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i nuxt-module-utils

Weekly Downloads

5

Version

0.0.5

License

MIT

Unpacked Size

10.4 kB

Total Files

5

Last publish

Collaborators

  • bobbiegoede