@nuxt/docs-nightly 4.2.0-29345108.b399de2f → 4.2.0-29345484.1f69d1e1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -501,35 +501,51 @@ export default defineNuxtModule({
|
|
|
501
501
|
|
|
502
502
|
#### Using Other Modules in Your Module
|
|
503
503
|
|
|
504
|
-
If your module depends on other modules, you can
|
|
504
|
+
If your module depends on other modules, you can specify them using the `moduleDependencies` option. This provides a more robust way to handle module dependencies with version constraints and configuration merging:
|
|
505
505
|
|
|
506
506
|
```ts
|
|
507
|
-
import { createResolver, defineNuxtModule
|
|
508
|
-
|
|
509
|
-
export default defineNuxtModule<ModuleOptions>({
|
|
510
|
-
async setup (options, nuxt) {
|
|
511
|
-
const resolver = createResolver(import.meta.url)
|
|
507
|
+
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
512
508
|
|
|
513
|
-
|
|
514
|
-
nuxt.options.css.push(resolver.resolve('./runtime/assets/styles.css'))
|
|
509
|
+
const resolver = createResolver(import.meta.url)
|
|
515
510
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
511
|
+
export default defineNuxtModule<ModuleOptions>({
|
|
512
|
+
meta: {
|
|
513
|
+
name: 'my-module',
|
|
514
|
+
},
|
|
515
|
+
moduleDependencies: {
|
|
516
|
+
'@nuxtjs/tailwindcss': {
|
|
517
|
+
// You can specify a version constraint for the module
|
|
518
|
+
version: '>=6',
|
|
519
|
+
// Any configuration that should override `nuxt.options`
|
|
520
|
+
overrides: {
|
|
521
|
+
exposeConfig: true,
|
|
522
|
+
},
|
|
523
|
+
// Any configuration that should be set. It will override module defaults but
|
|
524
|
+
// will not override any configuration set in `nuxt.options`
|
|
525
|
+
defaults: {
|
|
526
|
+
config: {
|
|
527
|
+
darkMode: 'class',
|
|
528
|
+
content: {
|
|
529
|
+
files: [
|
|
530
|
+
resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
|
|
531
|
+
resolver.resolve('./runtime/*.{mjs,js,ts}'),
|
|
532
|
+
],
|
|
533
|
+
},
|
|
526
534
|
},
|
|
527
535
|
},
|
|
528
|
-
}
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
setup (options, nuxt) {
|
|
539
|
+
// We can inject our CSS file which includes Tailwind's directives
|
|
540
|
+
nuxt.options.css.push(resolver.resolve('./runtime/assets/styles.css'))
|
|
529
541
|
},
|
|
530
542
|
})
|
|
531
543
|
```
|
|
532
544
|
|
|
545
|
+
::callout{type="info"}
|
|
546
|
+
The `moduleDependencies` option replaces the deprecated `installModule` function and ensures proper setup order and configuration merging.
|
|
547
|
+
::
|
|
548
|
+
|
|
533
549
|
#### Using Hooks
|
|
534
550
|
|
|
535
551
|
[Lifecycle hooks](/docs/4.x/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
|
package/3.api/5.kit/1.modules.md
CHANGED
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
Modules are the building blocks of Nuxt. Kit provides a set of utilities to help you create and use modules. You can use these utilities to create your own modules or to reuse existing modules. For example, you can use the `defineNuxtModule` function to define a module and the `
|
|
11
|
+
Modules are the building blocks of Nuxt. Kit provides a set of utilities to help you create and use modules. You can use these utilities to create your own modules or to reuse existing modules. For example, you can use the `defineNuxtModule` function to define a module and specify dependencies using the `moduleDependencies` option.
|
|
12
12
|
|
|
13
13
|
## `defineNuxtModule`
|
|
14
14
|
|
|
@@ -62,6 +62,7 @@ export function defineNuxtModule<TOptions extends ModuleOptions> (): {
|
|
|
62
62
|
| `defaults` | `T \| ((nuxt: Nuxt) => T)`{lang="ts"} | `false` | Default options for the module. If a function is provided, it will be called with the Nuxt instance as the first argument. |
|
|
63
63
|
| `schema` | `T` | `false` | Schema for the module options. If provided, options will be applied to the schema. |
|
|
64
64
|
| `hooks` | `Partial<NuxtHooks>`{lang="ts"} | `false` | Hooks to be installed for the module. If provided, the module will install the hooks. |
|
|
65
|
+
| `moduleDependencies` | `Record<string, ModuleDependency> \| ((nuxt: Nuxt) => Record<string, ModuleDependency>)`{lang="ts"} | `false` | Dependencies on other modules with version constraints and configuration. Can be an object or a function that receives the Nuxt instance. See [example](#specifying-module-dependencies). |
|
|
65
66
|
| `onInstall` | `(nuxt: Nuxt) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is first installed. Requires `meta.name` and `meta.version` to be defined. |
|
|
66
67
|
| `onUpgrade` | `(options: T, nuxt: Nuxt, previousVersion: string) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is upgraded to a newer version. Requires `meta.name` and `meta.version` to be defined. |
|
|
67
68
|
| `setup` | `(this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void \| false \| ModuleSetupInstallResult>`{lang="ts"} | `false` | Setup function for the module. If provided, the module will call the setup function. |
|
|
@@ -239,8 +240,93 @@ export default defineNuxtModule({
|
|
|
239
240
|
})
|
|
240
241
|
```
|
|
241
242
|
|
|
243
|
+
#### Specifying Module Dependencies
|
|
244
|
+
|
|
245
|
+
You can use the `moduleDependencies` option to declare dependencies on other modules. This provides a robust way to ensure proper setup order, version compatibility, and configuration management.
|
|
246
|
+
|
|
247
|
+
The `moduleDependencies` option can be either an object or a function that receives the Nuxt instance:
|
|
248
|
+
|
|
249
|
+
##### Example
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
253
|
+
|
|
254
|
+
export default defineNuxtModule({
|
|
255
|
+
meta: {
|
|
256
|
+
name: 'my-module',
|
|
257
|
+
},
|
|
258
|
+
moduleDependencies: {
|
|
259
|
+
'@nuxtjs/tailwindcss': {
|
|
260
|
+
// Specify a version constraint (semver format)
|
|
261
|
+
version: '>=6.0.0',
|
|
262
|
+
// Configuration that overrides user settings
|
|
263
|
+
overrides: {
|
|
264
|
+
exposeConfig: true,
|
|
265
|
+
},
|
|
266
|
+
// Configuration that sets defaults but respects user settings
|
|
267
|
+
defaults: {
|
|
268
|
+
config: {
|
|
269
|
+
darkMode: 'class',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
'@nuxtjs/fontaine': {
|
|
274
|
+
// Optional dependencies won't be installed but ensure that options
|
|
275
|
+
// can be set if they _are_ installed
|
|
276
|
+
optional: true,
|
|
277
|
+
defaults: {
|
|
278
|
+
fonts: [
|
|
279
|
+
{
|
|
280
|
+
family: 'Roboto',
|
|
281
|
+
fallbacks: ['Impact'],
|
|
282
|
+
},
|
|
283
|
+
],
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
setup (options, nuxt) {
|
|
288
|
+
|
|
289
|
+
},
|
|
290
|
+
})
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
You can also use a function to dynamically determine dependencies based on the Nuxt configuration:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
297
|
+
|
|
298
|
+
export default defineNuxtModule({
|
|
299
|
+
meta: {
|
|
300
|
+
name: 'my-module',
|
|
301
|
+
},
|
|
302
|
+
moduleDependencies (nuxt) {
|
|
303
|
+
const dependencies: Record<string, any> = {
|
|
304
|
+
'@nuxtjs/tailwindcss': {
|
|
305
|
+
version: '>=6.0.0',
|
|
306
|
+
},
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Conditionally add dependencies based on Nuxt config
|
|
310
|
+
if (nuxt.options.experimental?.someFeature) {
|
|
311
|
+
dependencies['@nuxtjs/fontaine'] = {
|
|
312
|
+
optional: true,
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return dependencies
|
|
317
|
+
},
|
|
318
|
+
setup (options, nuxt) {
|
|
319
|
+
// Your setup logic runs after all dependencies are initialized
|
|
320
|
+
},
|
|
321
|
+
})
|
|
322
|
+
```
|
|
323
|
+
|
|
242
324
|
## `installModule`
|
|
243
325
|
|
|
326
|
+
::callout{type="warning"}
|
|
327
|
+
**Deprecated:** Use the [`moduleDependencies`](#specifying-module-dependencies) option in `defineNuxtModule` instead. The `installModule` function will be removed (or may become non-blocking) in a future version.
|
|
328
|
+
::
|
|
329
|
+
|
|
244
330
|
Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
|
|
245
331
|
|
|
246
332
|
### Usage
|