Skip to content

Commit b277e33

Browse files
committed
chore(remove-selector): add types
1 parent 5588451 commit b277e33

File tree

5 files changed

+155
-89
lines changed

5 files changed

+155
-89
lines changed
Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,33 @@
11
# postcss-plugin-remove-selector
22

3-
## 1.0.0 (2025-06-09)
3+
## <small>0.1.5 (2026-02-08)</small>
44

5-
* docs: update docs ([dc32fc4](https://github.com/novlan1/plugin-light/commits/dc32fc4))
6-
* docs: update docs ([47c235e](https://github.com/novlan1/plugin-light/commits/47c235e))
7-
* docs: update docs ([761f6c4](https://github.com/novlan1/plugin-light/commits/761f6c4))
8-
* docs: update docs ([6f3a885](https://github.com/novlan1/plugin-light/commits/6f3a885))
9-
* docs: update readme ([5483ce8](https://github.com/novlan1/plugin-light/commits/5483ce8))
10-
* chore: update license ([5ba3e91](https://github.com/novlan1/plugin-light/commits/5ba3e91))
11-
* refactor: use eslint import/order rule ([41afa85](https://github.com/novlan1/plugin-light/commits/41afa85))
12-
* feat(postcss-plugin-remove-selector): 增加press-icon-plus-remove-selector参数 ([837fc0a](https://github.com/novlan1/plugin-light/commits/837fc0a))
5+
* feat(remove-selector): add used/unused api ([68e1973](https://github.com/novlan1/plugins/commit/68e1973))
136

147

158

16-
## <small>0.0.5 (2025-03-18)</small>
9+
## <small>0.1.4 (2026-02-08)</small>
1710

18-
* feat(postcss-plugin-remove-selector): 异常参数兼容 ([91fd8a5](https://github.com/novlan1/plugin-light/commits/91fd8a5))
11+
* feat(remove-selector): add tdesignUsedIcon exports ([e9fbfc3](https://github.com/novlan1/plugins/commit/e9fbfc3))
1912

2013

2114

22-
## <small>0.0.4 (2025-03-18)</small>
15+
## <small>0.1.3 (2026-02-08)</small>
2316

24-
* feat(postcss-plugin-remove-selector): 优化api ([f6f3c3f](https://github.com/novlan1/plugin-light/commits/f6f3c3f))
25-
* chore: 优化版本号 ([bf5a4cb](https://github.com/novlan1/plugin-light/commits/bf5a4cb))
26-
* chore: sort package.json ([42cab1e](https://github.com/novlan1/plugin-light/commits/42cab1e))
27-
* chore: update docs ([43dbc9d](https://github.com/novlan1/plugin-light/commits/43dbc9d))
28-
* chore: update license ([7f6a9d9](https://github.com/novlan1/plugin-light/commits/7f6a9d9))
29-
* docs: update docs ([cc81245](https://github.com/novlan1/plugin-light/commits/cc81245))
17+
* feat(remove-selector): support postcss@7 ([615fbd8](https://github.com/novlan1/plugins/commit/615fbd8))
3018

3119

3220

33-
## <small>0.0.3 (2024-11-24)</small>
21+
## <small>0.1.2 (2026-02-08)</small>
3422

35-
* chore: add meta in pkg.json ([3f6088c](https://github.com/novlan1/plugin-light/commits/3f6088c))
36-
* chore: add release script ([defe6af](https://github.com/novlan1/plugin-light/commits/defe6af))
37-
* chore: update deps to latest ([455918d](https://github.com/novlan1/plugin-light/commits/455918d))
38-
* chore: update name ([02b2100](https://github.com/novlan1/plugin-light/commits/02b2100))
39-
* chore: update plugin-light-shared ([08a9790](https://github.com/novlan1/plugin-light/commits/08a9790))
40-
* chore: update plugin-light-shared to 0.0.12 ([4d33550](https://github.com/novlan1/plugin-light/commits/4d33550))
41-
* docs: move docs position ([1937e17](https://github.com/novlan1/plugin-light/commits/1937e17))
42-
* docs: update docs ([883589c](https://github.com/novlan1/plugin-light/commits/883589c))
23+
* feat(remove-selector): add tdesign-config exports ([ab9c52a](https://github.com/novlan1/plugins/commit/ab9c52a))
4324

4425

4526

46-
## <small>0.0.2 (2024-09-18)</small>
27+
## <small>0.1.1 (2026-02-08)</small>
4728

48-
* chore: update project-config ([af4ff17](https://github.com/novlan1/plugin-light/commits/af4ff17))
49-
* chore: update rollup.config ([19fc828](https://github.com/novlan1/plugin-light/commits/19fc828))
50-
* feat: add monorepo packages ([494adb6](https://github.com/novlan1/plugin-light/commits/494adb6))
29+
* chore: update plugin docs ([19ca7f4](https://github.com/novlan1/plugins/commit/19ca7f4))
30+
* init ([fc48bfe](https://github.com/novlan1/plugins/commit/fc48bfe))
5131

5232

5333

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# 移除选择器
22

33
<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">
1010
</p>
1111

1212
可用于移除三方库中的不需要的样式,从而减小包体积。
@@ -20,73 +20,119 @@
2020
安装
2121

2222
```bash
23-
pnpm add @plugin-light/postcss-plugin-remove-selector -D
23+
pnpm add @novlan/postcss-plugin-remove-selector -D
2424
```
2525

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` 可在预设基础上增量追加,不会覆盖预设中已有的列表:
2749

2850
```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+
})
4958
```
5059

51-
也可以在 `vite.config.ts` 中使用:
60+
### 标准模式
61+
62+
通过 `list` 数组传入完整配置,适用于需要匹配多个文件的复杂场景:
5263

5364
```ts
5465
import { defineConfig } from 'vite';
55-
import removeSelector from '@plugin-light/postcss-plugin-remove-selector';
66+
import { postcssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector';
5667
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';
5970

6071

6172
export default defineConfig({
6273
css: {
6374
postcss: {
64-
plugins: [removeSelector(PRESS_ICON_PLUS_REMOVE_SELECTOR)],
75+
plugins: [postcssPluginRemoveSelector(TDESIGN_ICON_REMOVE_SELECTOR)],
6576
},
6677
},
6778
});
6879
```
6980

7081
## 3. 类型
7182

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$/` |
81123

82124
## 4. 更新日志
83125

84-
[点此查看](../changelog/postcss-plugin-remove-selector.md)
126+
[点此查看](https://novlan1.github.io/plugins/changelog/postcss-plugin-remove-selector.html)
85127

86128
## 5. 效果
87129

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">
89135

90-
<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_5110452c29f3a22c3a.png" width="600">
136+
使用后:
91137

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">

packages/postcss-plugin-remove-selector/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ export default defineConfig({
123123

124124
## 4. 更新日志
125125

126-
[点此查看](./CHANGELOG.md)
126+
[点此查看](https://novlan1.github.io/plugins/changelog/postcss-plugin-remove-selector.html)
127127

128128
## 5. 效果
129129

130-
使用此插件,可使小程序大小减少`15KB`
130+
[tdesign-uniapp-starter](https://github.com/TDesignOteam/tdesign-uniapp-starter) 为例,使用此插件,可使小程序大小减少`138KB`,约`95.8%`
131131

132-
<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_5110452c29f3a22c3a.png" width="600">
132+
使用前:
133133

134-
<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_d5703094c04270a6d5.png" width="600">
134+
<img src="https://cdn.uwayfly.com/article/2026/2/own_mike_4w2yXDz3tMcaJ2nx.png" width="600">
135+
136+
使用后:
137+
138+
<img src="https://cdn.uwayfly.com/article/2026/2/own_mike_dsdB5TmAaedmkPr5.png" width="600">

packages/postcss-plugin-remove-selector/src/plugin.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { shouldHandleFile, shouldRemoveRule } from './helper';
22
import { PRESETS } from './presets';
33

4-
import type { Options, SimpleOptions } from './types';
4+
import type { Options, SimpleOptions, PostCSSRoot, PostCSSResult, PostCSSRule, PluginCreator } from './types';
55

6-
export type { Options, SimpleOptions, FileConfig, ShouldRemoveRuleOptions, PresetMode, Preset } from './types';
6+
export type { Options, SimpleOptions, FileConfig, ShouldRemoveRuleOptions, PresetMode, Preset, PostCSSRoot, PostCSSResult, PostCSSRule, PostCSSPlugin, PluginCreator } from './types';
77
export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
88

99
const PLUGIN_NAME = 'postcss-plugin-remove-selector';
@@ -76,7 +76,7 @@ function normalizeOptions(opts: Options | SimpleOptions): Options {
7676
/**
7777
* 核心处理逻辑,PostCSS 7 / 8 共用
7878
*/
79-
function processRoot(root: any, result: any, opts: Options) {
79+
function processRoot(root: PostCSSRoot, result: PostCSSResult, opts: Options) {
8080
const { list = [], debug = false } = opts;
8181
const fileName = result.opts?.from || '';
8282
const found = shouldHandleFile(list, fileName);
@@ -97,7 +97,7 @@ function processRoot(root: any, result: any, opts: Options) {
9797

9898
let removedCount = 0;
9999

100-
root.walkRules((rule: any) => {
100+
root.walkRules((rule: PostCSSRule) => {
101101
if (shouldRemoveRule({
102102
selectorPattern,
103103
used: mergedUsed,
@@ -129,11 +129,11 @@ function processRoot(root: any, result: any, opts: Options) {
129129
* @param opts 配置项
130130
* @returns PostCSS 插件
131131
*/
132-
const postcssPluginRemoveSelector: any = (opts: Options | SimpleOptions = { list: [] }) => {
132+
const postcssPluginRemoveSelector: PluginCreator = (opts: Options | SimpleOptions = { list: [] }) => {
133133
const normalizedOpts = normalizeOptions(opts);
134134
return {
135135
postcssPlugin: PLUGIN_NAME,
136-
Once(root: any, { result }: any) {
136+
Once(root: PostCSSRoot, { result }: { result: PostCSSResult }) {
137137
processRoot(root, result, normalizedOpts);
138138
},
139139
};
@@ -149,13 +149,13 @@ try {
149149
if (postcss && typeof postcss.plugin === 'function') {
150150
postcssPluginRemoveSelector.postcss7 = postcss.plugin(
151151
PLUGIN_NAME,
152-
(opts: Options | SimpleOptions = { list: [] }) => (root: any, result: any) => {
152+
(opts: Options | SimpleOptions = { list: [] }) => (root: PostCSSRoot, result: PostCSSResult) => {
153153
const normalizedOpts = normalizeOptions(opts);
154154
processRoot(root, result, normalizedOpts);
155155
},
156156
);
157157
}
158-
} catch (e) {
158+
} catch {
159159
// postcss 未安装或不支持 postcss.plugin,忽略
160160
}
161161

packages/postcss-plugin-remove-selector/src/types.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,40 @@ export interface ShouldRemoveRuleOptions {
8282
selector: string;
8383
}
8484

85+
/**
86+
* PostCSS 兼容类型定义
87+
* 兼容 PostCSS 7 和 PostCSS 8,避免直接依赖 postcss 包的类型
88+
*/
89+
90+
/** PostCSS Rule 节点(兼容 v7/v8) */
91+
export interface PostCSSRule {
92+
selector: string;
93+
remove(): void;
94+
}
95+
96+
/** PostCSS Root 节点(兼容 v7/v8) */
97+
export interface PostCSSRoot {
98+
walkRules(callback: (rule: PostCSSRule) => void): void;
99+
}
100+
101+
/** PostCSS Result 对象(兼容 v7/v8) */
102+
export interface PostCSSResult {
103+
opts?: {
104+
from?: string;
105+
};
106+
}
107+
108+
/** PostCSS 8 插件对象 */
109+
export interface PostCSSPlugin {
110+
postcssPlugin: string;
111+
Once(root: PostCSSRoot, helpers: { result: PostCSSResult }): void;
112+
}
113+
114+
/** PostCSS 插件创建函数(含 postcss 标记和 postcss7 兼容) */
115+
export interface PluginCreator {
116+
(opts?: Options | SimpleOptions): PostCSSPlugin;
117+
postcss: true;
118+
postcss7?: (opts?: Options | SimpleOptions) => (root: PostCSSRoot, result: PostCSSResult) => void;
119+
}
120+
85121

0 commit comments

Comments
 (0)