register-components

@vuepress/plugin-register-components

Register Vue components from component files or directory automatically.

Options

components

  • Type: Record<string, string>

  • Default: {}

  • Details:

    An object that defines name of components and their corresponding file path.

    The key will be used as the component name, and the value is an absolute path of the component file.

    If the component name from this option conflicts with componentsDir option, this option will have a higher priority.

  • Example:

const { path } = require('@vuepress/utils')

module.exports = {
  plugins: [
    [
      '@vuepress/register-components',
      {
        components: {
          FooBar: path.resolve(__dirname, './components/FooBar.vue'),
        },
      },
    ],
  ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

componentsDir

  • Type: string | null

  • Default: null

  • Details:

    An absolute path of the components directory.

    Files in this directory which are matched with componentsPatterns will be registered as Vue components automatically.

  • Example:

module.exports = {
  plugins: [
    [
      '@vuepress/register-components',
      {
        componentsDir: path.resolve(__dirname, './components'),
      },
    ],
  ],
}
1
2
3
4
5
6
7
8
9
10

Components directory:

components
├─ FooBar.vue
└─ Baz.vue
1
2
3

Components will be registered like this:

import { defineAsyncComponent } from 'vue'

app.component(
  'FooBar',
  defineAsyncComponent(() => import('/path/to/components/FooBar.vue'))
)

app.component(
  'Baz',
  defineAsyncComponent(() => import('/path/to/components/Baz.vue'))
)
1
2
3
4
5
6
7
8
9
10
11

componentsPatterns

getComponentName

  • Type: (filename: string) => string

  • Default: (filename) => path.trimExt(filename.replace(/\/|\\/g, '-'))

  • Details:

    A function to get component name from the filename.

    It will only take effect on the files in the componentsDir which are matched with the componentsPatterns.

    Notice that the filename is a filepath relative to componentsDir.