0.27.0 • Published 1 month ago

unplugin-vue-components v0.27.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

unplugin-vue-components

NPM version

On-demand components auto importing for Vue.

Features
  • 💚 Supports both Vue 2 and Vue 3 out-of-the-box.
  • ✨ Supports both components and directives.
  • ⚡️ Supports Vite, Webpack, Rspack, Vue CLI, Rollup, esbuild and more, powered by unplugin.
  • 🏝 Tree-shakable, only registers the components you use.
  • 🪐 Folder names as namespaces.
  • 🦾 Full TypeScript support.
  • 🌈 Built-in resolvers for popular UI libraries.
  • 😃 Works perfectly with unplugin-icons.

Installation

npm i unplugin-vue-components -D

vite-plugin-components has been renamed to unplugin-vue-components, see the migration guide.

// vite.config.ts
import Components from 'unplugin-vue-components/vite'

export default defineConfig({
  plugins: [
    Components({ /* options */ }),
  ],
})

// rollup.config.js
import Components from 'unplugin-vue-components/rollup'

export default {
  plugins: [
    Components({ /* options */ }),
  ],
}

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-components/webpack').default({ /* options */ }),
  ],
}

// rspack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-components/rspack').default({ /* options */ }),
  ],
}

You might not need this plugin for Nuxt. Use @nuxt/components instead.

// vue.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-components/webpack').default({ /* options */ }),
  ],
}

You can also rename the Vue configuration file to vue.config.mjs and use static import syntax (you should use latest @vue/cli-service ^5.0.8):

// vue.config.mjs
import Components from 'unplugin-vue-components/webpack'

export default {
  configureWebpack: {
    plugins: [
      Components({ /* options */ }),
    ],
  },
}

// esbuild.config.js
import { build } from 'esbuild'
import Components from 'unplugin-vue-components/esbuild'

build({
  /* ... */
  plugins: [
    Components({
      /* options */
    }),
  ],
})

Usage

Use components in templates as you would usually do, it will import components on demand, and there is no import and component registration required anymore! If you register the parent component asynchronously (or lazy route), the auto-imported components will be code-split along with their parent.

It will automatically turn this

<template>
  <div>
    <HelloWorld msg="Hello Vue 3.0 + Vite" />
  </div>
</template>

<script>
  export default {
    name: 'App',
  }
</script>

into this

<template>
  <div>
    <HelloWorld msg="Hello Vue 3.0 + Vite" />
  </div>
</template>

<script>
  import HelloWorld from './src/components/HelloWorld.vue'

  export default {
    name: 'App',
    components: {
      HelloWorld,
    },
  }
</script>

Note By default this plugin will import components in the src/components path. You can customize it using the dirs option.

TypeScript

To get TypeScript support for auto-imported components, there is a PR to Vue 3 extending the interface of global components. Currently, Volar has supported this usage already. If you are using Volar, you can change the config as following to get the support.

Components({
  dts: true, // enabled by default if `typescript` is installed
})

Once the setup is done, a components.d.ts will be generated and updates automatically with the type definitions. Feel free to commit it into git or not as you want.

Make sure you also add components.d.ts to your tsconfig.json under include.

Importing from UI Libraries

We have several built-in resolvers for popular UI libraries like Vuetify, Ant Design Vue, and Element Plus, where you can enable them by:

Supported Resolvers:

// vite.config.js
import Components from 'unplugin-vue-components/vite'
import {
  AntDesignVueResolver,
  ElementPlusResolver,
  VantResolver,
} from 'unplugin-vue-components/resolvers'

// your plugin installation
Components({
  resolvers: [
    AntDesignVueResolver(),
    ElementPlusResolver(),
    VantResolver(),
  ],
})

You can also write your own resolver quickly:

Components({
  resolvers: [
    // example of importing Vant
    (componentName) => {
      // where `componentName` is always CapitalCase
      if (componentName.startsWith('Van'))
        return { name: componentName.slice(3), from: 'vant' }
    },
  ],
})

We no longer accept new resolvers.

Types for global registered components

Some libraries might register some global components for you to use anywhere (e.g. Vue Router provides <RouterLink> and <RouterView>). Since they are global available, there is no need for this plugin to import them. However, those are commonly not TypeScript friendly, and you might need to register their types manually.

Thus unplugin-vue-components provided a way to only register types for global components.

Components({
  dts: true,
  types: [{
    from: 'vue-router',
    names: ['RouterLink', 'RouterView'],
  }],
})

So the RouterLink and RouterView will be presented in components.d.ts.

By default, unplugin-vue-components detects supported libraries automatically (e.g. vue-router) when they are installed in the workspace. If you want to disable it completely, you can pass an empty array to it:

Components({
  // Disable type only registration
  types: [],
})

Migrate from vite-plugin-components

package.json

{
  "devDependencies": {
-   "vite-plugin-components": "*",
+   "unplugin-vue-components": "^0.14.0",
  }
}

vite.config.js

- import Components, { ElementPlusResolver } from 'vite-plugin-components'
+ import Components from 'unplugin-vue-components/vite'
+ import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default {
  plugins: [
    /* ... */
    Components({
      /* ... */

      // `customComponentsResolvers` has renamed to `resolver`
-     customComponentsResolvers: [
+     resolvers: [
        ElementPlusResolver(),
      ],

      // `globalComponentsDeclaration` has renamed to `dts`
-     globalComponentsDeclaration: true,
+     dts: true,

      // `customLoaderMatcher` is depreacted, use `include` instead
-     customLoaderMatcher: id => id.endsWith('.md'),
+     include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
    }),
  ],
}

Configuration

The following show the default values of the configuration

Components({
  // relative paths to the directory to search for components.
  dirs: ['src/components'],

  // valid file extensions for components.
  extensions: ['vue'],

  // Glob patterns to match file names to be detected as components.
  // When specified, the `dirs` and `extensions` options will be ignored.
  globs: ['src/components/*.{vue}'],

  // search for subdirectories
  deep: true,

  // resolvers for custom components
  resolvers: [],

  // generate `components.d.ts` global declarations,
  // also accepts a path for custom filename
  // default: `true` if package typescript is installed
  dts: false,

  // Allow subdirectories as namespace prefix for components.
  directoryAsNamespace: false,

  // Collapse same prefixes (camel-sensitive) of folders and components
  // to prevent duplication inside namespaced component name.
  // works when `directoryAsNamespace: true`
  collapseSamePrefixes: false,

  // Subdirectory paths for ignoring namespace prefixes.
  // works when `directoryAsNamespace: true`
  globalNamespaces: [],

  // auto import for directives
  // default: `true` for Vue 3, `false` for Vue 2
  // Babel is needed to do the transformation for Vue 2, it's disabled by default for performance concerns.
  // To install Babel, run: `npm install -D @babel/parser`
  directives: true,

  // Transform path before resolving
  importPathTransform: v => v,

  // Allow for components to override other components with the same name
  allowOverrides: false,

  // filters for transforming targets
  include: [/\.vue$/, /\.vue\?vue/],
  exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],

  // Vue version of project. It will detect automatically if not specified.
  // Acceptable value: 2 | 2.7 | 3
  version: 2.7,

  // Only provide types of components in library (registered globally)
  types: []
})

Example

Vitesse starter template.

Thanks

Thanks to @brattonross, this project is heavily inspired by vite-plugin-voie.

License

MIT License © 2020-PRESENT Anthony Fu

visualization1belvoly-signature-testayynn-uiam-sctob-sub-moduledooringx-example-vue3.0@nuxtjs-alt/element-plusvue3-typescript-admin-template@snacking/see-vuevue3-plat-indexnuxt-naive-ui@bg-dev/nuxt-naive-uiice-datav-next@vri/config-vite@dankeu/vite-plugin-vuevy-andvhkcomponentsv3yc-series-ui@everything-registry/sub-chunk-3017@zf-dbs/vite-define-configxiaoyuer-uishifl-ant-designxlwal-vantjdlinker-systemjdlinker-weblc-ui-packagekk-element-plusjw-comsjustfe-vuelibrary-sanhosle-printlzz-uilib.common4link-lanhclc-front-componentshdfvuetemplatehocihooyn-calendarhoudunren-vueilesj-doguimodules-uimeltuimgltestmg-templatemydnic-uinextgen-frontendnetvuenetvuetemplate@stefanvh/quasar-app-vite@storipress/karbon@uni-web-lab/vue3-spa-template@phongthien/vue3-query-builder@pkt/uni-devkit@opentiny/unplugin-tiny-vue@prestashopcorp/smb-edition-homepage@pqcqcqc/qc-components@pmap-cesium/panorama@tofandel/hybridly-vite@ueberbit/vite-plugin-drupal@supjt/b20210930_wsir_wy2-demo@tailor-cms/tce-display-runtime@tailor-cms/tce-edit-next-runtime@tailor-cms/tce-edit-runtime@taskday/core@tb-dev/vue-import-config@tcsl-test/sl-ui@qso/app@slidev/cli@simple-site-generator/templates@stacksjs/types@stacksjs/vite@star-trek/arco-vue-resolver@starpad/nuxtwhscomyugu-packageyj-elm-uiyl-micro-web-utilsvy-vuevuetify-ds-shiflvuekit-devvuekit-vuevue3_newsvue-fun-rainvue-mailervue3minioupdatavue-vuecmf-editorvue-vuecmf-fileexplorervue-project-demo-testvulmixvue3-vuecmf-tablewaltz-buildxhl_libxiaoxin-vue-clixing-form-engineyoo-ui-proym-table-tplzhoubj-uizf-vue-web-vitezkzc-bpmnhdfvueframework
0.27.0

1 month ago

0.26.0

6 months ago

0.25.2

10 months ago

0.25.1

1 year ago

0.25.0

1 year ago

0.23.0

1 year ago

0.24.1

1 year ago

0.24.0

1 year ago

0.22.12

2 years ago

0.22.11

2 years ago

0.22.9

2 years ago

0.22.7

2 years ago

0.22.6

2 years ago

0.22.8

2 years ago

0.20.1

2 years ago

0.19.9

2 years ago

0.20.0

2 years ago

0.19.6

2 years ago

0.21.2

2 years ago

0.21.1

2 years ago

0.21.0

2 years ago

0.22.4

2 years ago

0.22.3

2 years ago

0.22.2

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.19.5

2 years ago

0.19.0

2 years ago

0.19.1

2 years ago

0.19.2

2 years ago

0.19.3

2 years ago

0.18.1

2 years ago

0.18.2

2 years ago

0.18.3

2 years ago

0.18.4

2 years ago

0.18.5

2 years ago

0.18.0

2 years ago

0.17.21

2 years ago

0.17.20

2 years ago

0.17.3

3 years ago

0.17.4

3 years ago

0.17.5

3 years ago

0.17.6

3 years ago

0.17.8

3 years ago

0.17.9

3 years ago

0.17.10

2 years ago

0.17.11

2 years ago

0.17.14

2 years ago

0.17.13

2 years ago

0.17.16

2 years ago

0.17.15

2 years ago

0.17.18

2 years ago

0.17.17

2 years ago

0.17.19

2 years ago

0.17.2

3 years ago

0.17.0

3 years ago

0.17.1

3 years ago

0.16.0

3 years ago

0.15.5

3 years ago

0.15.6

3 years ago

0.15.4

3 years ago

0.15.3

3 years ago

0.15.2

3 years ago

0.15.1

3 years ago

0.14.13

3 years ago

0.15.0

3 years ago

0.14.12

3 years ago

0.14.11

3 years ago

0.14.10

3 years ago

0.14.6

3 years ago

0.14.7

3 years ago

0.14.8

3 years ago

0.14.9

3 years ago

0.14.5

3 years ago

0.14.4

3 years ago

0.14.3

3 years ago

0.14.2

3 years ago

0.14.1

3 years ago

0.14.0-beta.1

3 years ago

0.0.0

3 years ago