mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-20 07:30:57 +00:00
docs: add nuxt layers introduction and authoring guide (#9405)
Co-authored-by: Alexander Lichter <github@lichter.io> Co-authored-by: Sébastien Chopin <seb@nuxtjs.com> Co-authored-by: Pooya Parsa <pooya@pi0.io> Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
parent
4c4249dc33
commit
77e2974f1c
38
docs/content/1.docs/1.getting-started/9.layers.md
Normal file
38
docs/content/1.docs/1.getting-started/9.layers.md
Normal file
@ -0,0 +1,38 @@
|
||||
---
|
||||
navigation.icon: uil:layer-group
|
||||
description: Nuxt provides a powerful system that allows you to extend the default files, configs, and much more.
|
||||
---
|
||||
|
||||
# Layers
|
||||
|
||||
One of the core features of Nuxt 3 is the layers and extending support. You can extend a default Nuxt application to reuse components, utils, and configuration. The layers structure is almost identical to a standard Nuxt application which makes them easy to author and maintain. Some example use cases:
|
||||
|
||||
::list{type="success"}
|
||||
- Share reusable configuration presets across projects using `nuxt.config` and `app.config`
|
||||
- Create a component library using `components/` directory
|
||||
- Create utility and composable library using `composables/` and `utils/` directories
|
||||
- Create Nuxt themes
|
||||
- Create Nuxt module presets
|
||||
- Share standard setup across projects
|
||||
::
|
||||
|
||||
You can extend a layer by adding the [extends](/docs/api/configuration/nuxt-config#extends) property to the `nuxt.config.ts` file.
|
||||
|
||||
```ts{}[app/nuxt.config.ts]
|
||||
export default defineNuxtConfig({
|
||||
extends: [
|
||||
'../base', // Extend from a local layer
|
||||
'@my-themes/awesome', // Extend from an installed npm package
|
||||
'github:my-themes/awesome#v1', // Extend from a git repository
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
## Authoring Nuxt Layers
|
||||
|
||||
See [Layer Author Guide](/docs/guide/going-further/layers) to learn more.
|
||||
|
||||
## Examples
|
||||
|
||||
- [Nuxt Docus Theme](https://github.com/nuxt-themes/docus#readme)
|
||||
- [Nuxt Content Wind Theme](https://github.com/Atinux/content-wind#readme)
|
194
docs/content/1.docs/2.guide/3.going-further/7.layers.md
Normal file
194
docs/content/1.docs/2.guide/3.going-further/7.layers.md
Normal file
@ -0,0 +1,194 @@
|
||||
---
|
||||
description: Nuxt provides a powerful system that allows you to extend the default files, configs, and much more.
|
||||
---
|
||||
|
||||
# Authoring Nuxt Layers
|
||||
|
||||
Nuxt layers are a powerful feature that you can use to share and reuse partial Nuxt applications within a monorepo, or from a git repository or npm package. The layers structure is almost identical to a standard Nuxt application, which makes them easy to author and maintain. ([Read More](/docs/getting-started/layers))
|
||||
|
||||
A minimal Nuxt layer directory should contain a `nuxt.config.ts` file to indicate it is a layer.
|
||||
|
||||
```ts{}[base/nuxt.config.ts]
|
||||
export default defineNuxtConfig({})
|
||||
```
|
||||
|
||||
Additionally, certain other files in the layer directory will be auto-scanned and used by Nuxt for the project extending this layer.
|
||||
|
||||
- `components/*` - Extend the default components
|
||||
- `composables/*` - Extend the default composables
|
||||
- `pages/*` - Extend the default pages
|
||||
- `server/*` - Extend the default server endpoints & middleware
|
||||
- `nuxt.config.ts` - Extend the default nuxt config
|
||||
- `app.config.ts` - Extend the default app config
|
||||
|
||||
## Basic Example
|
||||
|
||||
::code-group
|
||||
|
||||
```ts{}[app/nuxt.config.ts]
|
||||
export default defineNuxtConfig({
|
||||
extends: [
|
||||
'../base'
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
```vue{}[app/app.vue]
|
||||
<template>
|
||||
<BaseComponent/>
|
||||
</template>
|
||||
```
|
||||
|
||||
```ts{}[base/nuxt.config.ts]
|
||||
export default defineNuxtConfig({
|
||||
// Extending from base nuxt.config.ts!
|
||||
app: {
|
||||
head: {
|
||||
title: 'Extending Configs is Fun!',
|
||||
meta: [
|
||||
{ name: 'description', content: 'I am using the extends feature in nuxt 3!' }
|
||||
],
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
```vue{}[base/components/BaseComponent.vue]
|
||||
<template>
|
||||
<h1>Extending Components is Fun!</h1>
|
||||
</template>
|
||||
```
|
||||
|
||||
::
|
||||
|
||||
## Using Starter Templates
|
||||
|
||||
To get started you can initialize a layer with the [nuxt/starter/layer template](https://github.com/nuxt/starter/tree/layer). This will create a basic structure you can build upon. Execute this command within the terminal to get started:
|
||||
|
||||
```bash
|
||||
npx nuxi init --template layer nuxt-layer
|
||||
```
|
||||
|
||||
Follow up on the README instructions for the next steps.
|
||||
|
||||
::alert
|
||||
Check [nuxt-themes/starter](https://github.com/nuxt-themes/starter) for a more opinionated starter for authoring Nuxt themes. It can be initialized with:
|
||||
|
||||
```bash
|
||||
npx nuxi init --template gh:nuxt-themes/starter my-theme
|
||||
```
|
||||
|
||||
::
|
||||
|
||||
## Publishing layers
|
||||
|
||||
You can publish and share layers by either using a remote source or an npm package.
|
||||
|
||||
### Publishing via Git Repository
|
||||
|
||||
You can use a git repository to share your Nuxt layer. Some examples:
|
||||
|
||||
```ts{}[nuxt.config.ts]
|
||||
export default defineNuxtConfig({
|
||||
extends: [
|
||||
'github:username/repoName', // GitHub Remote Source
|
||||
'github:username/repoName/base', // GitHub Remote Source within /base directory
|
||||
'github:username/repoName#dev', // GitHub Remote Source from dev branch
|
||||
'github:username/repoName#v1.0.0', // GitHub Remote Source from v1.0.0 tag
|
||||
'gitlab:username/repoName', // GitLab Remote Source example
|
||||
'bitbucket:username/repoName', // Bitbucket Remote Source example
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
::alert{type="info"}
|
||||
If you want to extend a private remote source, you need to add the environment variable `GIGET_AUTH=<token>` to provide a token.
|
||||
::
|
||||
|
||||
::alert{type="warning"}
|
||||
Currently, with git remote sources, if a layer has npm dependencies, you will need to manually install them in the target project. We are working on this to auto-install layer dependencies with git sources.
|
||||
::
|
||||
|
||||
### Publishing via npm package
|
||||
|
||||
You can publish Nuxt layers as an npm package that contains the files and dependencies you want to extend. This allows you to share your config with others, use it in multiple projects or use it privately.
|
||||
|
||||
To extend from an npm package, you need to make sure that the module is published to npm and installed in the user's project as a devDependency. Then you can use the module name to extend the current nuxt config:
|
||||
|
||||
```ts{}[nuxt.config.ts]
|
||||
export default defineNuxtConfig({
|
||||
extends: [
|
||||
// Node Module with scope
|
||||
'@scope/moduleName',
|
||||
// or just the module name
|
||||
'moduleName'
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
To publish a layer directory as an npm package, you want to make sure that the `package.json` has the correct properties filled out. This will make sure that the files are included when the package is published.
|
||||
|
||||
```json{}[package.json]
|
||||
{
|
||||
"name": "my-theme",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "./nuxt.config.ts",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"nuxt": "^3.0.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
::alert{type="info"}
|
||||
Make sure any dependency imported in the layer is **explicitly added** to the `dependencies`. The `nuxt` dependency, and anything only used for testing the layer before publishing, should remain in the `devDependencies` field.
|
||||
::
|
||||
|
||||
Now you can proceed to publish the module to npm, either publicly or privately.
|
||||
|
||||
::alert{type="warning"}
|
||||
When publishing the layer as a private npm package, you need to make sure you log in, to authenticate with npm to download the node module.
|
||||
::
|
||||
|
||||
## Tips for Authoring Layers
|
||||
|
||||
### Relative Paths and Aliases
|
||||
|
||||
When importing using aliases (such as `~/` and `@/`) in a layer components and composables, note that aliases are resolved relative to the user's project paths. As a workaround, you can **use relative paths** to import them. We are working on a better solution for named layer aliases.
|
||||
|
||||
Also when using relative paths in `nuxt.config` file of a layer, (with exception of nested `extends`) they are resolved relative to user's project instead of the layer. As a workaround, use full resolved paths in `nuxt.config`:
|
||||
|
||||
```js
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
css: [fileURLToPath(new URL('./assets/main.css', import.meta.url))]
|
||||
})
|
||||
```
|
||||
|
||||
## Multi-Layer Support for Nuxt Modules
|
||||
|
||||
You can use the internal array `nuxt.options._layers` to support custom multi-layer handling for your modules. Example:
|
||||
|
||||
```js
|
||||
export default defineNuxtModule({
|
||||
setup(_options, nuxt) {
|
||||
for (const layer of nuxt.options._layers) {
|
||||
// You can check for a custom directory existence to extend for each layer
|
||||
console.log('Custom extension for', layer.cwd, layer.config)
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Notes:**
|
||||
|
||||
- Earlier items in the `_layers` array have higher priority and override later ones
|
||||
- The user's project is the first item in the `_layers` array
|
||||
|
||||
## Going Deeper
|
||||
|
||||
Loading configration and and extend support is handled by [unjs/c12](https://github.com/unjs/c12), merged using [unjs/defu](https://github.com/unjs/defu) and remote git sources are supported using [unjs/giget](https://github.com/unjs/giget). Check the docs and source code to learn more.
|
||||
|
||||
We are working to bring more improvements for layers support. Please refer to [nuxt/framework#3222](https://github.com/nuxt/framework/issues/3222).
|
@ -31,7 +31,7 @@ Then, reinstall your dependencies:
|
||||
yarn install
|
||||
```
|
||||
|
||||
```bash [NPM]
|
||||
```bash [npm]
|
||||
npm install
|
||||
```
|
||||
|
||||
@ -51,7 +51,7 @@ Install `@nuxt/bridge-edge` as a development dependency:
|
||||
yarn add --dev @nuxt/bridge@npm:@nuxt/bridge-edge
|
||||
```
|
||||
|
||||
```bash [NPM]
|
||||
```bash [npm]
|
||||
npm install -D @nuxt/bridge@npm:@nuxt/bridge-edge
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user