@@ -44,93 +44,100 @@ function getClassNamesFromSelector(selector) {
4444 return classNames
4545}
4646
47- async function process ( groups ) {
47+ async function process ( root ) {
4848 const tree = { }
4949 const commonContext = { }
5050
51- groups . forEach ( ( group ) => {
52- group . root . walkRules ( ( rule ) => {
53- const classNames = getClassNamesFromSelector ( rule . selector )
54-
55- const decls = { }
56- rule . walkDecls ( ( decl ) => {
57- if ( decls [ decl . prop ] ) {
58- decls [ decl . prop ] = [
59- ...( Array . isArray ( decls [ decl . prop ] )
60- ? decls [ decl . prop ]
61- : [ decls [ decl . prop ] ] ) ,
62- decl . value ,
63- ]
51+ let layer
52+
53+ root . walk ( ( node ) => {
54+ if ( node . type === 'comment' ) {
55+ let match = node . text . trim ( ) . match ( / ^ _ _ t w _ i n t e l l i s e n s e _ l a y e r _ ( [ a - z ] + ) _ _ $ / )
56+ if ( match === null ) return
57+ layer = match [ 1 ]
58+ node . remove ( )
59+ return
60+ }
61+
62+ if ( node . type !== 'rule' ) return
63+
64+ const rule = node
65+ const classNames = getClassNamesFromSelector ( rule . selector )
66+
67+ const decls = { }
68+ rule . walkDecls ( ( decl ) => {
69+ if ( decls [ decl . prop ] ) {
70+ decls [ decl . prop ] = [
71+ ...( Array . isArray ( decls [ decl . prop ] )
72+ ? decls [ decl . prop ]
73+ : [ decls [ decl . prop ] ] ) ,
74+ decl . value ,
75+ ]
76+ } else {
77+ decls [ decl . prop ] = decl . value
78+ }
79+ } )
80+
81+ let p = rule
82+ const keys = [ ]
83+ while ( p . parent . type !== 'root' ) {
84+ p = p . parent
85+ if ( p . type === 'atrule' ) {
86+ keys . push ( `@${ p . name } ${ p . params } ` )
87+ }
88+ }
89+
90+ for ( let i = 0 ; i < classNames . length ; i ++ ) {
91+ const context = keys . concat ( [ ] )
92+ const baseKeys = classNames [ i ] . className . split ( / _ _ T W S E P _ _ .* ?_ _ T W S E P _ _ / )
93+ const contextKeys = baseKeys . slice ( 0 , baseKeys . length - 1 )
94+ const index = [ ]
95+
96+ const existing = dlv ( tree , [ ...baseKeys , '__info' ] )
97+ if ( typeof existing !== 'undefined' ) {
98+ if ( Array . isArray ( existing ) ) {
99+ index . push ( existing . length )
64100 } else {
65- decls [ decl . prop ] = decl . value
66- }
67- } )
68-
69- let p = rule
70- const keys = [ ]
71- while ( p . parent . type !== 'root' ) {
72- p = p . parent
73- if ( p . type === 'atrule' ) {
74- keys . push ( `@${ p . name } ${ p . params } ` )
101+ dset ( tree , [ ...baseKeys , '__info' ] , [ existing ] )
102+ index . push ( 1 )
75103 }
76104 }
105+ if ( classNames [ i ] . __rule ) {
106+ dset ( tree , [ ...baseKeys , '__info' , ...index , '__rule' ] , true )
107+ dset ( tree , [ ...baseKeys , '__info' , ...index , '__source' ] , layer )
77108
78- for ( let i = 0 ; i < classNames . length ; i ++ ) {
79- const context = keys . concat ( [ ] )
80- const baseKeys = classNames [ i ] . className . split ( '__TAILWIND_SEPARATOR__' )
81- const contextKeys = baseKeys . slice ( 0 , baseKeys . length - 1 )
82- const index = [ ]
83-
84- const existing = dlv ( tree , [ ...baseKeys , '__info' ] )
85- if ( typeof existing !== 'undefined' ) {
86- if ( Array . isArray ( existing ) ) {
87- index . push ( existing . length )
88- } else {
89- dset ( tree , [ ...baseKeys , '__info' ] , [ existing ] )
90- index . push ( 1 )
91- }
92- }
93- if ( classNames [ i ] . __rule ) {
94- dset ( tree , [ ...baseKeys , '__info' , ...index , '__rule' ] , true )
95- dset (
96- tree ,
97- [ ...baseKeys , '__info' , ...index , '__source' ] ,
98- group . source
109+ dsetEach ( tree , [ ...baseKeys , '__info' , ...index ] , decls )
110+ }
111+ dset (
112+ tree ,
113+ [ ...baseKeys , '__info' , ...index , '__pseudo' ] ,
114+ classNames [ i ] . __pseudo
115+ )
116+ dset (
117+ tree ,
118+ [ ...baseKeys , '__info' , ...index , '__scope' ] ,
119+ classNames [ i ] . scope
120+ )
121+ dset (
122+ tree ,
123+ [ ...baseKeys , '__info' , ...index , '__context' ] ,
124+ context . concat ( [ ] ) . reverse ( )
125+ )
126+
127+ // common context
128+ context . push ( ...classNames [ i ] . __pseudo . map ( ( x ) => `&${ x } ` ) )
129+
130+ for ( let i = 0 ; i < contextKeys . length ; i ++ ) {
131+ if ( typeof commonContext [ contextKeys [ i ] ] === 'undefined' ) {
132+ commonContext [ contextKeys [ i ] ] = context
133+ } else {
134+ commonContext [ contextKeys [ i ] ] = intersection (
135+ commonContext [ contextKeys [ i ] ] ,
136+ context
99137 )
100-
101- dsetEach ( tree , [ ...baseKeys , '__info' , ...index ] , decls )
102- }
103- dset (
104- tree ,
105- [ ...baseKeys , '__info' , ...index , '__pseudo' ] ,
106- classNames [ i ] . __pseudo
107- )
108- dset (
109- tree ,
110- [ ...baseKeys , '__info' , ...index , '__scope' ] ,
111- classNames [ i ] . scope
112- )
113- dset (
114- tree ,
115- [ ...baseKeys , '__info' , ...index , '__context' ] ,
116- context . concat ( [ ] ) . reverse ( )
117- )
118-
119- // common context
120- context . push ( ...classNames [ i ] . __pseudo . map ( ( x ) => `&${ x } ` ) )
121-
122- for ( let i = 0 ; i < contextKeys . length ; i ++ ) {
123- if ( typeof commonContext [ contextKeys [ i ] ] === 'undefined' ) {
124- commonContext [ contextKeys [ i ] ] = context
125- } else {
126- commonContext [ contextKeys [ i ] ] = intersection (
127- commonContext [ contextKeys [ i ] ] ,
128- context
129- )
130- }
131138 }
132139 }
133- } )
140+ }
134141 } )
135142
136143 return { classNames : tree , context : commonContext }
0 commit comments