|
1 | 1 | # 移除选择器 |
2 | 2 |
|
3 | 3 | <p align="center"> |
4 | | - <img src="https://img.shields.io/npm/dw/@plugin-light/postcss-plugin-remove-selector"> |
5 | | - <img src="https://img.shields.io/npm/unpacked-size/@plugin-light/postcss-plugin-remove-selector"> |
6 | | - <img src="https://img.shields.io/npm/v/@plugin-light/postcss-plugin-remove-selector"> |
7 | | - <img src="https://img.shields.io/npm/l/@plugin-light/postcss-plugin-remove-selector"> |
8 | | - <img src="https://img.shields.io/github/last-commit/novlan1/plugin-light"> |
9 | | - <img src="https://img.shields.io/github/created-at/novlan1/plugin-light"> |
| 4 | + <img src="https://img.shields.io/npm/dw/@novlan/postcss-plugin-remove-selector"> |
| 5 | + <img src="https://img.shields.io/npm/unpacked-size/@novlan/postcss-plugin-remove-selector"> |
| 6 | + <img src="https://img.shields.io/npm/v/@novlan/postcss-plugin-remove-selector"> |
| 7 | + <img src="https://img.shields.io/npm/l/@novlan/postcss-plugin-remove-selector"> |
| 8 | + <img src="https://img.shields.io/github/last-commit/novlan1/plugins"> |
| 9 | + <img src="https://img.shields.io/github/created-at/novlan1/plugins"> |
10 | 10 | </p> |
11 | 11 |
|
12 | 12 | 可用于移除三方库中的不需要的样式,从而减小包体积。 |
|
20 | 20 | 安装 |
21 | 21 |
|
22 | 22 | ```bash |
23 | | -pnpm add @plugin-light/postcss-plugin-remove-selector -D |
| 23 | +pnpm add @novlan/postcss-plugin-remove-selector -D |
24 | 24 | ``` |
25 | 25 |
|
26 | | -`postcss.config.js` 中新增配置: |
| 26 | +### 简化模式(推荐) |
| 27 | + |
| 28 | +通过 `mode` 指定预设,只需关注 `used`/`unused`,无需手动配置 `file` 和 `selectorPattern`: |
| 29 | + |
| 30 | +```ts |
| 31 | +import { defineConfig } from 'vite'; |
| 32 | +import { postcssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector'; |
| 33 | + |
| 34 | +export default defineConfig({ |
| 35 | + css: { |
| 36 | + postcss: { |
| 37 | + plugins: [ |
| 38 | + postcssPluginRemoveSelector({ |
| 39 | + mode: 'tdesign', |
| 40 | + used: ['home', 'chat', 'user', 'add', 'search', 'close'], |
| 41 | + }), |
| 42 | + ], |
| 43 | + }, |
| 44 | + }, |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +使用 `customUsed` / `customUnused` 可在预设基础上增量追加,不会覆盖预设中已有的列表: |
27 | 49 |
|
28 | 50 | ```ts |
29 | | -module.exports = { |
30 | | - require('@plugin-light/postcss-plugin-remove-selector/lib/index')({ |
31 | | - list: [{ |
32 | | - file: new RegExp('press-ui/press-icon-plus/css/icon.scss'), |
33 | | - include: [ |
34 | | - 'arrow', |
35 | | - 'arrow-left', |
36 | | - 'arrow-right', |
37 | | - 'arrow-up', |
38 | | - 'arrow-down', |
39 | | - 'success', |
40 | | - 'cross', |
41 | | - 'plus', |
42 | | - 'minus', |
43 | | - 'fail', |
44 | | - 'circle', |
45 | | - ].map(item => `.press-icon-plus-${item}:before`), |
46 | | - }], |
47 | | - }), |
48 | | -} |
| 51 | +postcssPluginRemoveSelector({ |
| 52 | + mode: 'tdesign', |
| 53 | + // 在 tdesign 预设默认的 used 列表上,额外追加 'star' 和 'heart' |
| 54 | + customUsed: ['star', 'heart'], |
| 55 | + // 从结果中额外移除 'loading' |
| 56 | + customUnused: ['loading'], |
| 57 | +}) |
49 | 58 | ``` |
50 | 59 |
|
51 | | -也可以在 `vite.config.ts` 中使用: |
| 60 | +### 标准模式 |
| 61 | + |
| 62 | +通过 `list` 数组传入完整配置,适用于需要匹配多个文件的复杂场景: |
52 | 63 |
|
53 | 64 | ```ts |
54 | 65 | import { defineConfig } from 'vite'; |
55 | | -import removeSelector from '@plugin-light/postcss-plugin-remove-selector'; |
| 66 | +import { postcssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector'; |
56 | 67 | import { |
57 | | - PRESS_ICON_PLUS_REMOVE_SELECTOR |
58 | | -} from '@plugin-light/postcss-plugin-remove-selector/lib/press-ui-icon-plus'; |
| 68 | + TDESIGN_ICON_REMOVE_SELECTOR |
| 69 | +} from '@novlan/postcss-plugin-remove-selector/lib/tdesign-uniapp-icon'; |
59 | 70 |
|
60 | 71 |
|
61 | 72 | export default defineConfig({ |
62 | 73 | css: { |
63 | 74 | postcss: { |
64 | | - plugins: [removeSelector(PRESS_ICON_PLUS_REMOVE_SELECTOR)], |
| 75 | + plugins: [postcssPluginRemoveSelector(TDESIGN_ICON_REMOVE_SELECTOR)], |
65 | 76 | }, |
66 | 77 | }, |
67 | 78 | }); |
68 | 79 | ``` |
69 | 80 |
|
70 | 81 | ## 3. 类型 |
71 | 82 |
|
72 | | -```ts |
73 | | -interface Options { |
74 | | - list: Array<{ |
75 | | - file: RegExp | string; |
76 | | - exclude?: string[]; |
77 | | - include?: string[]; |
78 | | - }>; |
79 | | -} |
80 | | -``` |
| 83 | +插件支持两种配置方式,传入 `SimpleOptions`(简化模式)或 `Options`(标准模式)均可。 |
| 84 | + |
| 85 | +### SimpleOptions(简化模式) |
| 86 | + |
| 87 | +| 属性 | 类型 | 是否必填 | 说明 | |
| 88 | +| --- | --- | --- | --- | |
| 89 | +| `mode` | `'tdesign'` | 否 | 预设模式,设置后自动使用对应的 `file` 和 `selectorPattern` 默认值 | |
| 90 | +| `file` | `RegExp \| string` | 否 | 文件匹配规则。使用 `mode` 时可省略 | |
| 91 | +| `used` | `string[]` | 否 | 正在使用的图标名称列表,这些图标会被保留 | |
| 92 | +| `unused` | `string[]` | 否 | 未使用的图标名称列表,这些图标会被移除 | |
| 93 | +| `customUsed` | `string[]` | 否 | 增量追加到 `used` 列表(不覆盖预设或已有的 `used`) | |
| 94 | +| `customUnused` | `string[]` | 否 | 增量追加到 `unused` 列表(不覆盖预设或已有的 `unused`) | |
| 95 | +| `selectorPattern` | `RegExp` | 否 | 选择器匹配模式。使用 `mode` 时可省略 | |
| 96 | +| `debug` | `boolean` | 否 | 是否开启调试模式 | |
| 97 | + |
| 98 | +> `mode` 和 `file` 至少需要指定一个。当 `mode` 和 `file`/`selectorPattern` 同时指定时,`file`/`selectorPattern` 优先。 |
| 99 | +
|
| 100 | +### Options(标准模式) |
| 101 | + |
| 102 | +| 属性 | 类型 | 是否必填 | 说明 | |
| 103 | +| --- | --- | --- | --- | |
| 104 | +| `list` | `FileConfig[]` | 是 | 配置列表 | |
| 105 | +| `debug` | `boolean` | 否 | 是否开启调试模式 | |
| 106 | + |
| 107 | +### FileConfig |
| 108 | + |
| 109 | +| 属性 | 类型 | 是否必填 | 说明 | |
| 110 | +| --- | --- | --- | --- | |
| 111 | +| `file` | `RegExp \| string` | 是 | 文件匹配规则,可以是字符串或正则表达式 | |
| 112 | +| `used` | `string[]` | 否 | 正在使用的图标名称列表,这些图标会被保留 | |
| 113 | +| `unused` | `string[]` | 否 | 未使用的图标名称列表,这些图标会被移除 | |
| 114 | +| `customUsed` | `string[]` | 否 | 增量追加到 `used` 列表(不覆盖已有的 `used`) | |
| 115 | +| `customUnused` | `string[]` | 否 | 增量追加到 `unused` 列表(不覆盖已有的 `unused`) | |
| 116 | +| `selectorPattern` | `RegExp` | 否 | 选择器匹配模式,只处理匹配该模式的选择器 | |
| 117 | + |
| 118 | +### 内置预设 |
| 119 | + |
| 120 | +| mode | 说明 | 默认 file | 默认 selectorPattern | |
| 121 | +| --- | --- | --- | --- | |
| 122 | +| `tdesign` | TDesign UniApp 图标减包 | `/[@/]tdesign[/]uniapp[/]dist[/]icon[/]icon\.[css\|vue]/` | `/^\.t-icon-[\w-]+:before$/` | |
81 | 123 |
|
82 | 124 | ## 4. 更新日志 |
83 | 125 |
|
84 | | -[点此查看](../changelog/postcss-plugin-remove-selector.md) |
| 126 | +[点此查看](https://novlan1.github.io/plugins/changelog/postcss-plugin-remove-selector.html) |
85 | 127 |
|
86 | 128 | ## 5. 效果 |
87 | 129 |
|
88 | | -使用此插件,可使小程序大小减少`15KB`。 |
| 130 | +以 [tdesign-uniapp-starter](https://github.com/TDesignOteam/tdesign-uniapp-starter) 为例,使用此插件,可使小程序大小减少`138KB`,约`95.8%`。 |
| 131 | + |
| 132 | +使用前: |
| 133 | + |
| 134 | +<img src="https://cdn.uwayfly.com/article/2026/2/own_mike_4w2yXDz3tMcaJ2nx.png" width="600"> |
89 | 135 |
|
90 | | -<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_5110452c29f3a22c3a.png" width="600"> |
| 136 | +使用后: |
91 | 137 |
|
92 | | -<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_d5703094c04270a6d5.png" width="600"> |
| 138 | +<img src="https://cdn.uwayfly.com/article/2026/2/own_mike_dsdB5TmAaedmkPr5.png" width="600"> |
0 commit comments