11const  postcss  =  require ( 'postcss' ) ; 
2+ const  valueParser  =  require ( 'postcss-value-parser' ) ; 
23const  loaderUtils  =  require ( 'loader-utils' ) ; 
3- const  Tokenizer  =  require ( 'css-selector-tokenizer' ) ; 
44
55const  pluginName  =  'postcss-import-parser' ; 
66
7+ function  getArg ( nodes )  { 
8+   return  nodes . length  !==  0  &&  nodes [ 0 ] . type  ===  'string' 
9+     ? nodes [ 0 ] . value 
10+     : valueParser . stringify ( nodes ) ; 
11+ } 
12+ 
13+ function  getUrl ( node )  { 
14+   if  ( node . type  ===  'function'  &&  node . value . toLowerCase ( )  ===  'url' )  { 
15+     return  getArg ( node . nodes ) ; 
16+   } 
17+ 
18+   if  ( node . type  ===  'string' )  { 
19+     return  node . value ; 
20+   } 
21+ 
22+   return  '' ; 
23+ } 
24+ 
25+ function  parseImport ( params )  { 
26+   const  {  nodes }  =  valueParser ( params ) ; 
27+ 
28+   if  ( nodes . length  ===  0 )  { 
29+     return  null ; 
30+   } 
31+ 
32+   const  url  =  getUrl ( nodes [ 0 ] ) ; 
33+ 
34+   if  ( url . trim ( ) . length  ===  0 )  { 
35+     return  null ; 
36+   } 
37+ 
38+   return  { 
39+     url, 
40+     media : valueParser . stringify ( nodes . slice ( 1 ) ) . trim ( ) , 
41+   } ; 
42+ } 
43+ 
744module . exports  =  postcss . plugin ( 
845  pluginName , 
946  ( options )  => 
@@ -25,43 +62,22 @@ module.exports = postcss.plugin(
2562          return ; 
2663        } 
2764
28-         const  values  =  Tokenizer . parseValues ( atrule . params ) ; 
29-         let  [ url ]  =  values . nodes [ 0 ] . nodes ; 
65+         const  parsed  =  parseImport ( atrule . params ) ; 
3066
31-         if  ( url  &&  url . type  ===  'url' )  { 
32-           ( {  url }  =  url ) ; 
33-         }  else  if  ( url  &&  url . type  ===  'string' )  { 
34-           url  =  url . value ; 
35-         }  else  { 
36-           result . warn ( `Unable to find uri in '${ atrule . toString ( ) }  ,  { 
67+         if  ( ! parsed )  { 
68+           // eslint-disable-next-line consistent-return 
69+           return  result . warn ( `Unable to find uri in '${ atrule . toString ( ) }  ,  { 
3770            node : atrule , 
3871          } ) ; 
39- 
40-           return ; 
4172        } 
4273
43-         if  ( ! url . replace ( / \s / g,  '' ) . length )  { 
44-           result . warn ( `Unable to find uri in '${ atrule . toString ( ) }  ,  { 
45-             node : atrule , 
46-           } ) ; 
47- 
48-           return ; 
49-         } 
50- 
51-         values . nodes [ 0 ] . nodes . shift ( ) ; 
52- 
53-         const  mediaQuery  =  Tokenizer . stringifyValues ( values ) ; 
54- 
55-         url  =  url . trim ( ) ; 
74+         let  {  url }  =  parsed ; 
5675
5776        if  ( loaderUtils . isUrlRequest ( url ) )  { 
5877          url  =  loaderUtils . urlToRequest ( url ) ; 
5978        } 
6079
61-         importItems . push ( { 
62-           url, 
63-           mediaQuery, 
64-         } ) ; 
80+         importItems . push ( {  url,  mediaQuery : parsed . media  } ) ; 
6581
6682        atrule . remove ( ) ; 
6783      } ) ; 
0 commit comments