From 9c6a00bad5431312c709004a49c64a36d1c3ac08 Mon Sep 17 00:00:00 2001 From: Shigma <33423008+Shigma@users.noreply.github.com> Date: Wed, 13 Mar 2019 02:16:31 +0800 Subject: [PATCH] feat($plugin-register-components): custom name registration (close: #656) (#1418) --- packages/@vuepress/plugin-register-components/index.js | 10 ++++------ .../docs/plugin/official/plugin-register-components.md | 7 +++++++ .../zh/plugin/official/plugin-register-components.md | 7 +++++++ 3 files changed, 18 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 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` 中注册的组件的名称。