@novlan/postcss-plugin-remove-selector 0.1.2 → 0.1.4

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/README.md CHANGED
@@ -44,25 +44,21 @@ export default defineConfig({
44
44
 
45
45
  ## 3. 类型
46
46
 
47
- ```ts
48
- interface FileConfig {
49
- /** 文件匹配规则,可以是字符串或正则表达式 */
50
- file: RegExp | string;
51
- /** 需要保留的选择器列表(图标名称) */
52
- include?: string[];
53
- /** 需要移除的选择器列表(图标名称) */
54
- exclude?: string[];
55
- /** 选择器匹配模式,只处理匹配该模式的选择器 */
56
- selectorPattern?: RegExp;
57
- }
58
-
59
- interface Options {
60
- /** 配置列表 */
61
- list: FileConfig[];
62
- /** 是否开启调试模式 */
63
- debug?: boolean;
64
- }
65
- ```
47
+ ### Options
48
+
49
+ | 属性 | 类型 | 是否必填 | 说明 |
50
+ | --- | --- | --- | --- |
51
+ | `list` | `FileConfig[]` | 是 | 配置列表 |
52
+ | `debug` | `boolean` | 否 | 是否开启调试模式 |
53
+
54
+ ### FileConfig
55
+
56
+ | 属性 | 类型 | 是否必填 | 说明 |
57
+ | --- | --- | --- | --- |
58
+ | `file` | `RegExp \| string` | 是 | 文件匹配规则,可以是字符串或正则表达式 |
59
+ | `include` | `string[]` | 否 | 需要保留的选择器列表(图标名称) |
60
+ | `exclude` | `string[]` | 否 | 需要移除的选择器列表(图标名称) |
61
+ | `selectorPattern` | `RegExp` | 否 | 选择器匹配模式,只处理匹配该模式的选择器 |
66
62
 
67
63
  ## 4. 更新日志
68
64
 
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './plugin';
2
- export { TDESIGN_ICON_REMOVE_SELECTOR } from './tdesign-uniapp-icon';
2
+ export * from './tdesign-uniapp-icon';
3
+ export { postCssPluginRemoveSelector as default } from './plugin';
package/lib/index.js CHANGED
@@ -48,53 +48,77 @@ 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
 
93
117
  /**
94
118
  * TDesign UniApp 图标减包配置
95
119
  *
96
120
  * 使用说明:
97
- * 1. 在 USED_ICONS 数组中配置项目中实际使用的图标名称
121
+ * 1. 在 TDESIGN_USED_ICONS 数组中配置项目中实际使用的图标名称
98
122
  * 2. 插件只会处理 .t-icon-xxx:before 这类图标选择器
99
123
  * 3. 其他基础样式(如 @font-face、.t-icon 等)会自动保留
100
124
  *
@@ -105,7 +129,7 @@ function postCssPluginRemoveSelector(opts = { list: [] }) {
105
129
  * 项目中实际使用的图标列表
106
130
  * 根据 src 目录下的 .vue 文件分析得出
107
131
  */
108
- const USED_ICONS = [
132
+ const TDESIGN_USED_ICONS = [
109
133
  // custom-tab-bar.vue
110
134
  'home',
111
135
  'chat',
@@ -171,13 +195,15 @@ const TDESIGN_ICON_REMOVE_SELECTOR = {
171
195
  // 只处理 .t-icon-xxx:before 这类图标选择器,其他样式自动保留
172
196
  selectorPattern: /^\.t-icon-[\w-]+:before$/,
173
197
  // 保留的图标名称列表
174
- include: USED_ICONS,
198
+ include: TDESIGN_USED_ICONS,
175
199
  exclude: [],
176
200
  },
177
201
  ],
178
202
  };
179
203
 
180
204
  exports.TDESIGN_ICON_REMOVE_SELECTOR = TDESIGN_ICON_REMOVE_SELECTOR;
205
+ exports.TDESIGN_USED_ICONS = TDESIGN_USED_ICONS;
206
+ exports["default"] = postCssPluginRemoveSelector;
181
207
  exports.extractIconName = extractIconName;
182
208
  exports.postCssPluginRemoveSelector = postCssPluginRemoveSelector;
183
209
  exports.shouldHandleFile = shouldHandleFile;
package/lib/plugin.d.ts CHANGED
@@ -1,13 +1,15 @@
1
- import type { Options, PostCSSPlugin } from './types';
2
- export type { Options, FileConfig, ShouldRemoveRuleOptions, PostCSSPlugin } from './types';
1
+ export type { Options, FileConfig, ShouldRemoveRuleOptions } from './types';
3
2
  export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
4
3
  /**
5
4
  * PostCSS 插件:移除指定的 CSS 选择器
6
5
  * 用于图标样式减包等场景
7
6
  *
7
+ * 同时兼容 PostCSS 7 和 PostCSS 8:
8
+ * - PostCSS 8:使用标准 Creator 函数格式 + postcss: true 标记
9
+ * - PostCSS 7:回退到 postcss.plugin() 注册方式
10
+ *
8
11
  * @param opts 配置项
9
12
  * @returns PostCSS 插件
10
13
  */
11
- export declare function postCssPluginRemoveSelector(opts?: Options): PostCSSPlugin & {
12
- postcss: true;
13
- };
14
+ declare const postCssPluginRemoveSelector: any;
15
+ export { postCssPluginRemoveSelector };
@@ -2,13 +2,18 @@
2
2
  * TDesign UniApp 图标减包配置
3
3
  *
4
4
  * 使用说明:
5
- * 1. 在 USED_ICONS 数组中配置项目中实际使用的图标名称
5
+ * 1. 在 TDESIGN_USED_ICONS 数组中配置项目中实际使用的图标名称
6
6
  * 2. 插件只会处理 .t-icon-xxx:before 这类图标选择器
7
7
  * 3. 其他基础样式(如 @font-face、.t-icon 等)会自动保留
8
8
  *
9
9
  * 如何查找项目中使用的图标:
10
10
  * grep -rho 'icon="[^"]*"\|left-icon="[^"]*"\|name="[^"]*"' src --include="*.vue" | sort | uniq
11
11
  */
12
+ /**
13
+ * 项目中实际使用的图标列表
14
+ * 根据 src 目录下的 .vue 文件分析得出
15
+ */
16
+ export declare const TDESIGN_USED_ICONS: string[];
12
17
  /**
13
18
  * TDesign UniApp 图标减包配置
14
19
  */
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.2",
3
+ "version": "0.1.4",
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",