@tdesign/uniapp 图标瘦身

1. 背景

随着 tdesign-icons 图标的不断丰富,@tdesign/uniapp 包的图标文件大小也不断变大。但是普通业务一般并不需要同时使用这么多图标,并且小程序等场景图标只能使用 iconfont 等,不支持动态加载,所以需要一种方式减包、瘦身。

2. 解决方案

不同于原生小程序,@tdesign/uniapp 在上传前有构建一步,可以利用插件来减包。

可以使用postcss-plugin-remove-selector 来在构建时进行无用图标的删减。

点此查看文档

3. 如何使用

安装

bash 复制代码
pnpm add @novlan/postcss-plugin-remove-selector -D

3.1. 简化模式(推荐)

通过 mode 指定预设,只需关注 used/unused,无需手动配置 fileselectorPattern

ts 复制代码
import { defineConfig } from 'vite';
import { postcssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector';

export default defineConfig({
  css: {
    postcss: {
      plugins: [
        postcssPluginRemoveSelector({
          mode: 'tdesign',
          used: ['home', 'chat', 'user', 'add', 'search', 'close'],
        }),
      ],
    },
  },
});

使用 customUsed / customUnused 可在预设基础上增量追加,不会覆盖预设中已有的列表:

ts 复制代码
postcssPluginRemoveSelector({
  mode: 'tdesign',
  // 在 tdesign 预设默认的 used 列表上,额外追加 'star' 和 'heart'
  customUsed: ['star', 'heart'],
  // 从结果中额外移除 'loading'
  customUnused: ['loading'],
})

3.2. 标准模式

通过 list 数组传入完整配置,适用于需要匹配多个文件的复杂场景:

ts 复制代码
import { defineConfig } from 'vite';
import { postcssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector';
import {
  TDESIGN_ICON_REMOVE_SELECTOR
} from '@novlan/postcss-plugin-remove-selector/lib/tdesign-uniapp-icon';


export default defineConfig({
  css: {
    postcss: {
      plugins: [postcssPluginRemoveSelector(TDESIGN_ICON_REMOVE_SELECTOR)],
    },
  },
});

4. 类型

插件支持两种配置方式,传入 SimpleOptions(简化模式)或 Options(标准模式)均可。

4.1. SimpleOptions(简化模式)

属性 类型 是否必填 说明
mode 'tdesign' 预设模式,设置后自动使用对应的 fileselectorPattern 默认值
file `RegExp string`
used string[] 正在使用的图标名称列表,这些图标会被保留
unused string[] 未使用的图标名称列表,这些图标会被移除
customUsed string[] 增量追加到 used 列表(不覆盖预设或已有的 used
customUnused string[] 增量追加到 unused 列表(不覆盖预设或已有的 unused
selectorPattern RegExp 选择器匹配模式。使用 mode 时可省略
debug boolean 是否开启调试模式

modefile 至少需要指定一个。当 modefile/selectorPattern 同时指定时,file/selectorPattern 优先。

4.2. Options(标准模式)

属性 类型 是否必填 说明
list FileConfig[] 配置列表
debug boolean 是否开启调试模式

4.3. FileConfig

属性 类型 是否必填 说明
file `RegExp string`
used string[] 正在使用的图标名称列表,这些图标会被保留
unused string[] 未使用的图标名称列表,这些图标会被移除
customUsed string[] 增量追加到 used 列表(不覆盖已有的 used
customUnused string[] 增量追加到 unused 列表(不覆盖已有的 unused
selectorPattern RegExp 选择器匹配模式,只处理匹配该模式的选择器

4.4. 内置预设

mode 说明 默认 file 默认 selectorPattern
tdesign TDesign UniApp 图标减包 `/[@/]tdesign[/]uniapp[/]dist[/]icon[/]icon.[css vue]/`

5. 文档

  1. 文档
  2. 更新日志
  3. 示例项目

6. 效果

tdesign-uniapp-starter 为例,使用此插件,可使小程序大小减少138KB,约95.8%

使用前:

使用后:

相关推荐
IT_陈寒5 小时前
SpringBoot自动配置的坑差点让我加班到天亮
前端·人工智能·后端
xingpanvip5 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
@PHARAOH5 小时前
WHAT - GitLens supercharged 插件
前端
TT模板6 小时前
苹果cms整合西瓜播放器XGplayer插件支持跳过片头尾
前端·html5
Wect6 小时前
React 性能优化精讲
前端·react.js·性能优化
追风筝的人er7 小时前
SpringBoot+Vue3 企业考勤如何处理法定假期?节假日方案、调休补班与工作日判断链路拆解
前端·vue.js·后端
无敌的黑星星7 小时前
Java8 CompletableFuture 实战指南
linux·前端·python
雁鸣零落7 小时前
如何在 Chrome 中查看其他浏览器的书签?书签空间订阅与侧边栏只读切换指南
前端·chrome·edge浏览器
hpoenixf8 小时前
一天上线 + 零返工:我如何给复杂前端需求建立“安全感”
前端
广州华水科技9 小时前
单北斗GNSS变形监测系统在水利工程安全保障中的应用与优势分析
前端