This repository was archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule.ts
120 lines (107 loc) · 3.98 KB
/
module.ts
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
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, addComponent, addImportsDir, createResolver } from '@nuxt/kit'
import { defu } from 'defu'
import { consola } from 'consola'
import { PrimeVueConfiguration } from 'primevue/config'
import { name, version } from '../package.json'
import {
defaultPrimevueComponentNames,
defaultPrimeVueComponents,
defaultPrimevueExcludeComponentNames,
PrimeVueComponent
} from './runtime/primevueComponents'
// #region options
export interface ModuleOptions {
config: PrimeVueConfiguration
components?: {
include?: Array<PrimeVueComponent | string>
exclude?: Array<string>
force?: Array<PrimeVueComponent | string>
}
useFormkit: boolean
quiet: boolean
includeDemo: boolean
}
// #endregion options
async function registerComponent (component: PrimeVueComponent, registeredNames: string[]) {
if (!registeredNames.includes(component.name)) {
await addComponent({
export: 'default',
filePath: `primevue/${component.name.toLowerCase()}`,
global: component.global,
name: component.name
})
}
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name,
version,
configKey: 'primevue',
compatibility: {
nuxt: '^3.0.0'
}
},
defaults: {
config: {
ripple: true
},
useFormkit: true,
quiet: false,
includeDemo: true
},
async setup (moduleOptions, nuxt) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.runtimeConfig.public.primevue = defu(nuxt.options.runtimeConfig.public.primevue,
{
config: moduleOptions.config
}
)
nuxt.options.build.transpile.push(runtimeDir)
nuxt.options.build.transpile.push('primevue')
const resolver = createResolver(import.meta.url)
addPlugin(resolver.resolve('./runtime/plugin'))
async function addComponents (components: Array<PrimeVueComponent | string>) {
for (const value of components) {
if (typeof value === 'string') {
const component = {} as PrimeVueComponent
component.name = value
component.global = false
await registerComponent(component, registeredNames)
registeredNames = [...registeredNames, value]
} else {
const component = value as PrimeVueComponent
await registerComponent(component, registeredNames)
registeredNames = [...registeredNames, component.name]
}
}
}
let registeredNames: string[] = []
if (moduleOptions.components?.include && moduleOptions.components?.include.length > 0) {
await addComponents(moduleOptions.components?.include)
} else {
for (const component of defaultPrimeVueComponents(moduleOptions.useFormkit)) {
if (!moduleOptions.components?.exclude || !moduleOptions.components?.exclude.includes(component.name)) {
if (!defaultPrimevueExcludeComponentNames.includes(component.name)) {
await registerComponent(component, registeredNames)
registeredNames = [...registeredNames, component.name]
}
}
}
}
if (moduleOptions.components?.force) { await addComponents(moduleOptions.components?.force) }
if (!moduleOptions.quiet) {
consola.info('[@sfxcode/nuxt-primevue] ' + registeredNames.length + ' of ' + (defaultPrimevueComponentNames.length + defaultPrimevueExcludeComponentNames.length) + ' PrimeVue Components added, finetetuning if needed by components configuration in module options')
}
// consola.info(registeredNames)
addImportsDir(resolve(runtimeDir, 'composables'))
if (moduleOptions.includeDemo) {
await addComponent({ name: 'PrimeDemoToast', filePath: resolve(runtimeDir, 'components/demo/PrimeDemoToast.vue') })
await addComponent({ name: 'PrimeDemoForm', filePath: resolve(runtimeDir, 'components/demo/PrimeDemoForm.vue') })
}
if (!moduleOptions.quiet) {
consola.success('[@sfxcode/nuxt-primevue] loaded')
}
}
})