-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
60 lines (48 loc) · 1.74 KB
/
index.js
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
const { fs, path, globby, datatypes: { isString }} = require('@vuepress/shared-utils')
function fileToComponentName (file) {
return file.replace(/\/|\\/g, '-')
}
async function resolveComponents (componentDir) {
if (!fs.existsSync(componentDir)) {
return
}
return (await globby(['**/*.vue'], { cwd: componentDir })).map(file => file.slice(0, -4))
}
// Since this plugin can ben used by multiple times, we need to
// give each generated files a uid or the previous file would be
// overwritten.
let moduleId = 0
module.exports = (options, context) => ({
multiple: true,
async enhanceAppFiles () {
const { componentsDir = [], components = [], getComponentName = fileToComponentName } = options
const baseDirs = Array.isArray(componentsDir) ? componentsDir : [componentsDir]
function importCode (name, absolutePath) {
return `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
}
function genImport (baseDir, file) {
const name = getComponentName(file)
const absolutePath = path.resolve(baseDir, file)
const code = importCode(name, absolutePath)
return code
}
let code = ''
// 1. Register components in specified directories
for (const baseDir of baseDirs) {
if (!isString(baseDir)) {
continue
}
const files = await resolveComponents(baseDir) || []
code += files.map(file => genImport(baseDir, file)).join('\n') + '\n'
}
// 2. Register named components.
code += components.map(({ name, path: absolutePath }) => importCode(name, absolutePath))
code = `import Vue from 'vue'\n` + code + '\n'
return [
{
name: `global-components-${++moduleId}.js`,
content: code
}
]
}
})