@nuxt/docs-nightly 4.2.3-29422039.47e05245 → 4.2.3-29424584.c1b3590f
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 -3
- package/1.getting-started/06.styling.md +1 -1
- package/1.getting-started/17.testing.md +1 -1
- package/1.getting-started/18.upgrade.md +1 -1
- package/2.directory-structure/1.modules.md +1 -1
- package/3.guide/0.index.md +8 -2
- package/3.guide/1.concepts/5.modules.md +1 -1
- package/3.guide/1.concepts/8.typescript.md +2 -2
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +299 -0
- package/3.guide/4.modules/4.recipes-advanced.md +231 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/3.guide/{5.going-further → 6.going-further}/1.internals.md +3 -3
- package/3.guide/{5.going-further → 6.going-further}/2.hooks.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/4.kit.md +1 -1
- package/4.api/4.commands/add.md +1 -1
- package/5.community/4.contribution.md +3 -3
- package/5.community/5.framework-contribution.md +1 -1
- package/7.migration/2.configuration.md +2 -2
- package/7.migration/20.module-authors.md +2 -2
- package/package.json +1 -1
- package/3.guide/5.going-further/3.modules.md +0 -968
- /package/3.guide/{4.recipes → 5.recipes}/.navigation.yml +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/1.custom-routing.md +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/2.vite-plugin.md +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/3.custom-usefetch.md +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/4.sessions-and-authentication.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/.navigation.yml +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.events.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.experimental-features.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.features.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/10.runtime-config.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/11.nightly-release-channel.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/6.nuxt-app.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/7.layers.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/9.debugging.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/index.md +0 -0
|
@@ -17,9 +17,7 @@ Or follow the steps below to set up a new Nuxt project on your computer.
|
|
|
17
17
|
|
|
18
18
|
## New Project
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
<!-- markdownlint-disable-next-line MD001 -->
|
|
22
|
-
#### Prerequisites
|
|
20
|
+
### Prerequisites
|
|
23
21
|
|
|
24
22
|
- **Node.js** - [`20.x`](https://nodejs.org/en) or newer (but we recommend the [active LTS release](https://github.com/nodejs/release#release-schedule))
|
|
25
23
|
- **Text editor** - There is no IDE requirement, but we recommend [Visual Studio Code](https://code.visualstudio.com/) with the [official Vue extension](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (previously known as Volar) or [WebStorm](https://www.jetbrains.com/webstorm/), which, along with [other JetBrains IDEs](https://www.jetbrains.com/ides/), offers great Nuxt support right out-of-the-box.
|
|
@@ -517,7 +517,7 @@ Here are a few modules to help you get started:
|
|
|
517
517
|
- [Nuxt UI](https://ui.nuxt.com): A UI Library for Modern Web Apps
|
|
518
518
|
- [Panda CSS](https://panda-css.com/docs/installation/nuxt): CSS-in-JS engine that generates atomic CSS at build time
|
|
519
519
|
|
|
520
|
-
Nuxt modules provide you with a good developer experience out of the box, but remember that if your favorite tool doesn't have a module, it doesn't mean that you can't use it with Nuxt! You can configure it yourself for your own project. Depending on the tool, you might need to use a [Nuxt plugin](/docs/4.x/directory-structure/app/plugins) and/or [make your own module](/docs/4.x/guide/
|
|
520
|
+
Nuxt modules provide you with a good developer experience out of the box, but remember that if your favorite tool doesn't have a module, it doesn't mean that you can't use it with Nuxt! You can configure it yourself for your own project. Depending on the tool, you might need to use a [Nuxt plugin](/docs/4.x/directory-structure/app/plugins) and/or [make your own module](/docs/4.x/guide/modules). Share them with the [community](/modules) if you do!
|
|
521
521
|
|
|
522
522
|
### Easily Load Webfonts
|
|
523
523
|
|
|
@@ -5,7 +5,7 @@ navigation.icon: i-lucide-circle-check
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
::tip
|
|
8
|
-
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/4.x/guide/
|
|
8
|
+
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/4.x/guide/modules/testing).
|
|
9
9
|
::
|
|
10
10
|
|
|
11
11
|
Nuxt offers first-class support for end-to-end and unit testing of your Nuxt application via `@nuxt/test-utils`, a library of test utilities and configuration that currently powers the [tests we use on Nuxt itself](https://github.com/nuxt/nuxt/tree/main/test) and tests throughout the module ecosystem.
|
|
@@ -463,7 +463,7 @@ export default defineNuxtConfig({
|
|
|
463
463
|
// 4. project-module-2 (can override layer modules)
|
|
464
464
|
```
|
|
465
465
|
|
|
466
|
-
If you encounter issues with module order dependencies due to needing to register a hook, consider using the [`modules:done` hook](/docs/4.x/guide/
|
|
466
|
+
If you encounter issues with module order dependencies due to needing to register a hook, consider using the [`modules:done` hook](/docs/4.x/guide/modules/recipes-advanced) for modules that need to call a hook. This is run after all other modules have been loaded, which means it is safe to use.
|
|
467
467
|
|
|
468
468
|
👉 See [PR #31507](https://github.com/nuxt/nuxt/pull/31507) and [issue #25719](https://github.com/nuxt/nuxt/issues/25719) for more details.
|
|
469
469
|
|
|
@@ -59,7 +59,7 @@ modules/
|
|
|
59
59
|
2.second-module.ts
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
:read-more{to="/docs/4.x/guide/
|
|
62
|
+
:read-more{to="/docs/4.x/guide/modules"}
|
|
63
63
|
|
|
64
64
|
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/creating-your-first-module-from-scratch?friend=nuxt" target="_blank"}
|
|
65
65
|
Watch Vue School video about Nuxt private modules.
|
package/3.guide/0.index.md
CHANGED
|
@@ -11,12 +11,18 @@ surround: false
|
|
|
11
11
|
Discover the main concepts behind Nuxt, from auto-import, hybrid rendering to its TypeScript support.
|
|
12
12
|
::
|
|
13
13
|
::card{icon="i-lucide-square-check" title="Best Practices" to="/docs/4.x/guide/best-practices"}
|
|
14
|
-
Learn about best practices when developing with Nuxt
|
|
14
|
+
Learn about best practices when developing with Nuxt.
|
|
15
|
+
::
|
|
16
|
+
::card{icon="i-lucide-bot" title="Working with AI" to="/docs/4.x/guide/ai"}
|
|
17
|
+
Integrate AI tools into your Nuxt workflow with MCP Server and LLMs.txt.
|
|
18
|
+
::
|
|
19
|
+
::card{icon="i-lucide-box" title="Module Author Guide" to="/docs/4.x/guide/modules"}
|
|
20
|
+
Learn how to create Nuxt modules to integrate, enhance or extend any Nuxt application.
|
|
15
21
|
::
|
|
16
22
|
::card{icon="i-lucide-book-open" title="Recipes" to="/docs/4.x/guide/recipes"}
|
|
17
23
|
Find solutions to common problems and learn how to implement them in your Nuxt project.
|
|
18
24
|
::
|
|
19
25
|
::card{icon="i-lucide-star" title="Going Further" to="/docs/4.x/guide/going-further"}
|
|
20
|
-
Master Nuxt with advanced concepts like experimental features, hooks,
|
|
26
|
+
Master Nuxt with advanced concepts like experimental features, hooks, and more.
|
|
21
27
|
::
|
|
22
28
|
::
|
|
@@ -45,4 +45,4 @@ Nuxt modules are now build-time-only, and the `buildModules` property used in Nu
|
|
|
45
45
|
|
|
46
46
|
Everyone has the opportunity to develop modules and we cannot wait to see what you will build.
|
|
47
47
|
|
|
48
|
-
:read-more{to="/docs/4.x/guide/
|
|
48
|
+
:read-more{to="/docs/4.x/guide/modules" title="Module Author Guide"}
|
|
@@ -52,7 +52,7 @@ Nuxt projects rely on auto-generated types to work properly. These types are sto
|
|
|
52
52
|
The generated `tsconfig.json` files inside the [`.nuxt`](/docs/4.x/directory-structure/nuxt) directory include **recommended basic TypeScript configuration** for your project, references to [auto-imports](/docs/4.x/guide/concepts/auto-imports), [API route types](/docs/4.x/guide/concepts/server-engine#typed-api-routes), path aliases like `#imports`, `~/file`, or `#build/file`, and more.
|
|
53
53
|
|
|
54
54
|
::warning
|
|
55
|
-
Nuxt relies on this configuration, and [Nuxt
|
|
55
|
+
Nuxt relies on this configuration, and [Nuxt modules](/docs/4.x/guide/modules) can extend it as well. For this reason, it is not recommended to modify your `tsconfig.json` file directly, as doing so could overwrite important settings. Instead, extend it via `nuxt.config.ts`. [Learn more about extending the configuration here](/docs/4.x/directory-structure/tsconfig).
|
|
56
56
|
::
|
|
57
57
|
|
|
58
58
|
::tip{icon="i-lucide-video" to="https://youtu.be/umLI7SlPygY" target="_blank"}
|
|
@@ -95,7 +95,7 @@ Similarly:
|
|
|
95
95
|
- For the `server` context, place the augmentation file in the `server/` directory.
|
|
96
96
|
- For types that are **shared between the app and server**, place the file in the `shared/` directory.
|
|
97
97
|
|
|
98
|
-
::read-more{to="/docs/4.x/guide/
|
|
98
|
+
::read-more{to="/docs/4.x/guide/modules/recipes-advanced#extend-typescript-config"}
|
|
99
99
|
Read more about augmenting specific type contexts from **files outside those contexts** in the Module Author Guide.
|
|
100
100
|
::
|
|
101
101
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Create Your First Module"
|
|
3
|
+
description: "Learn how to create your first Nuxt module using the official starter template."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Create a Module
|
|
7
|
+
|
|
8
|
+
We recommend you get started with Nuxt modules using our [starter template](https://github.com/nuxt/starter/tree/module):
|
|
9
|
+
|
|
10
|
+
::code-group{sync="pm"}
|
|
11
|
+
|
|
12
|
+
```bash [npm]
|
|
13
|
+
npm create nuxt -- -t module my-module
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```bash [yarn]
|
|
17
|
+
yarn create nuxt -t module my-module
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash [pnpm]
|
|
21
|
+
pnpm create nuxt -t module my-module
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash [bun]
|
|
25
|
+
bun create nuxt --template=module my-module
|
|
26
|
+
```
|
|
27
|
+
::
|
|
28
|
+
|
|
29
|
+
This will create a `my-module` project with all the boilerplate necessary to develop and publish your module.
|
|
30
|
+
|
|
31
|
+
**Next steps:**
|
|
32
|
+
|
|
33
|
+
1. Open `my-module` in your IDE of choice
|
|
34
|
+
2. Install dependencies using your favorite package manager
|
|
35
|
+
3. Prepare local files for development using `npm run dev:prepare`
|
|
36
|
+
4. Follow this document to learn more about Nuxt modules
|
|
37
|
+
|
|
38
|
+
## Use the Starter Template
|
|
39
|
+
|
|
40
|
+
Learn how to perform basic tasks with the module starter.
|
|
41
|
+
|
|
42
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/navigating-the-official-starter-template?friend=nuxt" target="_blank"}
|
|
43
|
+
Watch Vue School video about Nuxt module starter template.
|
|
44
|
+
::
|
|
45
|
+
|
|
46
|
+
### Develop Your Module
|
|
47
|
+
|
|
48
|
+
While your module source code lives inside the `src` directory, to develop a module you often need a Nuxt application to test it against. That's what the `playground` directory is for. It's a Nuxt application you can tinker with that is already configured to run with your module.
|
|
49
|
+
|
|
50
|
+
You can interact with the playground like with any Nuxt application.
|
|
51
|
+
|
|
52
|
+
- Launch its development server with `npm run dev`, it should reload itself as you make changes to your module in the `src` directory
|
|
53
|
+
- Build it with `npm run dev:build`
|
|
54
|
+
|
|
55
|
+
::note
|
|
56
|
+
All other `nuxt` commands can be used against the `playground` directory (e.g. `nuxt <COMMAND> playground`). Feel free to declare additional `dev:*` scripts within your `package.json` referencing them for convenience.
|
|
57
|
+
::
|
|
58
|
+
|
|
59
|
+
### Run Tests
|
|
60
|
+
|
|
61
|
+
The module starter comes with a basic test suite:
|
|
62
|
+
|
|
63
|
+
- A linter powered by [ESLint](https://eslint.org), run it with `npm run lint`
|
|
64
|
+
- A test runner powered by [Vitest](https://vitest.dev), run it with `npm run test` or `npm run test:watch`
|
|
65
|
+
|
|
66
|
+
::tip
|
|
67
|
+
Feel free to augment this default test strategy to better suit your needs.
|
|
68
|
+
::
|
|
69
|
+
|
|
70
|
+
### Build Your Module
|
|
71
|
+
|
|
72
|
+
Nuxt modules come with their own builder provided by [`@nuxt/module-builder`](https://github.com/nuxt/module-builder#readme). This builder doesn't require any configuration on your end, supports TypeScript, and makes sure your assets are properly bundled to be distributed to other Nuxt applications.
|
|
73
|
+
|
|
74
|
+
You can build your module by running `npm run prepack`.
|
|
75
|
+
|
|
76
|
+
::tip
|
|
77
|
+
While building your module can be useful in some cases, most of the time you won't need to build it on your own: the `playground` takes care of it while developing, and the release script also has you covered when publishing.
|
|
78
|
+
::
|
|
79
|
+
|
|
80
|
+
### Publish to npm
|
|
81
|
+
|
|
82
|
+
::important
|
|
83
|
+
Before publishing your module to npm, makes sure you have an [npmjs.com](https://www.npmjs.com) account and that you're authenticated to it locally with `npm login`.
|
|
84
|
+
::
|
|
85
|
+
|
|
86
|
+
While you can publish your module by bumping its version and using the `npm publish` command, the module starter comes with a release script that helps you make sure you publish a working version of your module to npm and more.
|
|
87
|
+
|
|
88
|
+
To use the release script, first, commit all your changes (we recommend you follow [Conventional Commits](https://www.conventionalcommits.org) to also take advantage of automatic version bump and changelog update), then run the release script with `npm run release`.
|
|
89
|
+
|
|
90
|
+
When running the release script, the following will happen:
|
|
91
|
+
|
|
92
|
+
- First, it will run your test suite by:
|
|
93
|
+
- Running the linter (`npm run lint`)
|
|
94
|
+
- Running unit, integration, and e2e tests (`npm run test`)
|
|
95
|
+
- Building the module (`npm run prepack`)
|
|
96
|
+
- Then, if your test suite went well, it will proceed to publish your module by:
|
|
97
|
+
- Bumping your module version and generating a changelog according to your Conventional Commits
|
|
98
|
+
- Publishing the module to npm (for that purpose, the module will be built again to ensure its updated version number is taken into account in the published artifact)
|
|
99
|
+
- Pushing a git tag representing the newly published version to your git remote origin
|
|
100
|
+
|
|
101
|
+
::tip
|
|
102
|
+
As with other scripts, feel free to fine-tune the default `release` script in your `package.json` to better suit your needs.
|
|
103
|
+
::
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Understand Module Structure"
|
|
3
|
+
description: "Learn how Nuxt modules are structured and how to define them."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
There are two types of Nuxt modules:
|
|
7
|
+
|
|
8
|
+
- published modules are distributed on npm - you can see a list of some community modules on [the Nuxt website](/modules).
|
|
9
|
+
- "local" modules exist within a Nuxt project, either [inlined in Nuxt config](/docs/4.x/api/nuxt-config#modules) or within [the `modules` directory](/docs/4.x/directory-structure/modules).
|
|
10
|
+
|
|
11
|
+
In either case, they work in the same way.
|
|
12
|
+
|
|
13
|
+
## Define Your Module
|
|
14
|
+
|
|
15
|
+
::note
|
|
16
|
+
When using the starter, your module definition is available at `src/module.ts`.
|
|
17
|
+
::
|
|
18
|
+
|
|
19
|
+
The module definition is the entry point of your module. It's what gets loaded by Nuxt when your module is referenced within a Nuxt configuration.
|
|
20
|
+
|
|
21
|
+
At a low level, a Nuxt module definition is a simple, potentially asynchronous, function accepting inline user options and a `nuxt` object to interact with Nuxt.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
export default function (inlineOptions, nuxt) {
|
|
25
|
+
// You can do whatever you like here..
|
|
26
|
+
console.log(inlineOptions.token) // `123`
|
|
27
|
+
console.log(nuxt.options.dev) // `true` or `false`
|
|
28
|
+
nuxt.hook('ready', (nuxt) => {
|
|
29
|
+
console.log('Nuxt is ready')
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
You can get type hinting for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/4.x/guide/going-further/kit).
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
38
|
+
|
|
39
|
+
export default defineNuxtModule((options, nuxt) => {
|
|
40
|
+
nuxt.hook('pages:extend', (pages) => {
|
|
41
|
+
console.log(`Discovered ${pages.length} pages`)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
However, **we do not recommend** using this low-level function definition. Instead, to define a module, **we recommend** using the object-syntax with `meta` property to identify your module, especially when publishing to npm.
|
|
47
|
+
|
|
48
|
+
This helper makes writing Nuxt modules more straightforward by implementing many common patterns needed by modules, guaranteeing future compatibility and improving the experience for both module authors and users.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
52
|
+
|
|
53
|
+
export default defineNuxtModule({
|
|
54
|
+
meta: {
|
|
55
|
+
// Usually the npm package name of your module
|
|
56
|
+
name: '@nuxtjs/example',
|
|
57
|
+
// The key in `nuxt.config` that holds your module options
|
|
58
|
+
configKey: 'sample',
|
|
59
|
+
// Compatibility constraints
|
|
60
|
+
compatibility: {
|
|
61
|
+
// Semver version of supported nuxt versions
|
|
62
|
+
nuxt: '>=3.0.0',
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
// Default configuration options for your module, can also be a function returning those
|
|
66
|
+
defaults: {},
|
|
67
|
+
// Shorthand sugar to register Nuxt hooks
|
|
68
|
+
hooks: {},
|
|
69
|
+
// Configuration for other modules - this does not ensure the module runs before
|
|
70
|
+
// your module, but it allows you to change the other module's configuration before it runs
|
|
71
|
+
moduleDependencies: {
|
|
72
|
+
'some-module': {
|
|
73
|
+
// You can specify a version constraint for the module. If the user has a different
|
|
74
|
+
// version installed, Nuxt will throw an error on startup.
|
|
75
|
+
version: '>=2',
|
|
76
|
+
// By default moduleDependencies will be added to the list of modules to be installed
|
|
77
|
+
// by Nuxt unless `optional` is set.
|
|
78
|
+
optional: true,
|
|
79
|
+
// Any configuration that should override `nuxt.options`.
|
|
80
|
+
overrides: {},
|
|
81
|
+
// Any configuration that should be set. It will override module defaults but
|
|
82
|
+
// will not override any configuration set in `nuxt.options`.
|
|
83
|
+
defaults: {},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
// The function holding your module logic, it can be asynchronous
|
|
87
|
+
setup (moduleOptions, nuxt) {
|
|
88
|
+
// ...
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`defineNuxtModule` returns a wrapper function with the lower level `(inlineOptions, nuxt)` module signature. This wrapper function applies defaults and other necessary steps before calling your `setup` function:
|
|
94
|
+
|
|
95
|
+
- Support `defaults` and `meta.configKey` for automatically merging module options
|
|
96
|
+
- Type hints and automated type inference
|
|
97
|
+
- Ensure module gets installed only once using a unique key computed from `meta.name` or `meta.configKey`
|
|
98
|
+
- Automatically register Nuxt hooks
|
|
99
|
+
- Automatically check for compatibility issues based on module meta
|
|
100
|
+
- Expose `getOptions` and `getMeta` for internal usage of Nuxt
|
|
101
|
+
- Ensuring backward and upward compatibility as long as the module is using `defineNuxtModule` from the latest version of `@nuxt/kit`
|
|
102
|
+
- Integration with module builder tooling
|
|
103
|
+
|
|
104
|
+
## Add Runtime Code
|
|
105
|
+
|
|
106
|
+
::note
|
|
107
|
+
When using the starter, the runtime directory is `src/runtime/`.
|
|
108
|
+
::
|
|
109
|
+
|
|
110
|
+
Modules, like everything in a Nuxt configuration, aren't included in your application runtime. However, you might want your module to provide, or inject runtime code to the application it's installed on. That's what the runtime directory enables you to do.
|
|
111
|
+
|
|
112
|
+
Inside the runtime directory, you can provide any kind of assets related to the Nuxt app:
|
|
113
|
+
- Vue components
|
|
114
|
+
- Composables
|
|
115
|
+
- [Nuxt plugins](/docs/4.x/directory-structure/app/plugins)
|
|
116
|
+
|
|
117
|
+
To the [server engine](/docs/4.x/guide/concepts/server-engine), Nitro:
|
|
118
|
+
- API routes
|
|
119
|
+
- Middlewares
|
|
120
|
+
- Nitro plugins
|
|
121
|
+
|
|
122
|
+
Or any other kind of asset you want to inject in users' Nuxt applications:
|
|
123
|
+
- Stylesheets
|
|
124
|
+
- 3D models
|
|
125
|
+
- Images
|
|
126
|
+
- etc.
|
|
127
|
+
|
|
128
|
+
You'll then be able to inject all those assets inside the application from your [module definition](#define-your-module).
|
|
129
|
+
|
|
130
|
+
::tip
|
|
131
|
+
Learn more about asset injection in [the recipes section](/docs/4.x/guide/modules/recipes-basics).
|
|
132
|
+
::
|
|
133
|
+
|
|
134
|
+
::warning
|
|
135
|
+
Published modules cannot leverage auto-imports for assets within their runtime directory. Instead, they have to import them explicitly from `#imports` or alike.
|
|
136
|
+
:br :br
|
|
137
|
+
Auto-imports are not enabled for files within `node_modules` (the location where a published module will eventually live) for performance reasons.
|
|
138
|
+
::
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Add Plugins, Components & More"
|
|
3
|
+
description: "Learn how to inject plugins, components, composables and server routes from your module."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Here are some common patterns used by module authors.
|
|
7
|
+
|
|
8
|
+
## Modify Nuxt Configuration
|
|
9
|
+
|
|
10
|
+
Nuxt configuration can be read and altered by modules. Here's an example of a module enabling an experimental feature.
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
14
|
+
|
|
15
|
+
export default defineNuxtModule({
|
|
16
|
+
setup (options, nuxt) {
|
|
17
|
+
// We create the `experimental` object if it doesn't exist yet
|
|
18
|
+
nuxt.options.experimental ||= {}
|
|
19
|
+
nuxt.options.experimental.componentIslands = true
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
When you need to handle more complex configuration alterations, you should consider using [defu](https://github.com/unjs/defu).
|
|
25
|
+
|
|
26
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/extending-and-altering-nuxt-configuration-and-options?friend=nuxt" target="_blank"}
|
|
27
|
+
Watch Vue School video about altering Nuxt configuration.
|
|
28
|
+
::
|
|
29
|
+
|
|
30
|
+
## Expose Options to Runtime
|
|
31
|
+
|
|
32
|
+
Because modules aren't part of the application runtime, their options aren't either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt's [`runtimeConfig`](/docs/4.x/api/nuxt-config#runtimeconfig).
|
|
33
|
+
|
|
34
|
+
<!-- TODO: Update after #18466 (or equivalent) -->
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
38
|
+
import { defu } from 'defu'
|
|
39
|
+
|
|
40
|
+
export default defineNuxtModule({
|
|
41
|
+
setup (options, nuxt) {
|
|
42
|
+
nuxt.options.runtimeConfig.public.myModule = defu(nuxt.options.runtimeConfig.public.myModule, {
|
|
43
|
+
foo: options.foo,
|
|
44
|
+
})
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Note that we use [`defu`](https://github.com/unjs/defu) to extend the public runtime configuration the user provides instead of overwriting it.
|
|
50
|
+
|
|
51
|
+
You can then access your module options in a plugin, component, the application like any other runtime configuration:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { useRuntimeConfig } from '@nuxt/kit'
|
|
55
|
+
|
|
56
|
+
const options = useRuntimeConfig().public.myModule
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
::warning
|
|
60
|
+
Be careful not to expose any sensitive module configuration on the public runtime config, such as private API keys, as they will end up in the public bundle.
|
|
61
|
+
::
|
|
62
|
+
|
|
63
|
+
:read-more{to="/docs/4.x/guide/going-further/runtime-config"}
|
|
64
|
+
|
|
65
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/passing-and-exposing-module-options?friend=nuxt" target="_blank"}
|
|
66
|
+
Watch Vue School video about passing and exposing Nuxt module options.
|
|
67
|
+
::
|
|
68
|
+
|
|
69
|
+
## Add Plugins
|
|
70
|
+
|
|
71
|
+
Plugins are a common way for a module to add runtime logic. You can use the `addPlugin` utility to register them from your module.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
75
|
+
|
|
76
|
+
export default defineNuxtModule({
|
|
77
|
+
setup (options, nuxt) {
|
|
78
|
+
// Create resolver to resolve relative paths
|
|
79
|
+
const resolver = createResolver(import.meta.url)
|
|
80
|
+
|
|
81
|
+
addPlugin(resolver.resolve('./runtime/plugin'))
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
:read-more{to="/docs/4.x/guide/going-further/kit"}
|
|
87
|
+
|
|
88
|
+
## Add Components
|
|
89
|
+
|
|
90
|
+
If your module should provide Vue components, you can use the `addComponent` utility to add them as auto-imports for Nuxt to resolve.
|
|
91
|
+
|
|
92
|
+
```ts twoslash
|
|
93
|
+
import { addComponent, createResolver, defineNuxtModule, useRuntimeConfig } from '@nuxt/kit'
|
|
94
|
+
|
|
95
|
+
export default defineNuxtModule({
|
|
96
|
+
setup (options, nuxt) {
|
|
97
|
+
const resolver = createResolver(import.meta.url)
|
|
98
|
+
|
|
99
|
+
// From the runtime directory
|
|
100
|
+
addComponent({
|
|
101
|
+
name: 'MySuperComponent', // name of the component to be used in vue templates
|
|
102
|
+
export: 'MySuperComponent', // (optional) if the component is a named (rather than default) export
|
|
103
|
+
filePath: resolver.resolve('runtime/components/MySuperComponent.vue'),
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// From a library
|
|
107
|
+
addComponent({
|
|
108
|
+
name: 'MyAwesomeComponent', // name of the component to be used in vue templates
|
|
109
|
+
export: 'MyAwesomeComponent', // (optional) if the component is a named (rather than default) export
|
|
110
|
+
filePath: '@vue/awesome-components',
|
|
111
|
+
})
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Alternatively, you can add an entire directory by using `addComponentsDir`.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { addComponentsDir, defineNuxtModule } from '@nuxt/kit'
|
|
120
|
+
|
|
121
|
+
export default defineNuxtModule({
|
|
122
|
+
setup (options, nuxt) {
|
|
123
|
+
const resolver = createResolver(import.meta.url)
|
|
124
|
+
|
|
125
|
+
addComponentsDir({
|
|
126
|
+
path: resolver.resolve('runtime/components'),
|
|
127
|
+
})
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
::tip{icon="i-lucide-lightbulb"}
|
|
133
|
+
It is highly recommended to prefix your exports to avoid conflicts with user code or other modules.
|
|
134
|
+
|
|
135
|
+
:read-more{to="/docs/4.x/guide/modules/best-practices#prefix-your-exports"}
|
|
136
|
+
::
|
|
137
|
+
|
|
138
|
+
## Add Composables
|
|
139
|
+
|
|
140
|
+
If your module should provide composables, you can use the `addImports` utility to add them as auto-imports for Nuxt to resolve.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { addImports, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
144
|
+
|
|
145
|
+
export default defineNuxtModule({
|
|
146
|
+
setup (options, nuxt) {
|
|
147
|
+
const resolver = createResolver(import.meta.url)
|
|
148
|
+
|
|
149
|
+
addImports({
|
|
150
|
+
name: 'useComposable', // name of the composable to be used
|
|
151
|
+
as: 'useComposable',
|
|
152
|
+
from: resolver.resolve('runtime/composables/useComposable'), // path of composable
|
|
153
|
+
})
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Alternatively, you can add an entire directory by using `addImportsDir`.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { addImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
162
|
+
|
|
163
|
+
export default defineNuxtModule({
|
|
164
|
+
setup (options, nuxt) {
|
|
165
|
+
const resolver = createResolver(import.meta.url)
|
|
166
|
+
|
|
167
|
+
addImportsDir(resolver.resolve('runtime/composables'))
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
::tip{icon="i-lucide-lightbulb"}
|
|
173
|
+
It is highly recommended to prefix your exports to avoid conflicts with user code or other modules.
|
|
174
|
+
|
|
175
|
+
:read-more{to="/docs/4.x/guide/modules/best-practices#prefix-your-exports"}
|
|
176
|
+
::
|
|
177
|
+
|
|
178
|
+
## Add Server Routes
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
182
|
+
|
|
183
|
+
export default defineNuxtModule({
|
|
184
|
+
setup (options, nuxt) {
|
|
185
|
+
const resolver = createResolver(import.meta.url)
|
|
186
|
+
|
|
187
|
+
addServerHandler({
|
|
188
|
+
route: '/api/_my-module/hello',
|
|
189
|
+
handler: resolver.resolve('./runtime/server/api/hello/index.get'),
|
|
190
|
+
})
|
|
191
|
+
},
|
|
192
|
+
})
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
You can also add a dynamic server route:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
199
|
+
|
|
200
|
+
export default defineNuxtModule({
|
|
201
|
+
setup (options, nuxt) {
|
|
202
|
+
const resolver = createResolver(import.meta.url)
|
|
203
|
+
|
|
204
|
+
addServerHandler({
|
|
205
|
+
route: '/api/_my-module/hello/:name',
|
|
206
|
+
handler: resolver.resolve('./runtime/server/api/hello/[name].get'),
|
|
207
|
+
})
|
|
208
|
+
},
|
|
209
|
+
})
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
::tip{icon="i-lucide-lightbulb"}
|
|
213
|
+
It is highly recommended to prefix your server routes to avoid conflicts with user-defined routes. Common paths like `/api/auth`, `/api/login`, or `/api/user` may already be used by the application.
|
|
214
|
+
|
|
215
|
+
:read-more{to="/docs/4.x/guide/modules/best-practices#prefix-your-exports"}
|
|
216
|
+
::
|
|
217
|
+
|
|
218
|
+
## Add Other Assets
|
|
219
|
+
|
|
220
|
+
If your module should provide other kinds of assets, they can also be injected. Here's a simple example module injecting a stylesheet through Nuxt's `css` array.
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
224
|
+
|
|
225
|
+
export default defineNuxtModule({
|
|
226
|
+
setup (options, nuxt) {
|
|
227
|
+
const resolver = createResolver(import.meta.url)
|
|
228
|
+
|
|
229
|
+
nuxt.options.css.push(resolver.resolve('./runtime/style.css'))
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
And a more advanced one, exposing a folder of assets through [Nitro](/docs/4.x/guide/concepts/server-engine)'s `publicAssets` option:
|
|
235
|
+
|
|
236
|
+
```js
|
|
237
|
+
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
238
|
+
|
|
239
|
+
export default defineNuxtModule({
|
|
240
|
+
setup (options, nuxt) {
|
|
241
|
+
const resolver = createResolver(import.meta.url)
|
|
242
|
+
|
|
243
|
+
nuxt.hook('nitro:config', (nitroConfig) => {
|
|
244
|
+
nitroConfig.publicAssets ||= []
|
|
245
|
+
nitroConfig.publicAssets.push({
|
|
246
|
+
dir: resolver.resolve('./runtime/public'),
|
|
247
|
+
maxAge: 60 * 60 * 24 * 365, // 1 year
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
},
|
|
251
|
+
})
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Use Other Modules
|
|
255
|
+
|
|
256
|
+
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:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
260
|
+
|
|
261
|
+
const resolver = createResolver(import.meta.url)
|
|
262
|
+
|
|
263
|
+
export default defineNuxtModule<ModuleOptions>({
|
|
264
|
+
meta: {
|
|
265
|
+
name: 'my-module',
|
|
266
|
+
},
|
|
267
|
+
moduleDependencies: {
|
|
268
|
+
'@nuxtjs/tailwindcss': {
|
|
269
|
+
// You can specify a version constraint for the module
|
|
270
|
+
version: '>=6',
|
|
271
|
+
// Any configuration that should override `nuxt.options`
|
|
272
|
+
overrides: {
|
|
273
|
+
exposeConfig: true,
|
|
274
|
+
},
|
|
275
|
+
// Any configuration that should be set. It will override module defaults but
|
|
276
|
+
// will not override any configuration set in `nuxt.options`
|
|
277
|
+
defaults: {
|
|
278
|
+
config: {
|
|
279
|
+
darkMode: 'class',
|
|
280
|
+
content: {
|
|
281
|
+
files: [
|
|
282
|
+
resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
|
|
283
|
+
resolver.resolve('./runtime/*.{mjs,js,ts}'),
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
setup (options, nuxt) {
|
|
291
|
+
// We can inject our CSS file which includes Tailwind's directives
|
|
292
|
+
nuxt.options.css.push(resolver.resolve('./runtime/assets/styles.css'))
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
::callout{type="info"}
|
|
298
|
+
The `moduleDependencies` option replaces the deprecated `installModule` function and ensures proper setup order and configuration merging.
|
|
299
|
+
::
|