@novlan/postcss-plugin-remove-selector 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,13 +1,3 @@
1
- import type { Options, PostCSSPlugin } from './types';
2
- export type { Options, FileConfig, ShouldRemoveRuleOptions, PostCSSPlugin } from './types';
3
- export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
4
- /**
5
- * PostCSS 插件:移除指定的 CSS 选择器
6
- * 用于图标样式减包等场景
7
- *
8
- * @param opts 配置项
9
- * @returns PostCSS 插件
10
- */
11
- export declare function postCssPluginRemoveSelector(opts?: Options): PostCSSPlugin & {
12
- postcss: true;
13
- };
1
+ export * from './plugin';
2
+ export { TDESIGN_ICON_REMOVE_SELECTOR } from './tdesign-uniapp-icon';
3
+ export { postCssPluginRemoveSelector as default } from './plugin';
package/lib/index.js CHANGED
@@ -48,48 +48,161 @@ function shouldRemoveRule(options) {
48
48
  return false;
49
49
  }
50
50
 
51
+ const PLUGIN_NAME = 'postcss-plugin-remove-selector';
52
+ /**
53
+ * 核心处理逻辑,PostCSS 7 / 8 共用
54
+ */
55
+ function processRoot(root, result, opts) {
56
+ const { list = [], debug = false } = opts;
57
+ const fileName = result.opts?.from || '';
58
+ const found = shouldHandleFile(list, fileName);
59
+ if (!found) {
60
+ return;
61
+ }
62
+ const { exclude = [], include = [], selectorPattern } = found;
63
+ if (debug) {
64
+ console.log('[postcss-plugin-remove-selector] handling:', fileName);
65
+ }
66
+ let removedCount = 0;
67
+ root.walkRules((rule) => {
68
+ if (shouldRemoveRule({
69
+ selectorPattern,
70
+ exclude,
71
+ include,
72
+ selector: rule.selector,
73
+ })) {
74
+ rule.remove();
75
+ removedCount += 1;
76
+ }
77
+ });
78
+ if (debug) {
79
+ console.log(`[postcss-plugin-remove-selector] removed ${removedCount} rules from:`, fileName);
80
+ }
81
+ }
51
82
  /**
52
83
  * PostCSS 插件:移除指定的 CSS 选择器
53
84
  * 用于图标样式减包等场景
54
85
  *
86
+ * 同时兼容 PostCSS 7 和 PostCSS 8:
87
+ * - PostCSS 8:使用标准 Creator 函数格式 + postcss: true 标记
88
+ * - PostCSS 7:回退到 postcss.plugin() 注册方式
89
+ *
55
90
  * @param opts 配置项
56
91
  * @returns PostCSS 插件
57
92
  */
58
- function postCssPluginRemoveSelector(opts = { list: [] }) {
59
- const { list = [], debug = false } = opts;
60
- const plugin = {
61
- postcssPlugin: 'postcss-plugin-remove-selector',
62
- postcss: true,
63
- Once(root, { result }) {
64
- const fileName = result.opts?.from || '';
65
- const found = shouldHandleFile(list, fileName);
66
- if (!found) {
67
- return;
68
- }
69
- const { exclude = [], include = [], selectorPattern } = found;
70
- if (debug) {
71
- console.log('[postcss-plugin-remove-selector] handling:', fileName);
72
- }
73
- let removedCount = 0;
74
- root.walkRules((rule) => {
75
- if (shouldRemoveRule({
76
- selectorPattern,
77
- exclude,
78
- include,
79
- selector: rule.selector,
80
- })) {
81
- rule.remove();
82
- removedCount += 1;
83
- }
84
- });
85
- if (debug) {
86
- console.log(`[postcss-plugin-remove-selector] removed ${removedCount} rules from:`, fileName);
87
- }
88
- },
89
- };
90
- return plugin;
93
+ const postCssPluginRemoveSelector = (opts = { list: [] }) =>
94
+ // PostCSS 8 格式
95
+ ({
96
+ postcssPlugin: PLUGIN_NAME,
97
+ Once(root, { result }) {
98
+ processRoot(root, result, opts);
99
+ },
100
+ });
101
+ // 标记为 PostCSS 8 插件
102
+ postCssPluginRemoveSelector.postcss = true;
103
+ // PostCSS 7 兼容:通过 postcss.plugin() 注册
104
+ try {
105
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
106
+ const postcss = require('postcss');
107
+ if (postcss && typeof postcss.plugin === 'function') {
108
+ postCssPluginRemoveSelector.postcss7 = postcss.plugin(PLUGIN_NAME, (opts = { list: [] }) => (root, result) => {
109
+ processRoot(root, result, opts);
110
+ });
111
+ }
112
+ }
113
+ catch (e) {
114
+ // postcss 未安装或不支持 postcss.plugin,忽略
91
115
  }
92
116
 
117
+ /**
118
+ * TDesign UniApp 图标减包配置
119
+ *
120
+ * 使用说明:
121
+ * 1. 在 USED_ICONS 数组中配置项目中实际使用的图标名称
122
+ * 2. 插件只会处理 .t-icon-xxx:before 这类图标选择器
123
+ * 3. 其他基础样式(如 @font-face、.t-icon 等)会自动保留
124
+ *
125
+ * 如何查找项目中使用的图标:
126
+ * grep -rho 'icon="[^"]*"\|left-icon="[^"]*"\|name="[^"]*"' src --include="*.vue" | sort | uniq
127
+ */
128
+ /**
129
+ * 项目中实际使用的图标列表
130
+ * 根据 src 目录下的 .vue 文件分析得出
131
+ */
132
+ const USED_ICONS = [
133
+ // custom-tab-bar.vue
134
+ 'home',
135
+ 'chat',
136
+ 'user',
137
+ // home/index.vue
138
+ 'add',
139
+ // release/index.vue
140
+ 'location',
141
+ 'file-copy',
142
+ 'upload',
143
+ // search/index.vue
144
+ 'search',
145
+ 'delete',
146
+ // data-center/index.vue
147
+ 'info-circle-filled',
148
+ // my/index.vue - 静态图标
149
+ 'discount',
150
+ 'edit',
151
+ // my/index.vue - gridList 动态图标
152
+ 'root-list',
153
+ // my/index.vue - settingList 动态图标
154
+ 'service',
155
+ 'setting',
156
+ // setting/index.vue - menuData 动态图标
157
+ 'app',
158
+ 'notification',
159
+ 'image',
160
+ 'chart',
161
+ 'sound',
162
+ 'secured',
163
+ 'info-circle',
164
+ // login/login.vue
165
+ 'logo-wechat-stroke',
166
+ 'logo-qq',
167
+ 'logo-wecom',
168
+ 'caret-down-small',
169
+ // nav-bar.vue
170
+ 'view-list',
171
+ // 组件内部可能使用的图标(如 t-navbar left-arrow 等)
172
+ 'chevron-left',
173
+ 'chevron-right',
174
+ 'chevron-up',
175
+ 'chevron-down',
176
+ 'arrow-left',
177
+ 'arrow-right',
178
+ 'arrow-up',
179
+ 'arrow-down',
180
+ 'close',
181
+ 'close-circle-filled',
182
+ 'check',
183
+ 'check-circle-filled',
184
+ 'error-circle-filled',
185
+ 'loading',
186
+ ];
187
+ /**
188
+ * TDesign UniApp 图标减包配置
189
+ */
190
+ const TDESIGN_ICON_REMOVE_SELECTOR = {
191
+ list: [
192
+ {
193
+ // 匹配 @tdesign/uniapp 的图标 css 文件
194
+ file: /[@/]tdesign[/]uniapp[/]dist[/]icon[/]icon\.[css|vue]/,
195
+ // 只处理 .t-icon-xxx:before 这类图标选择器,其他样式自动保留
196
+ selectorPattern: /^\.t-icon-[\w-]+:before$/,
197
+ // 保留的图标名称列表
198
+ include: USED_ICONS,
199
+ exclude: [],
200
+ },
201
+ ],
202
+ };
203
+
204
+ exports.TDESIGN_ICON_REMOVE_SELECTOR = TDESIGN_ICON_REMOVE_SELECTOR;
205
+ exports["default"] = postCssPluginRemoveSelector;
93
206
  exports.extractIconName = extractIconName;
94
207
  exports.postCssPluginRemoveSelector = postCssPluginRemoveSelector;
95
208
  exports.shouldHandleFile = shouldHandleFile;
@@ -0,0 +1,15 @@
1
+ export type { Options, FileConfig, ShouldRemoveRuleOptions } from './types';
2
+ export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
3
+ /**
4
+ * PostCSS 插件:移除指定的 CSS 选择器
5
+ * 用于图标样式减包等场景
6
+ *
7
+ * 同时兼容 PostCSS 7 和 PostCSS 8:
8
+ * - PostCSS 8:使用标准 Creator 函数格式 + postcss: true 标记
9
+ * - PostCSS 7:回退到 postcss.plugin() 注册方式
10
+ *
11
+ * @param opts 配置项
12
+ * @returns PostCSS 插件
13
+ */
14
+ declare const postCssPluginRemoveSelector: any;
15
+ export { postCssPluginRemoveSelector };
package/lib/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { Root, Result } from 'postcss';
2
1
  /**
3
2
  * 单个文件匹配配置
4
3
  */
@@ -34,12 +33,3 @@ export interface ShouldRemoveRuleOptions {
34
33
  /** 当前选择器 */
35
34
  selector: string;
36
35
  }
37
- /**
38
- * PostCSS 插件返回类型
39
- */
40
- export interface PostCSSPlugin {
41
- postcssPlugin: string;
42
- Once(root: Root, helpers: {
43
- result: Result;
44
- }): void;
45
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novlan/postcss-plugin-remove-selector",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "homepage": "https://novlan1.github.io/plugins/zh/postcss-plugin-remove-selector.html",
5
5
  "bugs": {
6
6
  "url": "https://github.com/novlan1/plugins/issues"
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "dependencies": {},
20
20
  "peerDependencies": {
21
- "postcss": "^8.0.0"
21
+ "postcss": "^7.0.0 || ^8.0.0"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public",