Skip to content

Commit d026f8f

Browse files
committed
feat: add getLayerModuleOptions
1 parent 36f5d88 commit d026f8f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ export default defineNuxtModule({
3434
})
3535
```
3636

37+
### `getLayerModuleOptions<T>(layer: NuxtConfigLayer, configKey: string, name: string)`
38+
39+
Get module options from a given Nuxt layer.
40+
41+
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 if both are configured, or the first available option.
42+
43+
```ts
44+
// src/module.ts
45+
import { defineNuxtModule } from '@nuxt/kit'
46+
import { getLayerModuleOptions } from 'nuxt-module-utils'
47+
48+
export interface ModuleOptions {
49+
myOption?: string
50+
}
51+
52+
export default defineNuxtModule<ModuleOptions>({
53+
meta: {
54+
name: 'my-module',
55+
configKey: 'myModule', // key in nuxt.config
56+
},
57+
async setup(options, nuxt) {
58+
for (const layer of nuxt.options._layers) {
59+
const layerModuleOptions = getLayerModuleOptions(
60+
layer,
61+
'myModule', // key in nuxt.config
62+
'my-module' // name in modules array
63+
)
64+
// ...
65+
}
66+
}
67+
})
68+
```
69+
3770
## Sponsors
3871

3972
<p align="center">

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { hoistDependencies } from "./hoist";
2+
export { getLayerModuleOptions } from "./layers";

src/layers.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { NuxtConfigLayer } from "@nuxt/schema";
2+
import { defu } from "defu";
3+
4+
type GetModuleOptions<
5+
T,
6+
K extends keyof NuxtConfigLayer["config"],
7+
> = [T] extends [never] ? NuxtConfigLayer["config"][K] : T;
8+
9+
/**
10+
* Get module options from a given Nuxt layer
11+
*
12+
* This takes into account both inline module options specified in the `modules` array
13+
* and options specified in the layer's config under a specific key.
14+
*
15+
* Returns the merged options if both are provided, or the first available option.
16+
*
17+
* @param layer - a {@link NuxtConfigLayer | Nuxt Layer} from `nuxt.options._layers`
18+
* @param configKey - the key used to configure module options in nuxt.config
19+
* @param name - the module name as it would be specified in the `modules` array
20+
* @returns
21+
*/
22+
export function getLayerModuleOptions<
23+
T = never,
24+
K extends keyof NuxtConfigLayer["config"] = keyof NuxtConfigLayer["config"],
25+
>(
26+
layer: NuxtConfigLayer,
27+
configKey: K,
28+
name: string,
29+
): GetModuleOptions<T, K> | undefined {
30+
type Options = GetModuleOptions<T, K>;
31+
32+
const matchInlineOptions = (mod: any): mod is [string, Options] =>
33+
Array.isArray(mod) && typeof mod[0] === "string" && mod[0] === name;
34+
35+
const modules = (layer.config.modules || []) as [
36+
string,
37+
unknown | undefined,
38+
][];
39+
const inlineOptions = modules.find(matchInlineOptions)?.[1];
40+
const keyOptions = layer.config[configKey] as Options | undefined;
41+
42+
if (inlineOptions == null && keyOptions == null) {
43+
return undefined;
44+
}
45+
46+
return defu(keyOptions || {}, inlineOptions || {}) as Options;
47+
}

0 commit comments

Comments
 (0)