🍃前端项目常用的优秀插件指南

136 阅读3分钟

🛠 一、构建与开发工具类

1. Vite - 下一代前端构建工具

官方中文文档:Vite | 下一代的前端工具链

定位:基于原生 ESM 的现代化前端构建工具,由 Vue 作者尤雨溪开发

优势

  • ⚡️ 极速冷启动:利用浏览器原生 ES 模块加载,无需打包整个应用
  • 🔥 毫秒级HMR:热更新速度不受项目规模影响
  • 🧩 丰富插件生态:支持 Rollup 插件体系
  • 🌐 框架无关:完美支持 Vue、React、Svelte 等

对比 Webpack

官方中文文档:webpack | webpack中文文档 | webpack中文网

项目规模冷启动时间HMR更新速度
小型项目 (<100个模块)Vite: 50-100ms Webpack: 3-10秒Vite: <10ms Webpack: 0.5-2秒
中型项目 (100-1000模块)Vite: 100-300ms Webpack: 10-30秒Vite: 10-30ms Webpack: 1-3秒
大型项目 (>1000模块)Vite: 300ms-1s Webpack: 30-120秒Vite: 30-100ms Webpack: 3-10秒

安装与使用:

# 创建 Vite 项目
npm create vite@latest my-project -- --template react-ts
cd my-project
npm install
npm run dev

创建其他项目

  • vanilla
  • vanilla-ts
  • vue
  • vue-ts
  • react
  • react-ts
  • react-swc
  • react-swc-ts
  • preact
  • preact-ts
  • lit
  • lit-ts
  • svelte
  • svelte-ts
  • solid
  • solid-ts
  • qwik
  • qwik-ts

配置文件(vite.config.ts):

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    open: true
  },
  build: {
    outDir: 'dist',
    assetsDir: 'static'
  }
})

2. unplugin - 统一插件系统

  • unplugin-vue-components

    用于自动导入组件(Element Plus 或 Ant Design Vue 的组件)

  • unplugin-auto-import

    用于自动导入 Vue 的 composition API(如 ref, computed, onMounted)以及 lodash 的函数(如 omit, pick)

定位:跨构建工具的统一插件开发体系

优势:一套代码兼容 Vite/Rollup/Webpack 等构建工具

场景:开发跨构建工具的插件(如 unplugin-auto-import 自动导入)

  • vite.config.ts 配置示例

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';

export default defineConfig({
  plugins: [
    vue(),
    // 自动导入 API(Vue hooks, lodash 等)
    AutoImport({
      imports: [
        'vue',          // 自动导入 Vue 组合式 API
        'lodash-es',    // 自动导入 lodash 函数
      ],
      // 自定义解析规则
      resolvers: [
        ElementPlusResolver(), // Element Plus 的 API 自动导入(可选)
      ],
      dts: 'src/auto-imports.d.ts', // 生成类型声明文件
      eslintrc: {
        enabled: true, // 生成 .eslintrc-auto-import.json 解决 ESLint 报错
      },
    }),
    // 自动导入组件(Element Plus, Ant Design Vue, 自定义组件等)
    Components({
      resolvers: [
        // Element Plus 组件自动导入
        ElementPlusResolver(),
        // Ant Design Vue 组件自动导入(按需加载样式)
        AntDesignVueResolver({
          importStyle: 'less', // 使用 less 主题(需安装 less,Vite 默认支持)
          resolveIcons: true,  // 自动导入图标
        }),
      ],
      dts: 'src/components.d.ts', // 组件类型声明文件
    }),
  ],
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true, // Ant Design 需要此配置
      },
    },
  },
});

3. VueUse - Vue 组合式 API 工具库

官方中文文档 VueUse 中文网

VueUse 是一个基于 Vue Composition API 的实用函数集合库,由 Vue.js 核心团队成员 Anthony Fu 创建和维护。它提供了 200+ 个开箱即用的组合式函数,覆盖了:

  • 状态管理
  • 浏览器 API
  • 传感器(设备交互)
  • 动画
  • 网络请求
  • 工具函数
  • 等常见开发场景

使用步骤

  • 安装
npm install @vueuse/core
  • 使用示例
<script setup>
import { useMouse, useLocalStorage } from '@vueuse/core'

// 鼠标位置跟踪
const { x, y } = useMouse()

// 本地存储状态
const count = useLocalStorage('my-count', 0)
</script>

<template>
  <div>鼠标位置: {{ x }}, {{ y }}</div>
  <button @click="count++">计数: {{ count }}</button>
</template>

<script setup lang="ts">
import { ExpandOutlined, CompressOutlined } from '@ant-design/icons-vue';
import { useFullscreen } from '@vueuse/core';

const { isFullscreen, toggle } = useFullscreen();
</script>

<template>
  <component @click="toggle" :is="isFullscreen ? CompressOutlined : ExpandOutlined" />
</template>

✨ 二、样式与 UI 工具

1. UnoCSS - 原子化 CSS 引擎

UnoCSS 官方文档:  unocss.dev

  • 安装:  pnpm add -D unocss

    定位:原子化 CSS 引擎

    优势:按需生成 CSS,体积比 Tailwind 小 10 倍,支持即时预览

  • 配置:  在 vite.config.ts 文件中配置如下

import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    UnoCSS(),
  ],
})

  • 创建 uno.config.ts 文件
import { defineConfig, presetAttributify, transformerDirectives, presetWind3 } from 'unocss';

export default defineConfig({
  content: {
    pipeline: {
      include: [/\.(vue|ts|js|tsx|jsx)$/],
      exclude: ['node_modules/**', 'dist/**', '.git/**'],
    },
  },
  presets: [presetWind3(), presetAttributify()],
  transformers: [transformerDirectives()],
});
  • 将 virtual:uno.css main.ts
// main.ts
import 'virtual:uno.css'

使用前

<template>
  <table class="data-table">
   <thead>
      <tr>
        <th>标题</th>
        <!-- 更多表头 -->
      </tr>
    </thead>
  </table>
</template>

<style scoped>
.data-table {
  border: 1px solid #e5e7eb;
  border-collapse: collapse;
  width: 100%;
}

.data-table th {
  background-color: #f3f4f6;
  padding: 12px 16px;
  text-align: left;
}

/* 更多样式... */
</style>

使用后

<template>
  <table 
       border="1" 
       class="border-gray-200 w-full collapse"
    >
    <thead>
      <tr>
        <th class="bg-gray-100 p-3 text-left">标题</th>
        <!-- 更多表头 -->
      </tr>
    </thead>
  </table>
</template>

  • 对比传统 CSS 方案
指标UnoCSSTailwind传统 CSS
生产体积2-5KB45-80KB100KB+
HMR速度即时快速
学习曲线

2. Material Icons - 现代化图标库

定位:Google 开源的矢量图标解决方案 优势

  • 🎭 3000+精心设计的图标
  • 🖌 SVG矢量格式,任意缩放不失真
  • 🌈 支持主题色动态切换
  • 📦 多种集成方式(字体/SVG/组件)

安装与使用:

npm i @material-design-icons/font

在CSS中引入:

@import '@material-design-icons/font';

🔍 三、代码质量与规范

  1. oxlint

    定位:Rust编写的高性能Linter

    优势:比ESLint快50-100倍,内置TypeScript支持

    场景:替代ESLint进行快速代码检查

  2. Prettier

定位:JavaScript代码检查

常用插件:eslint-plugin-import(导入规范),eslint-config-airbnb(流行代码规范)

3. Biome - 一体化前端工具链

定位:Rust编写的All-in-One前端工具链
优势

  • 🚀 高性能:替代ESLint + Prettier + Babel
  • 🔋 电池内置:格式化、linting、编译一体化
  • 📦 零配置:开箱即用

安装与使用:

npm install --save-dev @biomejs/biome

添加脚本命令:

{
  "scripts": {
    "format": "biome format . --write",
    "lint": "biome lint ."
  }
}

配置文件(biome.json):

{
  "formatter": {
    "lineWidth": 120,
    "indentStyle": "space"
  },
  "linter": {
    "rules": {
      "recommended": true,
      "complexity": {
        "noUselessSwitchCase": "error"
      }
    }
  }
}

⚙️ 四、数据处理工具

1. dayjs - 轻量日期库

定位:Moment.js的现代替代品

优势

  • 📦 2KB超小体积(Moment.js 16倍大)
  • 📅 相同API:降低迁移成本
  • 🧊 不可变数据:避免副作用
  • 🚀 性能提升:操作速度快2倍

安装与使用:

npm install dayjs

基础用法:

import dayjs from 'dayjs'

// 日期格式化
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss')) // 2023-10-01 14:30:45

2. lodash-es - JavaScript 工具库

定位:JavaScript实用工具库的ES模块版本
优势

  • 📦 模块化导入:Tree-shaking友好
  • 🧩 200+实用函数:数组/对象/字符串操作
  • ⚡ 性能优化:高频操作优化实现

安装与使用:

npm install lodash-es

按需导入示例:

import cloneDeep from 'lodash-es/cloneDeep'
import throttle from 'lodash-es/throttle'
import { groupBy } from 'lodash-es'

// 深拷贝对象
const original = { a: 1, nested: { b: 2 } }
const cloned = cloneDeep(original)

// 节流函数
const throttledScroll = throttle(() => {
  console.log('滚动处理')
}, 200)
window.addEventListener('scroll', throttledScroll)

// 数据分组
const users = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Eve', age: 25 }
]
const grouped = groupBy(users, 'age')
/* 结果:
{
  "25": [{name:"Alice",age:25}, {name:"Eve",age:25}],
  "30": [{name:"Bob",age:30}]
}
*/

对比原生操作:

// 原生深拷贝(局限)
const nativeClone = JSON.parse(JSON.stringify(original))

// 原生节流(复杂实现)
let lastTime = 0
function nativeThrottle(fn, delay) {
  return function() {
    const now = Date.now()
    if (now - lastTime >= delay) {
      fn.apply(this, arguments)
      lastTime = now
    }
  }
}

📦 五、包管理与优化

1. pnpm - 高效包管理器

定位:快速、节省磁盘空间的包管理工具
优势

  • 💾 节省磁盘空间:相同依赖只存储一次(node_modules减小50%)
  • 🚀 安装速度快:比npm/yarn快2倍
  • 🛡️ 避免幽灵依赖:严格依赖隔离
  • 📦 Monorepo支持:内置workspace功能

安装与迁移:

# 全局安装pnpm
npm install -g pnpm

# 迁移现有项目
pnpm import  # 从package-lock.json或yarn.lock迁移

2. Commitlint - 提交信息规范

定位:Git提交信息校验工具
优势

  • 📝 标准化提交信息:采用 Conventional Commits 规范
  • 🔍 自动生成CHANGELOG:语义化提交信息可解析
  • 🚫 阻止不规范提交:预提交钩子校验

安装与配置:

npm install @commitlint/cli @commitlint/config-conventional -D

创建配置文件(.commitlintrc.js):

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2,
      'always',
      ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'revert']
    ],
    'subject-case': [0] // 不限制大小写
  }
}

添加 Husky 钩子:

npx husky init
npm install husky -D
npx husky add .husky/commit-msg 'npx commitlint --edit $1'

提交类型说明:

类型说明
feat新增功能
fix修复bug
docs文档更新
style代码格式调整
refactor代码重构(非功能修改)
test测试用例相关
chore构建过程或工具链修改
revert回滚提交