11/* eslint-disable */
22import postcss from 'postcss' ;
33import valueParser from 'postcss-value-parser' ;
4- // ICSS {String}
5- // import { createICSSRules } from "icss-utils";
64
7- const walkUrls = ( parsed , callback ) => {
5+ const walkUrls = ( parsed , cb ) => {
86 parsed . walk ( ( node ) => {
97 if ( node . type === 'function' && node . value === 'url' ) {
108 const content = node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
119 ? node . nodes [ 0 ] . value
1210 : valueParser . stringify ( node . nodes ) ;
1311
1412 if ( content . trim ( ) . length !== 0 ) {
15- callback ( node , content ) ;
13+ cb ( node , content ) ;
1614 }
1715
1816 // do not traverse inside url
@@ -22,30 +20,32 @@ const walkUrls = (parsed, callback) => {
2220} ;
2321
2422const mapUrls = ( parsed , map ) => {
25- walkUrls ( parsed , ( node , content ) => {
26- node . nodes = [ { type : 'word' , value : map ( content ) } ] ;
23+ walkUrls ( parsed , ( node , url ) => {
24+ node . nodes = [ { type : 'word' , value : map ( url ) } ] ;
2725 } ) ;
2826} ;
2927
30- const filterUrls = ( parsed , filter ) => {
28+ const filterUrls = ( parsed , filter , options ) => {
3129 const result = [ ] ;
3230
33- walkUrls ( parsed , ( node , content ) => {
34- if ( filter ( content ) ) {
35- result . push ( content ) ;
31+ walkUrls ( parsed , ( node , url ) => {
32+ if ( filter ( url , options ) ) {
33+ return false
3634 }
35+
36+ return result . push ( url ) ;
3737 } ) ;
3838
3939 return result ;
4040} ;
4141
42- const walkDeclsWithUrl = ( css , filter ) => {
42+ const walkDeclsWithUrl = ( css , filter , options ) => {
4343 const result = [ ] ;
4444
4545 css . walkDecls ( ( decl ) => {
4646 if ( decl . value . includes ( 'url(' ) ) {
4747 const parsed = valueParser ( decl . value ) ;
48- const values = filterUrls ( parsed , filter ) ;
48+ const values = filterUrls ( parsed , filter , options ) ;
4949
5050 if ( values . length ) {
5151 result . push ( {
@@ -60,10 +60,39 @@ const walkDeclsWithUrl = (css, filter) => {
6060 return result ;
6161} ;
6262
63- const filterValues = value => ! / ^ \w + : \/ \/ / . test ( value ) &&
64- ! value . startsWith ( '//' ) &&
65- ! value . startsWith ( '#' ) &&
66- ! value . startsWith ( 'data:' ) ;
63+ const URL = / ^ \w + : \/ \/ / ;
64+
65+ const filter = ( url , options ) => {
66+ if ( URL . test ( url ) ) {
67+ return true ;
68+ }
69+
70+ if ( url . startsWith ( '//' ) ) {
71+ return true ;
72+ }
73+
74+ if ( url . startsWith ( '//' ) ) {
75+ return true ;
76+ }
77+
78+ if ( url . startsWith ( '#' ) ) {
79+ return true ;
80+ }
81+
82+ if ( url . startsWith ( 'data:' ) ) {
83+ return true ;
84+ }
85+
86+ if ( options . url instanceof RegExp ) {
87+ return options . url . test ( url ) ;
88+ }
89+
90+ if ( typeof options . url === 'function' ) {
91+ return options . url ( url ) ;
92+ }
93+
94+ return false ;
95+ }
6796
6897const flatten = arr => arr . reduce ( ( acc , d ) => [ ...acc , ...d ] , [ ] ) ;
6998
@@ -72,24 +101,23 @@ const uniq = arr => arr.reduce(
72101 [ ] ,
73102) ;
74103
75- module . exports = postcss . plugin ( 'postcss-icss-url' , ( ) => ( css , result ) => {
76- const traversed = walkDeclsWithUrl ( css , filterValues ) ;
104+ module . exports = postcss . plugin ( 'postcss-icss-url' , ( options ) => ( css , result ) => {
105+ const traversed = walkDeclsWithUrl ( css , filter , options ) ;
77106 const paths = uniq ( flatten ( traversed . map ( item => item . values ) ) ) ;
78107
79- // ICSS imports {String}
80108 const aliases = { } ;
81109
82110 paths . forEach ( ( url , idx ) => {
83- // ICSS Placeholder
111+ // CSS Content Placeholder
84112 const alias = '${' + `CSS__URL__${ idx } ` + '}' ;
85-
113+
86114 aliases [ url ] = alias ;
87115
88116 result . messages . push ( {
89117 type : 'import' ,
90118 plugin : 'postcss-icss-url' ,
91119 import : `import CSS__URL__${ idx } from '${ url } ';\n`
92- } )
120+ } ) ;
93121 } ) ;
94122
95123 traversed . forEach ( ( item ) => {
@@ -98,3 +126,4 @@ module.exports = postcss.plugin('postcss-icss-url', () => (css, result) => {
98126 item . decl . value = item . parsed . toString ( ) ;
99127 } ) ;
100128} ) ;
129+
0 commit comments