-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathlayers.ts
More file actions
124 lines (114 loc) · 4.43 KB
/
layers.ts
File metadata and controls
124 lines (114 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { Nuxt } from '@nuxt/schema'
import defu from 'defu'
import { normalize } from 'pathe'
import type { FontIconSet, IconFontName, InlineModuleOptions, VuetifyModuleOptions } from '../types'
import { loadVuetifyConfiguration } from './config'
/**
* Merges project layer with registered vuetify modules
*/
export async function mergeVuetifyModules(options: VuetifyModuleOptions, nuxt: Nuxt) {
const moduleOptions: InlineModuleOptions[] = []
const vuetifyConfigurationFilesToWatch = new Set<string>()
await nuxt.callHook('vuetify:registerModule', layerModuleOptions => moduleOptions.push(layerModuleOptions))
if (nuxt.options._layers.length > 1) {
for (let i = 1; i < nuxt.options._layers.length; i++) {
const layer = nuxt.options._layers[i]
const resolvedOptions = await loadVuetifyConfiguration(
layer.config.rootDir,
layer.config.vuetify?.vuetifyOptions,
)
if (resolvedOptions.sources.length) {
resolvedOptions.sources
.map(s => s.replace(/\\/g, '/'))
.filter(s => !s.includes('/node_modules/'))
.forEach(s => vuetifyConfigurationFilesToWatch.add(s))
}
moduleOptions.push({
moduleOptions: layer.config.vuetify?.moduleOptions,
vuetifyOptions: resolvedOptions.config,
})
}
}
const resolvedOptions = await loadVuetifyConfiguration(
nuxt.options.rootDir,
options.vuetifyOptions,
)
// handle vuetify configuraton files changes only in dev mode
if (nuxt.options.dev && resolvedOptions.sources.length) {
// we need to restart nuxt dev server when SSR is enabled: vite-node doesn't support HMR in server yet
if (nuxt.options.ssr) {
if (nuxt.options.future?.compatibilityVersion === 4) {
if (resolvedOptions.sources.length) {
resolvedOptions.sources
.map(s => s.replace(/\\/g, '/'))
.filter(s => !s.includes('/node_modules/'))
.forEach(s => vuetifyConfigurationFilesToWatch.add(s))
}
}
else {
resolvedOptions.sources.forEach(s => nuxt.options.watch.push(normalize(s)))
}
}
else {
resolvedOptions.sources.forEach(s => vuetifyConfigurationFilesToWatch.add(s))
}
}
// unshift since we need to use the app configuration as base in defu call (L64 below): fix #231
moduleOptions.unshift({
moduleOptions: options.moduleOptions,
vuetifyOptions: resolvedOptions.config,
})
// this can be complex and may require dedupe a few more entries:
// - we don't know if the vuetify configuration is merged:
// for example, adding a layer with inlined vuetify options:
// nuxt will merge the conf for us (see issue #214 and #217)
// - if the layer is configured using an external file, then we need to merge the configuration
if (moduleOptions.length > 1) {
const [app, ...rest] = moduleOptions
// we don't need to reverse (defu second arg are the defaults): fix #218
const configuration = <InlineModuleOptions>defu(app, ...rest)
// dedupe icons sets: fix #214 and #217
// we need to reverse the modules to override the icons from bottom to top layer
dedupeIcons(configuration, moduleOptions.reverse())
return {
configuration,
vuetifyConfigurationFilesToWatch,
}
}
else {
return {
configuration: {
moduleOptions: options.moduleOptions,
vuetifyOptions: resolvedOptions.config,
} satisfies InlineModuleOptions,
vuetifyConfigurationFilesToWatch,
}
}
}
// dedupe icons sets: fix #214 and #217
function dedupeIcons(configuration: InlineModuleOptions, moduleOptions: InlineModuleOptions[]) {
const vuetifyOptions = configuration.vuetifyOptions
if (vuetifyOptions.icons) {
if (vuetifyOptions.icons.sets) {
const sets = new Map<string, FontIconSet>()
// modules are reversed, so the last one has the highest priority (app)
for (const { vuetifyOptions } of moduleOptions) {
if (vuetifyOptions.icons && vuetifyOptions.icons.sets) {
const mSets = vuetifyOptions.icons.sets
if (typeof mSets === 'string') {
sets.set(mSets, { name: mSets as IconFontName })
}
else {
for (const set of mSets) {
if (typeof set === 'string')
sets.set(set, { name: set as IconFontName })
else
sets.set(set.name, set)
}
}
}
}
vuetifyOptions.icons.sets = Array.from(sets.values())
}
}
}