@novlan/postcss-plugin-remove-selector 0.1.2 → 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,2 +1,3 @@
1
1
  export * from './plugin';
2
2
  export { TDESIGN_ICON_REMOVE_SELECTOR } from './tdesign-uniapp-icon';
3
+ export { postCssPluginRemoveSelector as default } from './plugin';
package/lib/index.js CHANGED
@@ -48,46 +48,70 @@ 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
  /**
@@ -178,6 +202,7 @@ const TDESIGN_ICON_REMOVE_SELECTOR = {
178
202
  };
179
203
 
180
204
  exports.TDESIGN_ICON_REMOVE_SELECTOR = TDESIGN_ICON_REMOVE_SELECTOR;
205
+ exports["default"] = postCssPluginRemoveSelector;
181
206
  exports.extractIconName = extractIconName;
182
207
  exports.postCssPluginRemoveSelector = postCssPluginRemoveSelector;
183
208
  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 };
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.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",