From e9e717d7b9b339249e86dd264cab90e184165fcd Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Fri, 8 Mar 2019 22:11:29 +0800 Subject: [PATCH 1/2] custom name registration --- packages/@vuepress/plugin-register-components/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/plugin-register-components/index.js b/packages/@vuepress/plugin-register-components/index.js index d5927f3f85..2def3e73fe 100644 --- a/packages/@vuepress/plugin-register-components/index.js +++ b/packages/@vuepress/plugin-register-components/index.js @@ -1,16 +1,14 @@ const { fs, path, globby, datatypes: { isString }} = require('@vuepress/shared-utils') function fileToComponentName (file) { - return file - .replace(/\/|\\/g, '-') - .replace(/\.vue$/, '') + return file.replace(/\/|\\/g, '-') } async function resolveComponents (componentDir) { if (!fs.existsSync(componentDir)) { return } - return (await globby(['**/*.vue'], { cwd: componentDir })) + return (await globby(['**/*.vue'], { cwd: componentDir })).map(file => file.slice(0, -4)) } // Since this plugin can ben used by multiple times, we need to @@ -22,7 +20,7 @@ module.exports = (options, context) => ({ multiple: true, async enhanceAppFiles () { - const { componentsDir = [], components = [] } = options + const { componentsDir = [], components = [], getComponentName = fileToComponentName } = options const baseDirs = Array.isArray(componentsDir) ? componentsDir : [componentsDir] function importCode (name, absolutePath) { @@ -30,7 +28,7 @@ module.exports = (options, context) => ({ } function genImport (baseDir, file) { - const name = fileToComponentName(file) + const name = getComponentName(file) const absolutePath = path.resolve(baseDir, file) const code = importCode(name, absolutePath) return code From 5125d2d1c79d5e8aeb80867f546048100a5ace62 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 11 Mar 2019 12:16:52 +0800 Subject: [PATCH 2/2] add docs --- .../docs/plugin/official/plugin-register-components.md | 7 +++++++ .../docs/zh/plugin/official/plugin-register-components.md | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/packages/docs/docs/plugin/official/plugin-register-components.md b/packages/docs/docs/plugin/official/plugin-register-components.md index 3971cedbe0..d7ce45f51e 100644 --- a/packages/docs/docs/plugin/official/plugin-register-components.md +++ b/packages/docs/docs/plugin/official/plugin-register-components.md @@ -68,3 +68,10 @@ module.exports = { ] } ``` + +### getComponentName + +- Type: `(file: string) => string` +- Default: `file => file.replace(/\/|\\/g, '-')` + +Customize component names for files under `componentsDir`. diff --git a/packages/docs/docs/zh/plugin/official/plugin-register-components.md b/packages/docs/docs/zh/plugin/official/plugin-register-components.md index 4bd28832ec..86a58d9243 100644 --- a/packages/docs/docs/zh/plugin/official/plugin-register-components.md +++ b/packages/docs/docs/zh/plugin/official/plugin-register-components.md @@ -68,3 +68,10 @@ module.exports = { ] } ``` + +### getComponentName + +- 类型: `(file: string) => string` +- 默认值: `file => file.replace(/\/|\\/g, '-')` + +自定义 `componentsDir` 中注册的组件的名称。