-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper-module.ts
54 lines (49 loc) · 1.42 KB
/
helper-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
import fs from 'fs'
import path from 'path'
import { addPluginTemplate, defineNuxtModule } from '@nuxt/kit'
/**
* Module
*/
export default defineNuxtModule({
meta: {
name: 'helper-module',
},
async setup(_, nuxt) {
const srcDir = nuxt.options.srcDir
const resolve = (...args) => path.resolve(srcDir, ...args)
const components = await Promise.all([
resolve('components/demo'),
resolve('components/example'),
resolve('components/ui'),
].map(async(dir) => {
const files = await fs.promises.readdir(dir)
return files.map((fileName) => {
const filePath = resolve(dir, fileName)
const name = fileName.replace(/\.vue$/, '')
const component = {
name,
path: filePath,
}
return component
})
}))
const flattenComponents = components.flat()
addPluginTemplate({
filename: 'demo-components.ts',
write: true,
getContents: () => {
return [
'import { defineNuxtPlugin } from \'#app\'',
...flattenComponents.map((component) => {
return `import ${component.name} from '${component.path}'`
}),
'\n',
'export default defineNuxtPlugin(async(nuxtApp) => {',
...flattenComponents
.map(component => ` nuxtApp.vueApp.component('${component.name}', ${component.name})`),
'})',
].join('\n')
},
})
},
})