11const postcss = require ( 'postcss' ) ;
22
3- const plugin = require ( '../src/index' ) ;
3+ const { postCssPluginRemoveSelector } = require ( '../src/index' ) ;
44
55const input = `
6- .press -icon-plus -arrow-down:before {
6+ .t -icon-arrow-down:before {
77 content: 'e65e';
88}
99
10- .press -icon-plus -arrow-up:before {
10+ .t -icon-arrow-up:before {
1111 content: 'e65f';
1212}
1313
14- .press -icon-plus -invitation:before {
14+ .t -icon-invitation:before {
1515 content: 'e6d6';
1616}
1717
18- .press -icon-plus -like-o:before {
18+ .t -icon-like-o:before {
1919 content: 'e6d7';
2020}
2121
@@ -25,13 +25,12 @@ const input = `
2525` ;
2626
2727
28- const getFullIconName = ( name => `.press-icon-plus-${ name } :before` ) ;
29-
3028test ( 'exclude' , async ( ) => {
31- const result = await postcss ( [ plugin ( {
29+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
3230 list : [ {
3331 file : 'abc' ,
34- exclude : [ 'invitation' , 'like-o' ] . map ( getFullIconName ) ,
32+ exclude : [ 'invitation' , 'like-o' ] ,
33+ selectorPattern : / ^ \. t - i c o n - [ \w - ] + : b e f o r e $ / ,
3534 } ] ,
3635 } ) ] ) . process ( input , { from : 'abc' } ) ;
3736
@@ -41,10 +40,11 @@ test('exclude', async () => {
4140
4241
4342test ( 'include' , async ( ) => {
44- const result = await postcss ( [ plugin ( {
43+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
4544 list : [ {
4645 file : 'abc' ,
47- include : [ 'arrow-down' , 'arrow-up' ] . map ( getFullIconName ) ,
46+ include : [ 'arrow-down' , 'arrow-up' ] ,
47+ selectorPattern : / ^ \. t - i c o n - [ \w - ] + : b e f o r e $ / ,
4848 } ] ,
4949 } ) ] ) . process ( input , { from : 'abc' } ) ;
5050
@@ -54,11 +54,12 @@ test('include', async () => {
5454
5555
5656test ( 'include & exclude' , async ( ) => {
57- const result = await postcss ( [ plugin ( {
57+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
5858 list : [ {
5959 file : 'abc' ,
60- include : [ 'arrow-down' , 'arrow-up' ] . map ( item => `.press-icon-plus-${ item } :before` ) ,
61- exclude : [ 'invitation' , 'like-o' , 'arrow-up' ] . map ( getFullIconName ) ,
60+ include : [ 'arrow-down' , 'arrow-up' ] ,
61+ exclude : [ 'invitation' , 'like-o' , 'arrow-up' ] ,
62+ selectorPattern : / ^ \. t - i c o n - [ \w - ] + : b e f o r e $ / ,
6263 } ] ,
6364 } ) ] ) . process ( input , { from : 'abc' } ) ;
6465
@@ -68,20 +69,48 @@ test('include & exclude', async () => {
6869
6970
7071test ( 'file is regexp' , async ( ) => {
71- const result = await postcss ( [ plugin ( {
72+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
7273 list : [ {
73- file : new RegExp ( 'press-ui/press-icon-plus/css/icon.scss' ) ,
74- include : [ 'arrow-down' , 'arrow-up' ] . map ( getFullIconName ) ,
74+ file : / t d e s i g n \/ u n i a p p \/ d i s t \/ i c o n / ,
75+ include : [ 'arrow-down' , 'arrow-up' ] ,
76+ selectorPattern : / ^ \. t - i c o n - [ \w - ] + : b e f o r e $ / ,
7577 } ] ,
76- } ) ] ) . process ( input , { from : 'press-ui/press-icon-plus/css /icon.scss ' } ) ;
78+ } ) ] ) . process ( input , { from : 'tdesign/uniapp/dist /icon/icon.css ' } ) ;
7779
7880 expect ( result . css ) . toMatchSnapshot ( ) ;
7981 expect ( result . warnings ( ) ) . toHaveLength ( 0 ) ;
8082} ) ;
8183
8284
8385test ( 'empty options' , async ( ) => {
84- const result = await postcss ( [ plugin ( ) ] ) . process ( input , { from : 'press-ui/press-icon-plus/css/icon.scss' } ) ;
86+ const result = await postcss ( [ postCssPluginRemoveSelector ( ) ] ) . process ( input , { from : 'tdesign/uniapp/dist/icon/icon.css' } ) ;
87+
88+ expect ( result . css ) . toMatchSnapshot ( ) ;
89+ expect ( result . warnings ( ) ) . toHaveLength ( 0 ) ;
90+ } ) ;
91+
92+
93+ test ( 'no selectorPattern - should not remove non-icon rules' , async ( ) => {
94+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
95+ list : [ {
96+ file : 'abc' ,
97+ include : [ 'arrow-down' ] ,
98+ } ] ,
99+ } ) ] ) . process ( input , { from : 'abc' } ) ;
100+
101+ expect ( result . css ) . toMatchSnapshot ( ) ;
102+ expect ( result . warnings ( ) ) . toHaveLength ( 0 ) ;
103+ } ) ;
104+
105+
106+ test ( 'file not matched - should not modify css' , async ( ) => {
107+ const result = await postcss ( [ postCssPluginRemoveSelector ( {
108+ list : [ {
109+ file : 'not-matched-file' ,
110+ exclude : [ 'arrow-down' ] ,
111+ selectorPattern : / ^ \. t - i c o n - [ \w - ] + : b e f o r e $ / ,
112+ } ] ,
113+ } ) ] ) . process ( input , { from : 'abc' } ) ;
85114
86115 expect ( result . css ) . toMatchSnapshot ( ) ;
87116 expect ( result . warnings ( ) ) . toHaveLength ( 0 ) ;
0 commit comments