Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 5): How to Use Vite to Package Your Component Library


๐Ÿ“š Building a Vue 3 Component Library from Scratch (Part 5): How to Use Vite to Package Your Component Library


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

This article will introduce how to use Vite to package our component library, and also explain how to use plugins to automatically generate declaration files (*.d.ts) for the packaged files.

Packaging Configuration

Vite specifically provides a packaging method for library mode, and the configuration is actually very simple. First, globally install Vite and @vitejs/plugin-vue.

pnpm add vite @vitejs/plugin-vue -D -w

Create vite.config.ts configuration file under the components directory.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
  build: {
    outDir: "es",
    minify: false,
    rollupOptions: {
      external: ["vue"],
      output: {
        globals: {
          vue: "Vue",
        },
        dir: "dist",
      },
    },
    lib: {
      entry: "./index.ts",
      name: "markliu2013",
      fileName: "markliu2013",
      formats: ["es", "umd", "cjs"],
    },
  },
  plugins: [vue()],
});

Then add the packaging command scripts in components/package.json.

"scripts": {
  "build": "vite build"
},

pnpm run build will get dist folder.

However, this packaging method will eventually package the entire component library into a single file, and the style files cannot be loaded on demand. Therefore, we need to modify the configuration to make the packaged structure consistent with our development structure. In the following configuration, we will place the packaged files in the 'markliu2013' directory, as the name of the component library we will publish later is 'markliu2013'. Of course, you can name it whatever you like.

import {defineConfig} from "vite";
import vue from "@vitejs/plugin-vue";

export default defineConfig({
    build: {
        outDir: "es",
        rollupOptions: {
            external: ["vue"],
            input: ["index.ts"],
            output: [
                {
                    format: "es",
                    entryFileNames: "[name].mjs",
                    preserveModules: true,
                    exports: "named",
                    dir: "dist/es",
                },
                {
                    format: "cjs",
                    entryFileNames: "[name].js",
                    preserveModules: true,
                    exports: "named",
                    dir: "dist/lib",
                },
            ],
        },
        lib: {
            entry: "./index.ts",
        },
    },
    plugins: [vue()],
});

run pnpm run build.

However, at this point, all style files will still be packaged together into style.css, so we still can't load styles on demand. Therefore, next we will configure Vite to not package the style files, and instead package the style files separately later.

Declaration Files

At this point, the packaged component library can only be used in JavaScript projects. Running it in TypeScript projects will cause some errors, and code hints will be lost when using it. This negates the purpose of using TypeScript to develop the component library. Therefore, we need to include declaration files (.d.ts) in the packaged library.

Install vite-plugin-dts, and make sure the version is consistent.

pnpm add [email protected] -D -w

Import it into vite.config.ts. Note that we also add the component naming plugin DefineOptions here (mentioned in the previous article). Be sure to write this after dts, as there might be errors in the source code.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import dts from "vite-plugin-dts";
import DefineOptions from "unplugin-vue-define-options/vite";
export default defineConfig({
  plugins: [
    vue(),
    dts({
      entryRoot: "./src",
      outputDir: ["../markliu2013/es/src", "../markliu2013/lib/src"],
      tsConfigFilePath: "../../tsconfig.json",
    }),
    DefineOptions(),
  ],
});

After packaging again, you will find that the declaration files we need have appeared in the packaged files.

Actually, most front-end build scaffolds now support ES modules, which inherently support on-demand loading. Therefore, the ES format of the packaged component library comes with tree shaking by default, and does not require additional configuration for on-demand imports. Next, we will make the style files support on-demand imports as well. Stay tuned.

...



๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 5): How to Use Vite to Package Your Component Library


๐Ÿ“ˆ 100.31 Punkte

๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 6): Using Gulp to Package the Component Library and On-Demand Import


๐Ÿ“ˆ 76.52 Punkte

๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 1): Introduction


๐Ÿ“ˆ 53.63 Punkte

๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 2): Monorepo


๐Ÿ“ˆ 53.63 Punkte

๐Ÿ“Œ Building a Vue 3 Component Library from Scratch (Part 3): Environment Configuration


๐Ÿ“ˆ 53.63 Punkte

๐Ÿ“Œ Moving From Vue 1 To Vue 2 To Vue 3: A Case Study Of Migrating A Headless CMS System


๐Ÿ“ˆ 39.92 Punkte

๐Ÿ“Œ 45sec scratch game vs 1min scratch game vs 10min scratch game


๐Ÿ“ˆ 39.1 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #6 Gulp Introduce


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #7 Using Gulp to Implement On-Demand Import


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #8 Publish


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #9 Setting Up and Deploying Documentation with VitePress


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #10 Create Cli Scaffold


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #12 Vitest


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Building Vue3 Component Library from Scratch #13 Husky


๐Ÿ“ˆ 33.96 Punkte

๐Ÿ“Œ Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


๐Ÿ“ˆ 32.93 Punkte

๐Ÿ“Œ Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


๐Ÿ“ˆ 32.93 Punkte

๐Ÿ“Œ Why Vite is the best? Advanced Features of Vite


๐Ÿ“ˆ 32.93 Punkte

๐Ÿ“Œ Building a multi-tenant B2B SaaS with Vite and Tanstack Router & Query - Part 1: The boilerplate


๐Ÿ“ˆ 30.02 Punkte

๐Ÿ“Œ How to develop a local package and use in a vite app


๐Ÿ“ˆ 29.84 Punkte

๐Ÿ“Œ Vite.js: Rasantes Build-Tool aus der Vue.js-Schmiede


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Entwicklungsumgebung WebStorm bringt Erweiterungen fรผr Vite, Vue und Next.js


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Laravel + Vue 3 (Vite, TypeScript) SPA Setup


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ How To Create A Mobile App Using Vite, Vue and Ionic Capacitor In 8 Minutes Including Explanation


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Web Development with Vite, Vue, and Flask


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Using Google Gemini With Flask, Vue and Vite


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ How to create a vue project using vite


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Web Development with Vite, Vue, and Flask (Update)


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ > Dynamic SVG in Vue with Vite


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Vite orqali Vue loyiha qurish.


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Vite orqali Vue loyiha qurish.


๐Ÿ“ˆ 29.77 Punkte

๐Ÿ“Œ Enhance the drag and drop of your Vue 3 application with Vue Fluid DnD


๐Ÿ“ˆ 29.73 Punkte

๐Ÿ“Œ Unleash Your Creativity: Building Your First UI Component with ReactFlow Library


๐Ÿ“ˆ 27.16 Punkte

๐Ÿ“Œ Mastering Vue 3: A Comprehensive Guide to Building Modern Web Applications <Part 7 />


๐Ÿ“ˆ 26.87 Punkte

๐Ÿ“Œ Building a chat app: Chatrooms with Nodejs websockets and Vue (PART 2)


๐Ÿ“ˆ 26.87 Punkte











matomo