Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ export default defineNuxtModule({
})
```

### `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.

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

## Sponsors

<p align="center">
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { hoistDependencies } from "./hoist";
export { getLayerModuleOptions } from "./layers";
47 changes: 47 additions & 0 deletions src/layers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { NuxtConfigLayer } from "@nuxt/schema";
import { defu } from "defu";

type GetModuleOptions<
T,
K extends keyof NuxtConfigLayer["config"],
> = [T] extends [never] ? NuxtConfigLayer["config"][K] : T;

/**
* 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.
*
* Returns the merged options (keyed config taking precedence) if both are configured, or the first available option.
*
* @param layer - a {@link NuxtConfigLayer | Nuxt Layer} from `nuxt.options._layers`
* @param configKey - the key used to configure module options in nuxt.config
* @param name - the module name as it would be specified in the `modules` array
* @returns
*/
export function getLayerModuleOptions<
T = never,
K extends keyof NuxtConfigLayer["config"] = keyof NuxtConfigLayer["config"],
>(
layer: NuxtConfigLayer,
configKey: K,
name: string,
): GetModuleOptions<T, K> | undefined {
type Options = GetModuleOptions<T, K>;

const matchInlineOptions = (mod: any): mod is [string, Options] =>
Array.isArray(mod) && typeof mod[0] === "string" && mod[0] === name;

const modules = (layer.config.modules || []) as [
string,
unknown | undefined,
][];
const inlineOptions = modules.find(matchInlineOptions)?.[1];
const keyOptions = layer.config[configKey] as Options | undefined;

if (inlineOptions == null && keyOptions == null) {
return undefined;
}

return defu(keyOptions || {}, inlineOptions || {}) as Options;
}