Skip to content

Commit 9c6a00b

Browse files
shigmaulivz
authored andcommitted
feat($plugin-register-components): custom name registration (close: #656) (#1418)
1 parent 5c0e62f commit 9c6a00b

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

packages/@vuepress/plugin-register-components/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
const { fs, path, globby, datatypes: { isString }} = require('@vuepress/shared-utils')
22

33
function fileToComponentName (file) {
4-
return file
5-
.replace(/\/|\\/g, '-')
6-
.replace(/\.vue$/, '')
4+
return file.replace(/\/|\\/g, '-')
75
}
86

97
async function resolveComponents (componentDir) {
108
if (!fs.existsSync(componentDir)) {
119
return
1210
}
13-
return (await globby(['**/*.vue'], { cwd: componentDir }))
11+
return (await globby(['**/*.vue'], { cwd: componentDir })).map(file => file.slice(0, -4))
1412
}
1513

1614
// Since this plugin can ben used by multiple times, we need to
@@ -22,15 +20,15 @@ module.exports = (options, context) => ({
2220
multiple: true,
2321

2422
async enhanceAppFiles () {
25-
const { componentsDir = [], components = [] } = options
23+
const { componentsDir = [], components = [], getComponentName = fileToComponentName } = options
2624
const baseDirs = Array.isArray(componentsDir) ? componentsDir : [componentsDir]
2725

2826
function importCode (name, absolutePath) {
2927
return `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
3028
}
3129

3230
function genImport (baseDir, file) {
33-
const name = fileToComponentName(file)
31+
const name = getComponentName(file)
3432
const absolutePath = path.resolve(baseDir, file)
3533
const code = importCode(name, absolutePath)
3634
return code

packages/docs/docs/plugin/official/plugin-register-components.md

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ module.exports = {
6868
]
6969
}
7070
```
71+
72+
### getComponentName
73+
74+
- Type: `(file: string) => string`
75+
- Default: `file => file.replace(/\/|\\/g, '-')`
76+
77+
Customize component names for files under `componentsDir`.

packages/docs/docs/zh/plugin/official/plugin-register-components.md

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ module.exports = {
6868
]
6969
}
7070
```
71+
72+
### getComponentName
73+
74+
- 类型: `(file: string) => string`
75+
- 默认值: `file => file.replace(/\/|\\/g, '-')`
76+
77+
自定义 `componentsDir` 中注册的组件的名称。

0 commit comments

Comments
 (0)