mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
docs: basic kit info (#206)
This commit is contained in:
parent
a3fbc3954f
commit
cdca16fa8c
@ -1 +1,8 @@
|
|||||||
# Nuxt Modules
|
# Nuxt Modules
|
||||||
|
|
||||||
|
Nuxt modules allow encapsulating features and detailed customization of how Nuxt works. You can use them to add features simply within your own project, or publish a library for others to use.
|
||||||
|
|
||||||
|
You can browse through a list of community-provided Nuxt modules at [modules.nuxtjs.org](https://modules.nuxtjs.org/), or via [GitHub discovery](https://github.com/topics/nuxt-module)
|
||||||
|
|
||||||
|
**Note**:
|
||||||
|
Not all current Nuxt modules are compatible with Nuxt3, in particular modules that have a runtime component. Module authors should consult [the documentation on creating compatible modules](/modules/kit), and please feel free to file an issue on the module repository to track progress.
|
||||||
|
@ -1 +1,83 @@
|
|||||||
# Kit (advanced)
|
# Creating Modules
|
||||||
|
|
||||||
|
Nuxt provides helper functions (accessed from `@nuxt/kit`) to assist in creating modules that can run on both Nuxt 2 and Nuxt 3.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { defineNuxtModule } from '@nuxt/kit'
|
||||||
|
|
||||||
|
export default defineNuxtModule({
|
||||||
|
// The npm package name of your module
|
||||||
|
name: '@nuxtjs/sample-module',
|
||||||
|
// The key in `nuxt.config` that holds your module options
|
||||||
|
configKey: 'sample',
|
||||||
|
// Default configuration options for your module
|
||||||
|
defaults: {},
|
||||||
|
hooks: {},
|
||||||
|
setup (options, nuxt) {},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Module options
|
||||||
|
|
||||||
|
Nuxt will automatically merge the configuration provided to your module (whether passed directly inline or in a configuration section in `nuxt.config`) with the defaults you provide.
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
buildModules: [
|
||||||
|
['@nuxtjs/sample-module', { sampleOption: true }]
|
||||||
|
],
|
||||||
|
sample: {
|
||||||
|
anotherOption: 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Hooks
|
||||||
|
|
||||||
|
For simple modules, you may be able to implement all you need simply with several hooks in this section.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
If the `hooks` configuration is not enough, you can provide a full setup function, with access to `nuxt`. You will no longer have access to `this` as in the previous Nuxt module specification, but there are dedicated helper functions from `@nuxt/kit` to replace the module container methods from previously.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
|
||||||
|
|
||||||
|
export default defineNuxtModule({
|
||||||
|
// ...
|
||||||
|
setup (options, nuxt) {
|
||||||
|
addPlugin({
|
||||||
|
src: path.resolve(__dirname, 'templates/foo.js')
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Advanced usage
|
||||||
|
|
||||||
|
In some cases you may need access to the Nuxt instance in order to define your module, for example, using other Nuxt options:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { defineNuxtModule } from '@nuxt/kit'
|
||||||
|
|
||||||
|
export default defineNuxtModule(nuxt => ({
|
||||||
|
// ...
|
||||||
|
defaults: {
|
||||||
|
root: nuxt.options.rootDir
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Module helpers
|
||||||
|
|
||||||
|
A number of helpers are also provided for use within this context (with more coming - feature requests welcome) that ensure compatible behavior across Nuxt versions.
|
||||||
|
|
||||||
|
* addTemplate
|
||||||
|
* addErrorLayout
|
||||||
|
* addLayout
|
||||||
|
* addPlugin
|
||||||
|
* addServerMiddleware
|
||||||
|
* extendBuild
|
||||||
|
* extendRoutes
|
||||||
|
|
||||||
|
Each of these functions provides documentation via IDE hover/autocomplete.
|
||||||
|
Loading…
Reference in New Issue
Block a user