All Projects → wuruoyun → vue-component-lib-starter

wuruoyun / vue-component-lib-starter

Licence: other
A bare-bones example of creating your own Vue component library.

Programming Languages

typescript
32286 projects
Vue
7211 projects
SCSS
7915 projects

Projects that are alternatives of or similar to vue-component-lib-starter

vue-devui-early
Vue3版本的DevUI组件库。本仓库已迁移至:https://github.com/DevCloudFE/vue-devui
Stars: ✭ 39 (-82.35%)
Mutual labels:  component-library, vue3, vite
vueuse-vite-starter
⚡️ Starter for Vite + VueUse + TypeScript
Stars: ✭ 121 (-45.25%)
Mutual labels:  starter, vue3, vite
vui-vc-next
Vue 3 with Vite Playground - Mobile web UI components - (vue3+vite2).
Stars: ✭ 15 (-93.21%)
Mutual labels:  vue3, vite, vitepress
vite-primevue-starter
VUE 3 Starter project for using primevue 3 with Vite 2 - Pages, Layouts, Validation
Stars: ✭ 37 (-83.26%)
Mutual labels:  starter, vue3, vite
codemirror-editor-vue3
CodeMirror component for Vue3
Stars: ✭ 22 (-90.05%)
Mutual labels:  vue3, vite, vitepress
miter-design
Miter Design component library made with ♡ by Prefect
Stars: ✭ 14 (-93.67%)
Mutual labels:  component-library, vue3
chusho
A library of bare & accessible components and tools for Vue.js 3
Stars: ✭ 47 (-78.73%)
Mutual labels:  component-library, vue3
layui-vue
layui - vue(谐音:类 UI) 是 一 套 Vue 3.0 的 桌 面 端 组 件 库
Stars: ✭ 112 (-49.32%)
Mutual labels:  component-library, vite
ungeui
A Vue 3 Component Library
Stars: ✭ 23 (-89.59%)
Mutual labels:  component-library, vue3
Nutui
京东风格的移动端 Vue2、Vue3 组件库 (A Vue.js UI Toolkit for Mobile Web)
Stars: ✭ 3,870 (+1651.13%)
Mutual labels:  vue3, vite
hoc-element-table
📦 A Vue 3.x Table Component built on Webpack 5
Stars: ✭ 26 (-88.24%)
Mutual labels:  component-library, vue3
fastadmin
vue3 + element-plus fast admin scaffold, 基于vue3和ElementPlus的中后台快速应用脚手架
Stars: ✭ 50 (-77.38%)
Mutual labels:  vue3, vite
Vue3 News
🔥 Find the latest breaking Vue3、Vue CLI 3+ & Vite News. (2021)
Stars: ✭ 2,416 (+993.21%)
Mutual labels:  vue3, vite
artemis
MateCloud前端代码,基于vue3、vite、pinia、ant-design vue实现的中台系统
Stars: ✭ 129 (-41.63%)
Mutual labels:  vue3, vite
Different-UI
✨ A Vue.js 3 UI Library — a Toy
Stars: ✭ 62 (-71.95%)
Mutual labels:  component-library, vue3
Vue Vben Admin
A modern vue admin. It is based on Vue3, vite and TypeScript. It's fast!
Stars: ✭ 8,036 (+3536.2%)
Mutual labels:  vue3, vite
bootstrap-vue-3
Early (but lovely) implementation of Vue 3, Bootstrap 5 and Typescript
Stars: ✭ 314 (+42.08%)
Mutual labels:  component-library, vue3
vue-next-admin
🎉🎉🔥基于vue3.x 、Typescript、vite、Element plus等,适配手机、平板、pc 的后台开源免费模板库(vue2.x请切换vue-prev-admin分支)
Stars: ✭ 1,002 (+353.39%)
Mutual labels:  vue3, vite
vite-electron-ts
基于vite和electorn的项目模板
Stars: ✭ 35 (-84.16%)
Mutual labels:  vue3, vite
vital
Starter template for Vite with React (TypeScript). Supports Tailwind with CSS-Modules. Jest and @react/testing-library configured and ready to go. Also ESLint, Prettier, Husky, Commit-lint and Atomic Design for components.
Stars: ✭ 151 (-31.67%)
Mutual labels:  starter, vite

Netlify Status

Vue Component Library Starter

Create your own Vue 3 component library with TypeScript, Vite and VitePress.

Sooner or later, you will find that creating a component library is much better than having all components inside your app project. A component library force to you remove app specific logic from your components, making it easier to test and reuse them in other apps.

Once the components are in a library, documentation becomes critical. This starter project includes a documentation app powered by VitePress. It not only documents the usage of the component, but also provides a testing bed during the development of components. See the generated documentation app here.

Setup

When running docs:dev for the first time, you may encounter error like vitepress data not properly injected in app in your browser. Restart the server and reload the browser. Please refer to issue #30 for more details.

# install dependencies
npm install

# start the doc app with hot reload, great for testing components
npm run docs:dev

# build the library, available under dist
npm run build

# build the doc app, available under docs/.vitepress/dist
npm run docs:build

# preview the doc app locally from docs/.vitepress/dist
npm run docs:serve

You may use Netlify to auto build and deloy the doc app like this project does.

Develop and test locally

The best way to develop and test your component is by creating demos in docs/components/demo folder, as shown by the example components.

If you want to test the library in your Vue3 app locally:

  • In the root folder of this library, run npm link. This will create a symbolic link to the library.
  • In the root folder of your client app, run npm link my-lib. This will add the symbolic link to the node_modules folder in your client app.
  • You can now import my-lib in your client app.

There is no need to add my-lib to your client app's dependency in this case.

If you made changes to the library, you will need to rebuild the library. Your Vue3 app shall hot reload when the building of library is completed.

How it works

Components

The library is a Vue plugin. The install function in index.ts registers all components under components to Vue globably.

The components are also exported by index.ts so that the client app can import them individually and register them locally, instead of using the library as a plugin. This may be a better option if the client app only use a small set of components in your library.

As there are already many UI component libraries for Vue 3, you may just want to build on top of one of them and create components for your specific needs. The Component B in this starter shows the example of using PrimeVue as the fundation library. However, this means the client app shall also use the same fundation component library as your library does.

The doc app itself is a client app of the libary, therefore PrimeVue is imported in docs/.vitepress/theme/index.js. The configuration in docs/.vitepress/config.js below forces VitePress to resolve these modules with no duplication, avoiding error at runtime, as PrimeVue also has Vue in its dependency.

module.exports = {
  vite: {
    resolve: {
      dedupe: ['vue', /primevue\/.+/],
    },
  },
}

In vite.config.ts, format 'umd' is not present in build.lib.formats option. This is because the PrimeVue components used by this library are externalized, and therefore requiring corresponding options in rollupOptions.output.globals. To avoid adding global varaibles for PrimeVue components, 'umd' is removed for simplicity.

Utilities and constants

The library includes example utilities and constants. They are also exported in index.ts. The client app may use them as below:

<script lang="ts">
import { MyConstants, MyUtil } from 'my-lib'

export default {
  data () {
    return {
      magicNum: MyConstants.MAGIC_NUM
    }
  },
  methods: {
    add (a:number, b:number) {
      return MyUtil.add(a, b)
    }
  }
}
</script>

Styling

Individual compopnent may have styles defined in its .vue file. They will be processed, combined and minified into dist/style.css, which is included in the exports list in package.json.

If you have library level styles shared by all components in the library, you may add them to src/assets/main.scss. This file is imported in index.ts, therefore the processed styles are also included into dist/style.css. To avoid conflicting with other global styles, consider pre-fixing the class names or wrapping them into a namespace class.

If you have your own special set of SVG icons, you may create a font file (.woff format) using tools like Icomoon or Fontello. This starter includes an example font file src/assets/fonts/myfont.woff and references it in src/assets/main.scss, with utility icon CSS classes. An icon from the font file is used in Component A. Vite will include the font file into the build, see https://vitejs.dev/guide/assets.html.

The client app shall import style.css, usually in the entry file:

import 'my-lib/dist/style.css'

Third-party dependencies

Third-party libraries used by you library may bloat up the size of your library, if you simply add them to the dependencies in package.json.

The following are some strategies to reduce the size of your library:

Externalization

If you expect the client app of your library may also need the same dependency, you may externalize the dependency. For example, to exclude PrimeVue from your library build artifact, in vite.config.ts, you may have

module.exports = defineConfig({
    rollupOptions: {
      external: ['vue', /primevue\/.+/]
    }
  }
})

The dependency to be externalized may be declared as peer dependency in your library.

Cherry picking

If you don't expect the client app of your library also needing the same dependency, you may embed cherry-picked functions. For example, to embed the fill function of popular library lodash, import the fill function like the following:

import fill from 'lodash/fill'

Even with tree-shaking, the codes being brought into your library may still be large, as the function may have its own dependencies.

Note that import { fill } from 'lodash' or import _ from 'lodash' will not work and will embed the whole lodash library.

Finally, if your client app also use lodash and you don't want lodash to be in both the client app and your libraries, even after cherry-picking, you may consider cherry-picking in component library and re-export them as utils for client to consume, so that the client does not need to depend on lodash, therefore avoiding duplication.

Type generation

In tsconfig.json, the following options instructs tsc to emit declaration (.d.ts files) only, as vite build handles the .js file generation. The generated .d.ts files are sent to dist/types folder.

"compilerOptions": {
  "declaration": true,
  "emitDeclarationOnly": true,
  "declarationDir": "./dist/types"
}

In package.json, the line below locates the generated types for library client.

"types": "./dist/types/index.d.ts",

In vite.config.ts, build.emptyOutDir is set to false and rimraf is used instead to remove the dist folder before the build. This is to avoid the dist/types folder generated by tsc being deleted when running vite build.

Configuration

TypeScript

In tsconfig.json, set the following as recommended by Vite (since esbuild is used). However, enableing this option leads to vitejs/vite#5814. The workaround is to also enable compilerOptions.skipLibCheck.

"compilerOptions": {
  "isolatedModules": true
}

In tsconfig.json, set the following to address Issue #32. The solution is from vuejs/language-tools#592.

"compilerOptions": {
  "types": [
    "vite/client"
  ]
}

Dependencies

In package.json, Vue and PrimeVue are declared in both peerDependencies and devDependencies. The former requires the client app to add these dependencies, and the later makes it easier to setup this library by simply running npm install.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].