《Vite 基础知识》基于 Vite 的组件库框架搭建

前言

《基于 Vite4 的 Vue3 项目创建(受 Nodejs 版本限制可参考)》有关联关系!
链接是项目框架搭建,此篇是组件库框架搭建!

开始

# 目录结构
.
├─ src              # Vue 组件库、指令等
|   ├─ components
|   └─ directive
├─ index.js         # 导出文件 
├─ package.json	    # 插件依赖
└─ vite.config.js	# vite 配置

package.json

注意插件都是固定版本,避免不可预知错误~ 还有别忘了 第 5 行 要设置 导出。

{
    "name": "my-test-webcomponents",
    "version": "999.99999.8",
    "type": "module",
    "main": "/dist/my-test-webcomponents.js",
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "preview": "vite preview"
    },
    "dependencies": {
        "@element-plus/icons-vue": "2.3.1",
        "axios": "1.6.5",
        "cnchar": "3.2.5",
        "echarts": "5.4.3",
        "element-plus": "2.5.1",
        "element-resize-detector": "1.2.4",
        "js-base64": "3.7.5",
        "js-cookie": "3.0.5",
        "js-md5": "0.8.3",
        "proj4": "2.6.3",
        "sortablejs": "1.15.1",
        "viewerjs": "1.11.6",
        "vue": "3.4.8",
        "vue-json-editor": "1.4.3",
        "vuex": "4.1.0",
        "wangeditor": "4.7.15",
        "xlsx": "0.18.5"
    },
    "devDependencies": {
        "@vitejs/plugin-vue": "4.6.2",
        "postcss-pxtorem": "6.0.0",
        "sass": "1.70.0",
        "vite": "4.5.1"
    }
}

vite.config.js

  • 代码第 7 行,必须设置 基于 Vite的 Vue 插件,否则编译报错 [vite:build-import-analysis]
  • 代码第 9 - 15 行,库构建配置
  • 代码第 16 - 25 行,rollup 配置,能解决错误 Cannot read properties of null (reading 'isCE')
  • 代码第 28 行,import 文件时可省略文件扩展名;
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
    plugins: [ 
        vue()
    ],
    build: {
        lib: {
            entry: resolve(__dirname, 'index.js'),
            fileName: 'my-test-webcomponents',
            name: 'my-test-webcomponents', // 暴露的全局变量
            // formats: ['es', 'umd'] // 默认输出格式
        },
        rollupOptions: {
            // 确保外部化处理那些你不想打包进库的依赖
            external: ['vue'], // 
            output: {
                // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量 https://cn.rollupjs.org/configuration-options/#output-globals
                globals: {
                    vue: 'Vue',
                },
            },
        },
    },
    resolve: {
        extensions: ['.vue', '.js', '.ts', '.jsx', '.tsx', '.json']
    },
})

index.js

差异

// 此处引入样式,Vite 会自动打包到 index.css 文件
import './src/assets/scss/main.scss'


/**
 * 要导出的组件、指令
 * @param {Object}  objs   对象列表
 * @param {String}  type 类型
 */
function exportModule(objs, type) {
    const modules = Object.keys(objs).reduce((obj, path) => {
        const name = path.replace(/(.*\/)*([^.]+).*/ig, "$2");
        obj[name] = objs[path].default;
        // console.log(name, modules[name])
        return obj;
    }, {})

    // 要导出的    
    let exportModules;
    switch (type) {
        case 'components':
            {
                exportModules = function install(app) {
                    // 组件框添加最小化按钮
                    for (let i in modules) {
                        app.component(i, modules[i]);
                    }
                }
                break;
            }
        case 'directive':
            {
                exportModules = function install(app) {
                    // 组件框添加最小化按钮
                    for (let i in modules) {
                        app.directive(i, modules[i]);
                    }
                }
                break;
            }
    }
    return exportModules;
}

// --- 组件 ---
const componentsList = import.meta.glob('./src/components/**/*.vue', { eager: true }); 
const MyTestComponents = exportModule(componentsList, 'components');

// --- 指令 ---
const directiveList = import.meta.glob('./src/directive/**/*.js', { eager: true }); 
const MyTestDirectives = exportModule(directiveList, 'directive');


export {
    MyTestComponents,
    MyTestDirectives 
}

npm 发布

参考《实战:如何使用Vue2.0开发一个npm组件库》- 1、npm 包:创建、发布、更新、删除和使用

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值