@nuxt/docs-nightly 4.1.0-29279319.00ede5ce → 4.1.0-29279382.7234ae61
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/3.api/5.kit/16.layers.md +220 -0
- package/package.json +1 -1
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Layers"
|
|
3
|
+
description: Nuxt Kit provides utilities to help you work with layers and their directory structures.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/kit/src/layers.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
Nuxt layers provide a powerful way to share and extend functionality across projects. When working with layers in modules, you often need to access directory paths from each layer. Nuxt Kit provides the `getLayerDirectories` utility to access resolved directory paths for all layers in your Nuxt application.
|
|
12
|
+
|
|
13
|
+
## `getLayerDirectories`
|
|
14
|
+
|
|
15
|
+
Get the resolved directory paths for all layers in a Nuxt application. This function provides a structured way to access layer directories without directly accessing the private `nuxt.options._layers` property.
|
|
16
|
+
|
|
17
|
+
### Usage
|
|
18
|
+
|
|
19
|
+
```ts twoslash
|
|
20
|
+
import { defineNuxtModule, getLayerDirectories } from '@nuxt/kit'
|
|
21
|
+
|
|
22
|
+
export default defineNuxtModule({
|
|
23
|
+
setup() {
|
|
24
|
+
const layerDirs = getLayerDirectories()
|
|
25
|
+
|
|
26
|
+
// Access directories from all layers
|
|
27
|
+
for (const [index, layer] of layerDirs.entries()) {
|
|
28
|
+
console.log(`Layer ${index}:`)
|
|
29
|
+
console.log(` Root: ${layer.root}`)
|
|
30
|
+
console.log(` App: ${layer.app}`)
|
|
31
|
+
console.log(` Server: ${layer.server}`)
|
|
32
|
+
console.log(` Pages: ${layer.appPages}`)
|
|
33
|
+
// ... other directories
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Type
|
|
40
|
+
|
|
41
|
+
```ts twoslash
|
|
42
|
+
// @errors: 2391
|
|
43
|
+
import type { Nuxt } from '@nuxt/schema'
|
|
44
|
+
// ---cut---
|
|
45
|
+
function getLayerDirectories(nuxt?: Nuxt): LayerDirectories[]
|
|
46
|
+
|
|
47
|
+
interface LayerDirectories {
|
|
48
|
+
/** Nuxt rootDir (`/` by default) */
|
|
49
|
+
readonly root: string
|
|
50
|
+
/** Nitro source directory (`/server` by default) */
|
|
51
|
+
readonly server: string
|
|
52
|
+
/** Local modules directory (`/modules` by default) */
|
|
53
|
+
readonly modules: string
|
|
54
|
+
/** Shared directory (`/shared` by default) */
|
|
55
|
+
readonly shared: string
|
|
56
|
+
/** Public directory (`/public` by default) */
|
|
57
|
+
readonly public: string
|
|
58
|
+
/** Nuxt srcDir (`/app/` by default) */
|
|
59
|
+
readonly app: string
|
|
60
|
+
/** Layouts directory (`/app/layouts` by default) */
|
|
61
|
+
readonly appLayouts: string
|
|
62
|
+
/** Middleware directory (`/app/middleware` by default) */
|
|
63
|
+
readonly appMiddleware: string
|
|
64
|
+
/** Pages directory (`/app/pages` by default) */
|
|
65
|
+
readonly appPages: string
|
|
66
|
+
/** Plugins directory (`/app/plugins` by default) */
|
|
67
|
+
readonly appPlugins: string
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Parameters
|
|
72
|
+
|
|
73
|
+
**`nuxt`** (optional): The Nuxt instance to get layers from. If not provided, the function will use the current Nuxt context.
|
|
74
|
+
|
|
75
|
+
### Return Value
|
|
76
|
+
|
|
77
|
+
The `getLayerDirectories` function returns an array of `LayerDirectories` objects, one for each layer in the application.
|
|
78
|
+
|
|
79
|
+
**Layer Priority Ordering**: The layers are ordered by priority, where:
|
|
80
|
+
- The **first layer** is the user/project layer (highest priority)
|
|
81
|
+
- **Earlier layers override later layers** in the array
|
|
82
|
+
- **Base layers appear last** in the array (lowest priority)
|
|
83
|
+
|
|
84
|
+
This ordering matches Nuxt's layer resolution system, where user-defined configurations and files take precedence over those from base layers.
|
|
85
|
+
|
|
86
|
+
**`LayerDirectories`**: An object containing the resolved directory paths for a layer.
|
|
87
|
+
|
|
88
|
+
| Property | Type | Description |
|
|
89
|
+
| --------------- | -------- | ------------------------------------------------------------------------------ |
|
|
90
|
+
| `root` | `string` | The root directory of the layer (equivalent to `rootDir`) |
|
|
91
|
+
| `server` | `string` | The server directory for Nitro server-side code |
|
|
92
|
+
| `modules` | `string` | The local modules directory |
|
|
93
|
+
| `shared` | `string` | The shared directory for code used by both client and server |
|
|
94
|
+
| `app` | `string` | The source directory of the layer (equivalent to `srcDir`) |
|
|
95
|
+
| `public` | `string` | The public directory for static assets |
|
|
96
|
+
| `appLayouts` | `string` | The layouts directory for Vue layout components |
|
|
97
|
+
| `appMiddleware` | `string` | The middleware directory for route middleware |
|
|
98
|
+
| `appPages` | `string` | The pages directory for file-based routing |
|
|
99
|
+
| `appPlugins` | `string` | The plugins directory for Nuxt plugins |
|
|
100
|
+
|
|
101
|
+
### Examples
|
|
102
|
+
|
|
103
|
+
**Processing files from all layers:**
|
|
104
|
+
|
|
105
|
+
```ts twoslash
|
|
106
|
+
// @errors: 2307
|
|
107
|
+
// ---cut---
|
|
108
|
+
import { defineNuxtModule, getLayerDirectories } from '@nuxt/kit'
|
|
109
|
+
import { resolve } from 'pathe'
|
|
110
|
+
import { globby } from 'globby'
|
|
111
|
+
|
|
112
|
+
export default defineNuxtModule({
|
|
113
|
+
async setup() {
|
|
114
|
+
const layerDirs = getLayerDirectories()
|
|
115
|
+
|
|
116
|
+
// Find all component files across layers
|
|
117
|
+
// Note: layerDirs[0] is the user layer (highest priority)
|
|
118
|
+
// Later layers in the array have lower priority
|
|
119
|
+
const componentFiles = []
|
|
120
|
+
for (const [index, layer] of layerDirs.entries()) {
|
|
121
|
+
const files = await globby('**/*.vue', {
|
|
122
|
+
cwd: resolve(layer.app, 'components'),
|
|
123
|
+
absolute: true
|
|
124
|
+
})
|
|
125
|
+
console.log(`Layer ${index} (${index === 0 ? 'user' : 'base'}):`, files.length, 'components')
|
|
126
|
+
componentFiles.push(...files)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**Adding templates from multiple layers:**
|
|
133
|
+
|
|
134
|
+
```ts twoslash
|
|
135
|
+
import { defineNuxtModule, getLayerDirectories, addTemplate } from '@nuxt/kit'
|
|
136
|
+
import { resolve, basename } from 'pathe'
|
|
137
|
+
import { existsSync } from 'fs'
|
|
138
|
+
|
|
139
|
+
export default defineNuxtModule({
|
|
140
|
+
async setup() {
|
|
141
|
+
const layerDirs = getLayerDirectories()
|
|
142
|
+
|
|
143
|
+
// Add a config file from each layer that has one
|
|
144
|
+
for (const dirs of layerDirs) {
|
|
145
|
+
const configPath = resolve(dirs.app, 'my-module.config.ts')
|
|
146
|
+
if (existsSync(configPath)) {
|
|
147
|
+
addTemplate({
|
|
148
|
+
filename: `my-module-${basename(dirs.root)}.config.ts`,
|
|
149
|
+
src: configPath
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Respecting layer priority:**
|
|
158
|
+
|
|
159
|
+
```ts twoslash
|
|
160
|
+
import { defineNuxtModule, getLayerDirectories } from '@nuxt/kit'
|
|
161
|
+
import { resolve } from 'pathe'
|
|
162
|
+
import { existsSync, readFileSync } from 'fs'
|
|
163
|
+
|
|
164
|
+
export default defineNuxtModule({
|
|
165
|
+
setup() {
|
|
166
|
+
const layerDirs = getLayerDirectories()
|
|
167
|
+
|
|
168
|
+
// Find the first (highest priority) layer that has a specific config file
|
|
169
|
+
// This respects the layer priority system
|
|
170
|
+
let configContent = null
|
|
171
|
+
for (const dirs of layerDirs) {
|
|
172
|
+
const configPath = resolve(dirs.app, 'my-config.json')
|
|
173
|
+
if (existsSync(configPath)) {
|
|
174
|
+
configContent = readFileSync(configPath, 'utf-8')
|
|
175
|
+
console.log(`Using config from layer: ${dirs.root}`)
|
|
176
|
+
break // Use the first (highest priority) config found
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Alternative: Collect configs from all layers, with user layer taking precedence
|
|
181
|
+
const allConfigs = {}
|
|
182
|
+
for (const dirs of layerDirs.reverse()) { // Process from lowest to highest priority
|
|
183
|
+
const configPath = resolve(dirs.app, 'my-config.json')
|
|
184
|
+
if (existsSync(configPath)) {
|
|
185
|
+
const config = JSON.parse(readFileSync(configPath, 'utf-8'))
|
|
186
|
+
Object.assign(allConfigs, config) // Later assignments override earlier ones
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**Checking for layer-specific directories:**
|
|
194
|
+
|
|
195
|
+
```ts twoslash
|
|
196
|
+
import { defineNuxtModule, getLayerDirectories } from '@nuxt/kit'
|
|
197
|
+
import { existsSync } from 'fs'
|
|
198
|
+
import { resolve } from 'pathe'
|
|
199
|
+
|
|
200
|
+
export default defineNuxtModule({
|
|
201
|
+
setup() {
|
|
202
|
+
const layerDirs = getLayerDirectories()
|
|
203
|
+
|
|
204
|
+
// Find layers that have a specific custom directory
|
|
205
|
+
const layersWithAssets = layerDirs.filter(layer => {
|
|
206
|
+
return existsSync(resolve(layer.app, 'assets'))
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
console.log(`Found ${layersWithAssets.length} layers with assets directory`)
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
::note
|
|
215
|
+
The `getLayerDirectories` function includes caching via a WeakMap to avoid recomputing directory paths for the same layers repeatedly, improving performance when called multiple times.
|
|
216
|
+
::
|
|
217
|
+
|
|
218
|
+
::note
|
|
219
|
+
Directory paths returned by this function always include a trailing slash for consistency.
|
|
220
|
+
::
|