11import { shouldHandleFile , shouldRemoveRule } from './helper' ;
2+ import { PRESETS } from './presets' ;
23
3- import type { Options } from './types' ;
4+ import type { Options , SimpleOptions } from './types' ;
45
5- export type { Options , FileConfig , ShouldRemoveRuleOptions } from './types' ;
6+ export type { Options , SimpleOptions , FileConfig , ShouldRemoveRuleOptions , PresetMode , Preset } from './types' ;
67export { shouldHandleFile , shouldRemoveRule , extractIconName } from './helper' ;
78
89const PLUGIN_NAME = 'postcss-plugin-remove-selector' ;
910
11+ /**
12+ * 判断传入的配置是否为简化模式
13+ */
14+ function isSimpleOptions ( opts : Options | SimpleOptions ) : opts is SimpleOptions {
15+ return ! ( 'list' in opts ) ;
16+ }
17+
18+ /**
19+ * 将简化配置转换为标准 Options
20+ */
21+ function normalizeOptions ( opts : Options | SimpleOptions ) : Options {
22+ if ( ! isSimpleOptions ( opts ) ) {
23+ return opts ;
24+ }
25+
26+ const { mode, file, used, unused, customUsed, customUnused, selectorPattern, debug } = opts ;
27+
28+ let resolvedFile = file ;
29+ let resolvedSelectorPattern = selectorPattern ;
30+ let resolvedUsed = used || [ ] ;
31+ let resolvedUnused = unused || [ ] ;
32+
33+ // 如果指定了 mode,使用预设的默认值
34+ if ( mode && PRESETS [ mode ] ) {
35+ const preset = PRESETS [ mode ] ;
36+ if ( ! resolvedFile ) {
37+ resolvedFile = preset . file ;
38+ }
39+ if ( ! resolvedSelectorPattern ) {
40+ resolvedSelectorPattern = preset . selectorPattern ;
41+ }
42+ // 如果用户没有显式指定 used/unused,使用预设的默认值
43+ if ( ! used && preset . used ) {
44+ resolvedUsed = [ ...preset . used ] ;
45+ }
46+ if ( ! unused && preset . unused ) {
47+ resolvedUnused = [ ...preset . unused ] ;
48+ }
49+ }
50+
51+ // 将 customUsed/customUnused 增量追加(不覆盖)
52+ if ( customUsed ?. length ) {
53+ resolvedUsed = [ ...resolvedUsed , ...customUsed ] ;
54+ }
55+ if ( customUnused ?. length ) {
56+ resolvedUnused = [ ...resolvedUnused , ...customUnused ] ;
57+ }
58+
59+ if ( ! resolvedFile ) {
60+ throw new Error ( `[${ PLUGIN_NAME } ] 必须指定 "file" 或 "mode",当前均未设置。` ) ;
61+ }
62+
63+ return {
64+ list : [
65+ {
66+ file : resolvedFile ,
67+ used : resolvedUsed ,
68+ unused : resolvedUnused ,
69+ selectorPattern : resolvedSelectorPattern ,
70+ } ,
71+ ] ,
72+ debug,
73+ } ;
74+ }
75+
1076/**
1177 * 核心处理逻辑,PostCSS 7 / 8 共用
1278 */
@@ -19,7 +85,12 @@ function processRoot(root: any, result: any, opts: Options) {
1985 return ;
2086 }
2187
22- const { exclude = [ ] , include = [ ] , selectorPattern } = found ;
88+ const { used = [ ] , unused = [ ] , customUsed = [ ] , customUnused = [ ] , selectorPattern } = found ;
89+
90+ // 合并 customUsed/customUnused 到 used/unused
91+ const mergedUsed = customUsed . length ? [ ...used , ...customUsed ] : used ;
92+ const mergedUnused = customUnused . length ? [ ...unused , ...customUnused ] : unused ;
93+
2394 if ( debug ) {
2495 console . log ( '[postcss-plugin-remove-selector] handling:' , fileName ) ;
2596 }
@@ -29,8 +100,8 @@ function processRoot(root: any, result: any, opts: Options) {
29100 root . walkRules ( ( rule : any ) => {
30101 if ( shouldRemoveRule ( {
31102 selectorPattern,
32- exclude ,
33- include ,
103+ used : mergedUsed ,
104+ unused : mergedUnused ,
34105 selector : rule . selector ,
35106 } ) ) {
36107 rule . remove ( ) ;
@@ -51,36 +122,41 @@ function processRoot(root: any, result: any, opts: Options) {
51122 * - PostCSS 8:使用标准 Creator 函数格式 + postcss: true 标记
52123 * - PostCSS 7:回退到 postcss.plugin() 注册方式
53124 *
125+ * 支持两种配置方式:
126+ * 1. 标准模式:传入 { list: [...], debug?: boolean }
127+ * 2. 简化模式:传入 { mode?, file?, used?, unused?, customUsed?, customUnused?, selectorPattern?, debug? }
128+ *
54129 * @param opts 配置项
55130 * @returns PostCSS 插件
56131 */
57- const postCssPluginRemoveSelector : any = ( opts : Options = { list : [ ] } ) =>
58- // PostCSS 8 格式
59- ( {
132+ const postcssPluginRemoveSelector : any = ( opts : Options | SimpleOptions = { list : [ ] } ) => {
133+ const normalizedOpts = normalizeOptions ( opts ) ;
134+ return {
60135 postcssPlugin : PLUGIN_NAME ,
61136 Once ( root : any , { result } : any ) {
62- processRoot ( root , result , opts ) ;
137+ processRoot ( root , result , normalizedOpts ) ;
63138 } ,
64- } )
65- ;
139+ } ;
140+ } ;
66141
67142// 标记为 PostCSS 8 插件
68- postCssPluginRemoveSelector . postcss = true as const ;
143+ postcssPluginRemoveSelector . postcss = true as const ;
69144
70145// PostCSS 7 兼容:通过 postcss.plugin() 注册
71146try {
72147 // eslint-disable-next-line @typescript-eslint/no-require-imports
73148 const postcss = require ( 'postcss' ) ;
74149 if ( postcss && typeof postcss . plugin === 'function' ) {
75- postCssPluginRemoveSelector . postcss7 = postcss . plugin (
150+ postcssPluginRemoveSelector . postcss7 = postcss . plugin (
76151 PLUGIN_NAME ,
77- ( opts : Options = { list : [ ] } ) => ( root : any , result : any ) => {
78- processRoot ( root , result , opts ) ;
152+ ( opts : Options | SimpleOptions = { list : [ ] } ) => ( root : any , result : any ) => {
153+ const normalizedOpts = normalizeOptions ( opts ) ;
154+ processRoot ( root , result , normalizedOpts ) ;
79155 } ,
80156 ) ;
81157 }
82158} catch ( e ) {
83159 // postcss 未安装或不支持 postcss.plugin,忽略
84160}
85161
86- export { postCssPluginRemoveSelector } ;
162+ export { postcssPluginRemoveSelector } ;
0 commit comments