@nuxt/docs 3.19.3 → 3.20.0
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.
- package/1.getting-started/02.installation.md +1 -1
- package/1.getting-started/18.upgrade.md +8 -4
- package/2.guide/2.concepts/7.esm.md +5 -1
- package/2.guide/3.going-further/1.experimental-features.md +88 -0
- package/2.guide/3.going-further/3.modules.md +35 -19
- package/2.guide/3.going-further/7.layers.md +14 -6
- package/2.guide/4.recipes/2.vite-plugin.md +41 -0
- package/3.api/1.components/13.nuxt-time.md +5 -1
- package/3.api/2.composables/use-async-data.md +6 -2
- package/3.api/2.composables/use-fetch.md +4 -0
- package/3.api/5.kit/1.modules.md +87 -1
- package/3.api/5.kit/11.nitro.md +4 -0
- package/3.api/5.kit/14.builder.md +60 -4
- package/3.api/5.kit/15.examples.md +3 -5
- package/3.api/5.kit/5.components.md +1 -0
- package/3.api/5.kit/9.head.md +132 -0
- package/3.api/6.advanced/1.hooks.md +2 -2
- package/package.json +1 -1
|
@@ -28,7 +28,7 @@ Or follow the steps below to set up a new Nuxt project on your computer.
|
|
|
28
28
|
::note
|
|
29
29
|
::details
|
|
30
30
|
:summary[Additional notes for an optimal setup:]
|
|
31
|
-
- **Node.js**: Make sure to use an even numbered version (
|
|
31
|
+
- **Node.js**: Make sure to use an even numbered version (20, 22, etc.)
|
|
32
32
|
- **Nuxtr**: Install the community-developed [Nuxtr extension](https://marketplace.visualstudio.com/items?itemName=Nuxtr.nuxtr-vscode)
|
|
33
33
|
- **WSL**: If you are using Windows and experience slow HMR, you may want to try using [WSL (Windows Subsystem for Linux)](https://docs.microsoft.com/en-us/windows/wsl/install) which may solve some performance issues.
|
|
34
34
|
- **Windows slow DNS resolution** - instead of using `localhost:3000` for local dev server on Windows, use `127.0.0.1` for much faster loading experience on browsers.
|
|
@@ -120,19 +120,23 @@ You can run all the codemods mentioned in this guide using the following `codemo
|
|
|
120
120
|
::code-group
|
|
121
121
|
|
|
122
122
|
```bash [npm]
|
|
123
|
-
|
|
123
|
+
# Using pinned version due to https://github.com/codemod-com/codemod/issues/1710
|
|
124
|
+
npx codemod@0.18.7 nuxt/4/migration-recipe
|
|
124
125
|
```
|
|
125
126
|
|
|
126
127
|
```bash [yarn]
|
|
127
|
-
|
|
128
|
+
# Using pinned version due to https://github.com/codemod-com/codemod/issues/1710
|
|
129
|
+
yarn dlx codemod@0.18.7 nuxt/4/migration-recipe
|
|
128
130
|
```
|
|
129
131
|
|
|
130
132
|
```bash [pnpm]
|
|
131
|
-
|
|
133
|
+
# Using pinned version due to https://github.com/codemod-com/codemod/issues/1710
|
|
134
|
+
pnpm dlx codemod@0.18.7 nuxt/4/migration-recipe
|
|
132
135
|
```
|
|
133
136
|
|
|
134
137
|
```bash [bun]
|
|
135
|
-
|
|
138
|
+
# Using pinned version due to https://github.com/codemod-com/codemod/issues/1710
|
|
139
|
+
bun x codemod@0.18.7 nuxt/4/migration-recipe
|
|
136
140
|
```
|
|
137
141
|
|
|
138
142
|
::
|
|
@@ -50,6 +50,10 @@ When adding modules to your package, things were a little different. A sample li
|
|
|
50
50
|
|
|
51
51
|
So in Nuxt 2, the bundler (webpack) would pull in the CJS file ('main') for the server build and use the ESM file ('module') for the client build.
|
|
52
52
|
|
|
53
|
+
::note
|
|
54
|
+
The `module` field is a convention used by bundlers like webpack and Rollup, but is not recognized by Node.js itself. Node.js only uses the [`exports`](https://nodejs.org/api/packages.html#exports) and [`main`](https://nodejs.org/api/packages.html#main) fields for module resolution.
|
|
55
|
+
::
|
|
56
|
+
|
|
53
57
|
However, in recent Node.js LTS releases, it is now possible to [use native ESM module](https://nodejs.org/api/esm.html) within Node.js. That means that Node.js itself can process JavaScript using ESM syntax, although it doesn't do it by default. The two most common ways to enable ESM syntax are:
|
|
54
58
|
|
|
55
59
|
- set `"type": "module"` within your `package.json` and keep using `.js` extension
|
|
@@ -59,7 +63,7 @@ This is what we do for Nuxt Nitro; we output a `.output/server/index.mjs` file.
|
|
|
59
63
|
|
|
60
64
|
### What Are Valid Imports in a Node.js Context?
|
|
61
65
|
|
|
62
|
-
When you `import` a module rather than `require` it, Node.js resolves it differently. For example, when you import `sample-library`, Node.js will look
|
|
66
|
+
When you `import` a module rather than `require` it, Node.js resolves it differently. For example, when you import `sample-library`, Node.js will look for the `exports` entry in that library's `package.json`, or fall back to the `main` entry if `exports` is not defined.
|
|
63
67
|
|
|
64
68
|
This is also true of dynamic imports, like `const b = await import('sample-library')`.
|
|
65
69
|
|
|
@@ -71,6 +71,44 @@ export default defineNuxtConfig({
|
|
|
71
71
|
})
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
+
## extractAsyncDataHandlers
|
|
75
|
+
|
|
76
|
+
Extracts handler functions from `useAsyncData` and `useLazyAsyncData` calls into separate chunks for improved code splitting and caching efficiency.
|
|
77
|
+
|
|
78
|
+
```ts twoslash [nuxt.config.ts]
|
|
79
|
+
export default defineNuxtConfig({
|
|
80
|
+
experimental: {
|
|
81
|
+
extractAsyncDataHandlers: true,
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
This feature transforms inline handler functions into dynamically imported chunks:
|
|
87
|
+
|
|
88
|
+
```vue
|
|
89
|
+
<!-- Before -->
|
|
90
|
+
<script setup>
|
|
91
|
+
const { data } = await useAsyncData('user', async () => {
|
|
92
|
+
return await $fetch('/api/user')
|
|
93
|
+
})
|
|
94
|
+
</script>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```vue
|
|
98
|
+
<!-- After transformation -->
|
|
99
|
+
<script setup>
|
|
100
|
+
const { data } = await useAsyncData('user', () =>
|
|
101
|
+
import('/generated-chunk.js').then(r => r.default()),
|
|
102
|
+
)
|
|
103
|
+
</script>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The benefit of this transformation is that we can split out data fetching logic — while still allowing the code to be loaded if required.
|
|
107
|
+
|
|
108
|
+
::important
|
|
109
|
+
This feature is only recommended for **static builds** with payload extraction, and where data does not need to be re-fetched at runtime.
|
|
110
|
+
::
|
|
111
|
+
|
|
74
112
|
## emitRouteChunkError
|
|
75
113
|
|
|
76
114
|
Emits `app:chunkError` hook when there is an error loading vite/webpack chunks. Default behavior is to perform a reload of the new route on navigation to a new route when a chunk fails to load.
|
|
@@ -721,3 +759,53 @@ export default defineNuxtConfig({
|
|
|
721
759
|
},
|
|
722
760
|
})
|
|
723
761
|
```
|
|
762
|
+
|
|
763
|
+
## typescriptPlugin
|
|
764
|
+
|
|
765
|
+
Enable enhanced TypeScript developer experience with the `@dxup/nuxt` module.
|
|
766
|
+
|
|
767
|
+
This experimental plugin provides improved TypeScript integration and development tooling for better DX when working with TypeScript in Nuxt applications.
|
|
768
|
+
|
|
769
|
+
This flag is disabled by default, but you can enable this feature:
|
|
770
|
+
|
|
771
|
+
```ts twoslash [nuxt.config.ts]
|
|
772
|
+
export default defineNuxtConfig({
|
|
773
|
+
experimental: {
|
|
774
|
+
typescriptPlugin: true,
|
|
775
|
+
},
|
|
776
|
+
})
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
::important
|
|
780
|
+
To use this feature, you need to:
|
|
781
|
+
- Have `typescript` installed as a dependency
|
|
782
|
+
- Configure VS Code to use your workspace TypeScript version (see [VS Code documentation](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript))
|
|
783
|
+
::
|
|
784
|
+
|
|
785
|
+
::read-more{icon="i-simple-icons-github" to="https://github.com/KazariEX/dxup" target="_blank"}
|
|
786
|
+
Learn more about **@dxup/nuxt**.
|
|
787
|
+
::
|
|
788
|
+
|
|
789
|
+
## viteEnvironmentApi
|
|
790
|
+
|
|
791
|
+
Enable Vite 6's new [Environment API](https://vite.dev/guide/api-environment) for improved build configuration and plugin architecture.
|
|
792
|
+
|
|
793
|
+
When you set `future.compatibilityVersion` to `5`, this feature is enabled by default. You can also enable it explicitly for testing:
|
|
794
|
+
|
|
795
|
+
```ts twoslash [nuxt.config.ts]
|
|
796
|
+
export default defineNuxtConfig({
|
|
797
|
+
experimental: {
|
|
798
|
+
viteEnvironmentApi: true,
|
|
799
|
+
},
|
|
800
|
+
})
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
The Vite Environment API provides better consistency between development and production builds, more granular control over environment-specific configuration, and improved performance.
|
|
804
|
+
|
|
805
|
+
::important
|
|
806
|
+
Enabling this feature changes how Vite plugins are registered and configured. See the [Vite Environment API migration guide](/docs/4.x/getting-started/upgrade#migration-to-vite-environment-api) for details on updating your plugins.
|
|
807
|
+
::
|
|
808
|
+
|
|
809
|
+
::read-more{to="https://vite.dev/guide/api-environment" target="_blank"}
|
|
810
|
+
Learn more about Vite's Environment API.
|
|
811
|
+
::
|
|
@@ -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/3.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.
|
|
@@ -234,22 +234,30 @@ export default defineNuxtConfig({
|
|
|
234
234
|
|
|
235
235
|
## Multi-Layer Support for Nuxt Modules
|
|
236
236
|
|
|
237
|
-
You can use the
|
|
237
|
+
You can use the [`getLayerDirectories`](/docs/api/kit/layers#getlayerdirectories) utility from Nuxt Kit to support custom multi-layer handling for your modules.
|
|
238
238
|
|
|
239
239
|
```ts [modules/my-module.ts]
|
|
240
|
+
import { defineNuxtModule, getLayerDirectories } from 'nuxt/kit'
|
|
241
|
+
|
|
240
242
|
export default defineNuxtModule({
|
|
241
243
|
setup (_options, nuxt) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
244
|
+
const layerDirs = getLayerDirectories()
|
|
245
|
+
|
|
246
|
+
for (const [index, layer] of layerDirs.entries()) {
|
|
247
|
+
console.log(`Layer ${index}:`)
|
|
248
|
+
console.log(` Root: ${layer.root}`)
|
|
249
|
+
console.log(` App: ${layer.app}`)
|
|
250
|
+
console.log(` Server: ${layer.server}`)
|
|
251
|
+
console.log(` Pages: ${layer.appPages}`)
|
|
252
|
+
// ... other directories
|
|
245
253
|
}
|
|
246
254
|
},
|
|
247
255
|
})
|
|
248
256
|
```
|
|
249
257
|
|
|
250
258
|
**Notes:**
|
|
251
|
-
- Earlier items in the
|
|
252
|
-
- The user's project is the first item in the
|
|
259
|
+
- Earlier items in the array have higher priority and override later ones
|
|
260
|
+
- The user's project is the first item in the array
|
|
253
261
|
|
|
254
262
|
## Going Deeper
|
|
255
263
|
|
|
@@ -63,3 +63,44 @@ import config from '~/data/hello.yaml'
|
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
::
|
|
66
|
+
|
|
67
|
+
## Using Vite Plugins in Nuxt Modules
|
|
68
|
+
|
|
69
|
+
If you're developing a Nuxt module and need to add Vite plugins, you should use the [`addVitePlugin`](/docs/4.x/api/kit/builder#addviteplugin) utility:
|
|
70
|
+
|
|
71
|
+
```ts [modules/my-module.ts]
|
|
72
|
+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
|
|
73
|
+
import yaml from '@rollup/plugin-yaml'
|
|
74
|
+
|
|
75
|
+
export default defineNuxtModule({
|
|
76
|
+
setup () {
|
|
77
|
+
addVitePlugin(yaml())
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For environment-specific plugins in Nuxt 5+, use the `applyToEnvironment()` method:
|
|
83
|
+
|
|
84
|
+
```ts [modules/my-module.ts]
|
|
85
|
+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
|
|
86
|
+
|
|
87
|
+
export default defineNuxtModule({
|
|
88
|
+
setup () {
|
|
89
|
+
addVitePlugin(() => ({
|
|
90
|
+
name: 'my-client-plugin',
|
|
91
|
+
applyToEnvironment (environment) {
|
|
92
|
+
return environment.name === 'client'
|
|
93
|
+
},
|
|
94
|
+
// Plugin configuration
|
|
95
|
+
}))
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
::important
|
|
101
|
+
If you're writing code that needs to access resolved Vite configuration, you should use the `config` and `configResolved` hooks _within_ your Vite plugin, rather than using Nuxt's `vite:extend`, `vite:extendConfig` and `vite:configResolved`.
|
|
102
|
+
::
|
|
103
|
+
|
|
104
|
+
::read-more{to="/docs/4.x/api/kit/builder#addviteplugin"}
|
|
105
|
+
Read more about `addVitePlugin` in the Nuxt Kit documentation.
|
|
106
|
+
::
|
|
@@ -104,13 +104,17 @@ Enables relative time formatting using the Intl.RelativeTimeFormat API:
|
|
|
104
104
|
|
|
105
105
|
When `relative` is set to `true`, the component also accepts properties from [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat):
|
|
106
106
|
|
|
107
|
+
::warning
|
|
108
|
+
Due to `style` being a reserved prop, `relativeStyle` prop is used instead.
|
|
109
|
+
::
|
|
110
|
+
|
|
107
111
|
```vue
|
|
108
112
|
<template>
|
|
109
113
|
<NuxtTime
|
|
110
114
|
:datetime="Date.now() - 3 * 24 * 60 * 60 * 1000"
|
|
111
115
|
relative
|
|
112
116
|
numeric="auto"
|
|
113
|
-
style="long"
|
|
117
|
+
relative-style="long"
|
|
114
118
|
/>
|
|
115
119
|
</template>
|
|
116
120
|
```
|
|
@@ -102,6 +102,7 @@ The `handler` function should be **side-effect free** to ensure predictable beha
|
|
|
102
102
|
- `dedupe`: avoid fetching same key more than once at a time (defaults to `cancel`). Possible options:
|
|
103
103
|
- `cancel` - cancels existing requests when a new one is made
|
|
104
104
|
- `defer` - does not make new requests at all if there is a pending request
|
|
105
|
+
- `timeout` - a number in milliseconds to wait before timing out the request (defaults to `undefined`, which means no timeout)
|
|
105
106
|
|
|
106
107
|
::note
|
|
107
108
|
Under the hood, `lazy: false` uses `<Suspense>` to block the loading of the route before the data has been fetched. Consider using `lazy: true` and implementing a loading state instead for a snappier user experience.
|
|
@@ -170,12 +171,12 @@ If you have not fetched data on the server (for example, with `server: false`),
|
|
|
170
171
|
|
|
171
172
|
```ts [Signature]
|
|
172
173
|
export function useAsyncData<DataT, DataE> (
|
|
173
|
-
handler: (nuxtApp
|
|
174
|
+
handler: (nuxtApp: NuxtApp, options: { signal: AbortSignal }) => Promise<DataT>,
|
|
174
175
|
options?: AsyncDataOptions<DataT>
|
|
175
176
|
): AsyncData<DataT, DataE>
|
|
176
177
|
export function useAsyncData<DataT, DataE> (
|
|
177
178
|
key: MaybeRefOrGetter<string>,
|
|
178
|
-
handler: (nuxtApp
|
|
179
|
+
handler: (nuxtApp: NuxtApp, options: { signal: AbortSignal }) => Promise<DataT>,
|
|
179
180
|
options?: AsyncDataOptions<DataT>
|
|
180
181
|
): Promise<AsyncData<DataT, DataE>>
|
|
181
182
|
|
|
@@ -190,6 +191,7 @@ type AsyncDataOptions<DataT> = {
|
|
|
190
191
|
pick?: string[]
|
|
191
192
|
watch?: MultiWatchSources | false
|
|
192
193
|
getCachedData?: (key: string, nuxtApp: NuxtApp, ctx: AsyncDataRequestContext) => DataT | undefined
|
|
194
|
+
timeout?: number
|
|
193
195
|
}
|
|
194
196
|
|
|
195
197
|
type AsyncDataRequestContext = {
|
|
@@ -208,6 +210,8 @@ type AsyncData<DataT, ErrorT> = {
|
|
|
208
210
|
|
|
209
211
|
interface AsyncDataExecuteOptions {
|
|
210
212
|
dedupe?: 'cancel' | 'defer'
|
|
213
|
+
timeout?: number
|
|
214
|
+
signal?: AbortSignal
|
|
211
215
|
}
|
|
212
216
|
|
|
213
217
|
type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
@@ -145,6 +145,7 @@ type UseFetchOptions<DataT> = {
|
|
|
145
145
|
getCachedData?: (key: string, nuxtApp: NuxtApp, ctx: AsyncDataRequestContext) => DataT | undefined
|
|
146
146
|
deep?: boolean
|
|
147
147
|
dedupe?: 'cancel' | 'defer'
|
|
148
|
+
timeout?: number
|
|
148
149
|
default?: () => DataT
|
|
149
150
|
transform?: (input: DataT) => DataT | Promise<DataT>
|
|
150
151
|
pick?: string[]
|
|
@@ -169,6 +170,8 @@ type AsyncData<DataT, ErrorT> = {
|
|
|
169
170
|
|
|
170
171
|
interface AsyncDataExecuteOptions {
|
|
171
172
|
dedupe?: 'cancel' | 'defer'
|
|
173
|
+
timeout?: number
|
|
174
|
+
signal?: AbortSignal
|
|
172
175
|
}
|
|
173
176
|
|
|
174
177
|
type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
@@ -195,6 +198,7 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
195
198
|
| `lazy` | `boolean` | `false` | If true, resolves after route loads (does not block navigation). |
|
|
196
199
|
| `immediate` | `boolean` | `true` | If false, prevents request from firing immediately. |
|
|
197
200
|
| `default` | `() => DataT` | - | Factory for default value of `data` before async resolves. |
|
|
201
|
+
| `timeout` | `number` | - | A number in milliseconds to wait before timing out the request (defaults to `undefined`, which means no timeout) |
|
|
198
202
|
| `transform` | `(input: DataT) => DataT \| Promise<DataT>` | - | Function to transform the result after resolving. |
|
|
199
203
|
| `getCachedData`| `(key, nuxtApp, ctx) => DataT \| undefined` | - | Function to return cached data. See below for default. |
|
|
200
204
|
| `pick` | `string[]` | - | Only pick specified keys from the result. |
|
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
|
package/3.api/5.kit/11.nitro.md
CHANGED
|
@@ -197,6 +197,10 @@ Add plugin to extend Nitro's runtime behavior.
|
|
|
197
197
|
You can read more about Nitro plugins in the [Nitro documentation](https://nitro.build/guide/plugins).
|
|
198
198
|
::
|
|
199
199
|
|
|
200
|
+
::warning
|
|
201
|
+
It is necessary to explicitly import `defineNitroPlugin` from `nitropack/runtime` within your plugin file. The same requirement applies to utilities such as `useRuntimeConfig`.
|
|
202
|
+
::
|
|
203
|
+
|
|
200
204
|
### Usage
|
|
201
205
|
|
|
202
206
|
```ts twoslash
|
|
@@ -14,6 +14,10 @@ Nuxt have builders based on [Vite](https://github.com/nuxt/nuxt/tree/main/packag
|
|
|
14
14
|
|
|
15
15
|
Extends the Vite configuration. Callback function can be called multiple times, when applying to both client and server builds.
|
|
16
16
|
|
|
17
|
+
::warning
|
|
18
|
+
This hook is now deprecated, and we recommend using a Vite plugin instead with a `config` hook, or — for environment-specific configuration — the `applyToEnvironment` hook.
|
|
19
|
+
::
|
|
20
|
+
|
|
17
21
|
### Usage
|
|
18
22
|
|
|
19
23
|
```ts twoslash
|
|
@@ -30,6 +34,45 @@ export default defineNuxtModule({
|
|
|
30
34
|
})
|
|
31
35
|
```
|
|
32
36
|
|
|
37
|
+
For environment-specific configuration in Nuxt 5+, use `addVitePlugin()` instead:
|
|
38
|
+
|
|
39
|
+
```ts twoslash
|
|
40
|
+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
|
|
41
|
+
|
|
42
|
+
export default defineNuxtModule({
|
|
43
|
+
setup () {
|
|
44
|
+
// For global configuration (affects all environments)
|
|
45
|
+
addVitePlugin(() => ({
|
|
46
|
+
name: 'my-global-plugin',
|
|
47
|
+
config (config) {
|
|
48
|
+
// This runs before environment setup
|
|
49
|
+
config.optimizeDeps ||= {}
|
|
50
|
+
config.optimizeDeps.include ||= []
|
|
51
|
+
config.optimizeDeps.include.push('cross-fetch')
|
|
52
|
+
},
|
|
53
|
+
}))
|
|
54
|
+
|
|
55
|
+
// For environment-specific configuration
|
|
56
|
+
addVitePlugin(() => ({
|
|
57
|
+
name: 'my-client-plugin',
|
|
58
|
+
applyToEnvironment (environment) {
|
|
59
|
+
return environment.name === 'client'
|
|
60
|
+
},
|
|
61
|
+
configEnvironment (name, config) {
|
|
62
|
+
// This only affects the client environment
|
|
63
|
+
config.optimizeDeps ||= {}
|
|
64
|
+
config.optimizeDeps.include ||= []
|
|
65
|
+
config.optimizeDeps.include.push('client-only-package')
|
|
66
|
+
},
|
|
67
|
+
}))
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
::warning
|
|
73
|
+
**Important:** The `config` hook runs before `applyToEnvironment` and modifies the global configuration. Use `configEnvironment` for environment-specific configuration changes.
|
|
74
|
+
::
|
|
75
|
+
|
|
33
76
|
### Type
|
|
34
77
|
|
|
35
78
|
```ts twoslash
|
|
@@ -54,8 +97,8 @@ Checkout Vite website for more information about its configuration.
|
|
|
54
97
|
| --------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------ |
|
|
55
98
|
| `dev` | `boolean` | `false` | If set to `true`, the callback function will be called when building in development mode. |
|
|
56
99
|
| `build` | `boolean` | `false` | If set to `true`, the callback function will be called when building in production mode. |
|
|
57
|
-
| `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle.
|
|
58
|
-
| `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle.
|
|
100
|
+
| `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. **Deprecated in Nuxt 5+.** Use `addVitePlugin()` with `applyToEnvironment()` instead. |
|
|
101
|
+
| `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. **Deprecated in Nuxt 5+.** Use `addVitePlugin()` with `applyToEnvironment()` instead. |
|
|
59
102
|
| `prepend` | `boolean` | `false` | If set to `true`, the callback function will be prepended to the array with `unshift()` instead of `push()`. |
|
|
60
103
|
|
|
61
104
|
## `extendWebpackConfig`
|
|
@@ -111,6 +154,10 @@ Checkout webpack website for more information about its configuration.
|
|
|
111
154
|
|
|
112
155
|
Append Vite plugin to the config.
|
|
113
156
|
|
|
157
|
+
::warning
|
|
158
|
+
In Nuxt 5+, plugins registered with `server: false` or `client: false` options will not have their `config` or `configResolved` hooks called. Instead, use the `applyToEnvironment()` method instead for environment-specific plugins.
|
|
159
|
+
::
|
|
160
|
+
|
|
114
161
|
### Usage
|
|
115
162
|
|
|
116
163
|
```ts twoslash
|
|
@@ -131,6 +178,15 @@ export default defineNuxtModule({
|
|
|
131
178
|
},
|
|
132
179
|
setup (options) {
|
|
133
180
|
addVitePlugin(svg4VuePlugin(options.svg4vue))
|
|
181
|
+
|
|
182
|
+
// or, to add a vite plugin to only one environment
|
|
183
|
+
addVitePlugin(() => ({
|
|
184
|
+
name: 'my-client-plugin',
|
|
185
|
+
applyToEnvironment (environment) {
|
|
186
|
+
return environment.name === 'client'
|
|
187
|
+
},
|
|
188
|
+
// ... rest of your client-only plugin
|
|
189
|
+
}))
|
|
134
190
|
},
|
|
135
191
|
})
|
|
136
192
|
```
|
|
@@ -159,8 +215,8 @@ See [Vite website](https://vite.dev/guide/api-plugin.html) for more information
|
|
|
159
215
|
| --------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------ |
|
|
160
216
|
| `dev` | `boolean` | `false` | If set to `true`, the callback function will be called when building in development mode. |
|
|
161
217
|
| `build` | `boolean` | `false` | If set to `true`, the callback function will be called when building in production mode. |
|
|
162
|
-
| `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle.
|
|
163
|
-
| `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle.
|
|
218
|
+
| `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. **Deprecated in Nuxt 5+.** Use `applyToEnvironment()` instead. |
|
|
219
|
+
| `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. **Deprecated in Nuxt 5+.** Use `applyToEnvironment()` instead. |
|
|
164
220
|
| `prepend` | `boolean` | `false` | If set to `true`, the callback function will be prepended to the array with `unshift()` instead of `push()`. |
|
|
165
221
|
|
|
166
222
|
## `addWebpackPlugin`
|
|
@@ -22,11 +22,9 @@ import { buildNuxt, loadNuxt } from '@nuxt/kit'
|
|
|
22
22
|
async function getViteConfig () {
|
|
23
23
|
const nuxt = await loadNuxt({ cwd: process.cwd(), dev: false, overrides: { ssr: false } })
|
|
24
24
|
return new Promise((resolve, reject) => {
|
|
25
|
-
nuxt.hook('vite:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
throw new Error('_stop_')
|
|
29
|
-
}
|
|
25
|
+
nuxt.hook('vite:extend', (config) => {
|
|
26
|
+
resolve(config)
|
|
27
|
+
throw new Error('_stop_')
|
|
30
28
|
})
|
|
31
29
|
buildNuxt(nuxt).catch((err) => {
|
|
32
30
|
if (!err.toString().includes('_stop_')) {
|
|
@@ -113,6 +113,7 @@ function addComponent (options: AddComponentOptions): void
|
|
|
113
113
|
| ------------------ | ---------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
|
|
114
114
|
| `name` | `string` | `true` | Component name. |
|
|
115
115
|
| `filePath` | `string` | `true` | Path to the component. |
|
|
116
|
+
| `declarationPath` | `string` | `false` | Path to component's declaration file. It is used to generate components' [type templates](/docs/4.x/api/kit/templates#addtypetemplate); if not provided, `filePath` is used instead. |
|
|
116
117
|
| `pascalName` | `string` | `false` | Pascal case component name. If not provided, it will be generated from the component name. |
|
|
117
118
|
| `kebabName` | `string` | `false` | Kebab case component name. If not provided, it will be generated from the component name. |
|
|
118
119
|
| `export` | `string` | `false` | Specify named or default export. If not provided, it will be set to `'default'`. |
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Head
|
|
3
|
+
description: Nuxt Kit provides utilities to help you manage head configuration in modules.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/kit/src/head.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## `setGlobalHead`
|
|
12
|
+
|
|
13
|
+
Sets global head configuration for your Nuxt application. This utility allows modules to programmatically configure meta tags, links, scripts, and other head elements that will be applied across all pages.
|
|
14
|
+
|
|
15
|
+
The provided head configuration will be merged with any existing head configuration using deep merging, with your provided values taking precedence.
|
|
16
|
+
|
|
17
|
+
::tip
|
|
18
|
+
This is particularly useful for modules that need to inject global meta tags, stylesheets, or scripts into the application head.
|
|
19
|
+
::
|
|
20
|
+
|
|
21
|
+
### Type
|
|
22
|
+
|
|
23
|
+
```ts twoslash
|
|
24
|
+
// @errors: 2391
|
|
25
|
+
// ---cut---
|
|
26
|
+
import type { SerializableHead } from '@unhead/vue/types'
|
|
27
|
+
|
|
28
|
+
interface AppHeadMetaObject extends SerializableHead {
|
|
29
|
+
charset?: string
|
|
30
|
+
viewport?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function setGlobalHead (head: AppHeadMetaObject): void
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Parameters
|
|
37
|
+
|
|
38
|
+
#### `head`
|
|
39
|
+
|
|
40
|
+
**Type**: `AppHeadMetaObject`
|
|
41
|
+
|
|
42
|
+
An object containing head configuration. All properties are optional and will be merged with existing configuration:
|
|
43
|
+
|
|
44
|
+
- `charset`: Character encoding for the document
|
|
45
|
+
- `viewport`: Viewport meta tag configuration
|
|
46
|
+
- `meta`: Array of meta tag objects
|
|
47
|
+
- `link`: Array of link tag objects (stylesheets, icons, etc.)
|
|
48
|
+
- `style`: Array of inline style tag objects
|
|
49
|
+
- `script`: Array of script tag objects
|
|
50
|
+
- `noscript`: Array of noscript tag objects
|
|
51
|
+
- `title`: Default page title
|
|
52
|
+
- `titleTemplate`: Template for formatting page titles
|
|
53
|
+
- `bodyAttrs`: Attributes to add to the `<body>` tag
|
|
54
|
+
- `htmlAttrs`: Attributes to add to the `<html>` tag
|
|
55
|
+
|
|
56
|
+
### Examples
|
|
57
|
+
|
|
58
|
+
#### Adding Global Meta Tags
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
|
|
62
|
+
|
|
63
|
+
export default defineNuxtModule({
|
|
64
|
+
setup () {
|
|
65
|
+
setGlobalHead({
|
|
66
|
+
meta: [
|
|
67
|
+
{ name: 'theme-color', content: '#ffffff' },
|
|
68
|
+
{ name: 'author', content: 'Your Name' },
|
|
69
|
+
],
|
|
70
|
+
})
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Injecting Global Stylesheets
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
|
|
79
|
+
|
|
80
|
+
export default defineNuxtModule({
|
|
81
|
+
setup () {
|
|
82
|
+
setGlobalHead({
|
|
83
|
+
link: [
|
|
84
|
+
{
|
|
85
|
+
rel: 'stylesheet',
|
|
86
|
+
href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
})
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Adding Global Scripts
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
|
|
98
|
+
|
|
99
|
+
export default defineNuxtModule({
|
|
100
|
+
setup () {
|
|
101
|
+
setGlobalHead({
|
|
102
|
+
script: [
|
|
103
|
+
{
|
|
104
|
+
src: 'https://cdn.example.com/analytics.js',
|
|
105
|
+
async: true,
|
|
106
|
+
defer: true,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
})
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Setting HTML Attributes
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
|
|
118
|
+
|
|
119
|
+
export default defineNuxtModule({
|
|
120
|
+
setup () {
|
|
121
|
+
setGlobalHead({
|
|
122
|
+
htmlAttrs: {
|
|
123
|
+
lang: 'en',
|
|
124
|
+
dir: 'ltr',
|
|
125
|
+
},
|
|
126
|
+
bodyAttrs: {
|
|
127
|
+
class: 'custom-body-class',
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
```
|
|
@@ -75,8 +75,8 @@ Hook | Arguments | Description
|
|
|
75
75
|
`schema:beforeWrite` | `schema` | Called before writing the given schema.
|
|
76
76
|
`schema:written` | - | Called after the schema is written.
|
|
77
77
|
`vite:extend` | `viteBuildContext` | Allows extending Vite default context.
|
|
78
|
-
`vite:extendConfig` | `viteInlineConfig, env` | Allows extending Vite default config.
|
|
79
|
-
`vite:configResolved` | `viteInlineConfig, env` | Allows reading the resolved Vite config.
|
|
78
|
+
`vite:extendConfig` | `viteInlineConfig, env` | Allows extending Vite default config. **Deprecated in Nuxt 5+.** In Nuxt 5, this operates on a shared configuration rather than separate client/server configs.
|
|
79
|
+
`vite:configResolved` | `viteInlineConfig, env` | Allows reading the resolved Vite config. **Deprecated in Nuxt 5+.** In Nuxt 5, this operates on a shared configuration rather than separate client/server configs.
|
|
80
80
|
`vite:serverCreated` | `viteServer, env` | Called when the Vite server is created.
|
|
81
81
|
`vite:compiled` | - | Called after Vite server is compiled.
|
|
82
82
|
`webpack:config` | `webpackConfigs` | Called before configuring the webpack compiler.
|