当前位置: 首页 > news >正文

vite+tailwind封装组件库

前言

演示视频

https://www.bilibili.com/video/BV1EST3zPEyP/?spm_id_from=333.1387.homepage.video_card.click

参考

https://juejin.cn/post/7112295067682865166

https://juejin.cn/post/7046187185615142949

代码仓库

https://gitee.com/malguy/vite-components-lib-tutorial

初始化项目

创建vite

直接使用命令行

pnpm create vite react-components
cd react-components
pnpm i

你也可以用我的脚手架面板(推销一下😁🤓)

malred/cli-panel

无需联网, 因为是用文件复制的方法创建的

安装tailwind

使用tailwind v3

Installation - Tailwind CSS

pnpm install -D tailwindcss@3 postcss autoprefixer
# npx tailwindcss init -p
pnpm dlx tailwindcss@3 init -p

将路径添加到 **<font style="color:rgb(15, 23, 42);">tailwind.config.js</font>** 文件中的所有模板文件中。

/** @type {import('tailwindcss').Config} */
export default {content: ["./index.html",// 使用react时"./src/**/*.{js,ts,jsx,tsx}",// 使用vue时"./src/**/*.{vue,js,ts,jsx,tsx}",// 使用svelte时"./src/**/*.{svelte,js,ts,jsx,tsx}",],theme: {extend: {},},plugins: [],
}

将Tailwind每个层的 **<font style="color:rgb(15, 23, 42);">@tailwind</font>** 指令添加到您的 **<font style="color:rgb(15, 23, 42);">./src/app.css</font>** 文件中。

@tailwind base;
@tailwind components;
@tailwind utilities;

写一个button组件

src/components/Button/Button.jsx

// src/components/button/index.tsx
/** @Author WangZhiGang* @Date 2025-06-07 05:58:37* @Description */
import './button.css'import * as React from "react";interface Props {size: 'lg' | 'md' | 'sm'children: string | React.ReactNode
}const Button = ({size, children}: Props) => {const className = size + " base "return (<buttonclassName={className}>{children}</button>);
};export default Button;

如果觉得tailwind样式太长, 可以写一个css

button {&.base {@apply text-white bg-blue-500 rounded-md}&.sm {@apply text-xs p-2}&.md {@apply text-sm p-4}&.lg {@apply text-lg p-8}
}

在index.js中导出

// !!! 重要 包含@tailwind的css必须导出, 否则新项目会丢失变量 !!!
import './index.css'
import './App.css'import Button from "./components/button/index.jsx";export {Button as MalButton
}

修改vite配置文件

import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
// const path = require("path");const resolvePath = (str) => path.resolve(__dirname, str);// https://vitejs.dev/config/
export default defineConfig({plugins: [react()],// 防止 vite 将 rgba() 颜色转化为 #RGBA 十六进制cssTarget: 'chrome61',resolve: {alias: {"@": path.resolve(__dirname, "./src"),},},// 打包编译配置build: {rollupOptions: {// 请确保外部化那些你的库中不需要的依赖external: ["react", "react-dom"],output: {// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量globals: {react: "react","react-dom": "react-dom",},},},lib: {// 打包入口文件, 使用时从入口进行寻找依赖entry: resolvePath("src/index.js"),name: "mal-react-components",// 打包后文件名 format表示不同的规范(commonjs之类的) // 如果打包成 UMD 格式,文件名可能是 mal-vue3-components.umd.js。// 如果打包成 ES Module 格式,文件名可能是 mal-vue3-components.es.jsfileName: format => `mal-react-components.${format}.js`,},}
})
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
// import tailwindcss from 'tailwindcss';// https://vitejs.dev/config/
export default defineConfig({plugins: [vue()],// css: {//     postcss: {//         plugins: [tailwindcss],//     }// },// 打包编译配置build: {rollupOptions: {// 请确保外部化那些你的库中不需要的依赖external: ['vue'],output: {// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量globals: {vue: 'Vue',},},},lib: {// 打包入口文件, 使用时从入口进行寻找依赖entry: './src/index.js',// 名称name: 'mal-vue-components',// 打包后文件名 format表示不同的规范(commonjs之类的) // 如果打包成 UMD 格式,文件名可能是 mal-vue3-components.umd.js。// 如果打包成 ES Module 格式,文件名可能是 mal-vue3-components.es.jsfileName: (format) => `mal-vue3-components.${format}.js`,},}
})

如果使用的是ts

pnpm i @rollup/plugin-typescript tslib
# 在vite.config文件里使用node的依赖需要安装
pnpm i @types/node --save-dev
import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import typescript from '@rollup/plugin-typescript'
// 想在ts使用node的依赖, 需要安装 @types/node
import { resolve } from "node:path";const resolvePath = (str: string) => resolve(__dirname, str);// https://vitejs.dev/config/
export default defineConfig({plugins: [react()],resolve: {alias: {"@": resolve(__dirname, "./src"),},},// 打包编译配置build: {rollupOptions: {// 请确保外部化那些你的库中不需要的依赖external: ["react", "react-dom"],output: {// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量globals: {react: "react","react-dom": "react-dom",},},plugins: [typescript({target: "es2015", // 这里指定编译到的版本,rootDir: resolvePath("src/"),declaration: true,declarationDir: resolvePath("dist"),exclude: resolvePath("node_modules/**"),allowSyntheticDefaultImports: true,}),],},lib: {// 打包入口文件, 使用时从入口进行寻找依赖entry: resolvePath("src/index.ts"),name: "mal-react-components",// 打包后文件名 format表示不同的规范(commonjs之类的)// 如果打包成 UMD 格式,文件名可能是 mal-vue3-components.umd.js。// 如果打包成 ES Module 格式,文件名可能是 mal-vue3-components.es.jsfileName: format => `mal-react-components.${format}.js`,},},
})

打包 发布

pnpm build
pnpm publish

使用

在本地想测试, 可以用

# 在库的目录执行
npm link 
# 在要使用的目录中执行
npm link [自己写的库的名称]

但是我之前试了老是失败, 我的解决方法是直接复制打包后的文件到项目中, 然后从其中引入依赖


文章转载自:

http://cBGtcEvh.Lgmgn.cn
http://3zQiqO5B.Lgmgn.cn
http://5Hs2y3il.Lgmgn.cn
http://hvyHlfuO.Lgmgn.cn
http://h2ytlMx9.Lgmgn.cn
http://g3L0fvpZ.Lgmgn.cn
http://Rd4C53hU.Lgmgn.cn
http://9vwI1FZv.Lgmgn.cn
http://FmLWiDqn.Lgmgn.cn
http://GItudaFM.Lgmgn.cn
http://U01UFAE5.Lgmgn.cn
http://6RASUthp.Lgmgn.cn
http://7AWE1IAF.Lgmgn.cn
http://RWjPZkhl.Lgmgn.cn
http://C0CLWwlx.Lgmgn.cn
http://88I0j1fW.Lgmgn.cn
http://EtSxCy8e.Lgmgn.cn
http://LXHtf2VK.Lgmgn.cn
http://hybU4NXK.Lgmgn.cn
http://Mpe6wo3M.Lgmgn.cn
http://Sl6hjNK7.Lgmgn.cn
http://qSZVbgk3.Lgmgn.cn
http://JPfREc18.Lgmgn.cn
http://0Jqx7IYv.Lgmgn.cn
http://1IriC9ON.Lgmgn.cn
http://0G7lURoB.Lgmgn.cn
http://U78vT0y0.Lgmgn.cn
http://CsdKZkok.Lgmgn.cn
http://ssjmvPj9.Lgmgn.cn
http://i89HEhrt.Lgmgn.cn
http://www.dtcms.com/a/236414.html

相关文章:

  • Android和硬件通信
  • 408第一季 - 数据结构 - 字符串和KMP算法
  • phosphobot开源程序是控制您的 SO-100 和 SO-101 机器人并训练 VLA AI 机器人开源模型
  • ubuntu20使用自主探索算法explore_lite实现机器人自主探索导航建图
  • PGSR : 基于平面的高斯溅射高保真表面重建【全流程分析与测试!】【2025最新版!!】
  • 中山大学美团港科大提出首个音频驱动多人对话视频生成MultiTalk,输入一个音频和提示,即可生成对应唇部、音频交互视频。
  • 【python与生活】如何构建一个解读IPO招股书的算法?
  • 机器学习的数学基础:神经网络
  • PCA笔记
  • Now formdata是什么?如何使用
  • SAP学习笔记 - 开发27 - 前端Fiori开发 Routing and Navigation(路由和导航)
  • STM32学习笔记:定时器(TIM)原理与应用(详解篇)
  • Linux进程(中)
  • AI大神吴恩达-提示词课程笔记
  • LLM 笔记:Speculative Decoding 投机采样
  • python并发编程
  • 【力扣】2434.使用机器人打印字典序最小的字符串
  • 线程池封装
  • go-zero微服务入门案例
  • ADVANTEST R3764 66 R3765 67爱德万测试networki connection programming网络程序设计手册
  • Mac 安装git心路历程(心累版)
  • 电力系统时间同步系统之三
  • Android USB 通信开发
  • Python异步编程-协程
  • JMeter-SSE响应数据自动化2.0
  • 在 Linux 中修改 Apache HTTP Server(httpd)默认端口的完整指南
  • 基于库博Cobot进行二次规则开发实训
  • VScode打开后一直显示正在重新激活终端 问题的解决方法
  • 【优选算法】C++滑动窗口
  • 【Go语言基础【13】】函数、闭包、方法