@@ -4,15 +4,15 @@ import valueParser from 'postcss-value-parser';
44// ICSS {String}
55// import { createICSSRules } from "icss-utils";
66
7- const walkUrls = ( parsed , callback ) => {
7+ const walkUrls = ( parsed , cb ) => {
88 parsed . walk ( ( node ) => {
99 if ( node . type === 'function' && node . value === 'url' ) {
1010 const content = node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
1111 ? node . nodes [ 0 ] . value
1212 : valueParser . stringify ( node . nodes ) ;
1313
1414 if ( content . trim ( ) . length !== 0 ) {
15- callback ( node , content ) ;
15+ cb ( node , content ) ;
1616 }
1717
1818 // do not traverse inside url
@@ -22,30 +22,32 @@ const walkUrls = (parsed, callback) => {
2222} ;
2323
2424const mapUrls = ( parsed , map ) => {
25- walkUrls ( parsed , ( node , content ) => {
26- node . nodes = [ { type : 'word' , value : map ( content ) } ] ;
25+ walkUrls ( parsed , ( node , url ) => {
26+ node . nodes = [ { type : 'word' , value : map ( url ) } ] ;
2727 } ) ;
2828} ;
2929
30- const filterUrls = ( parsed , filter ) => {
30+ const filterUrls = ( parsed , filter , options ) => {
3131 const result = [ ] ;
3232
33- walkUrls ( parsed , ( node , content ) => {
34- if ( filter ( content ) ) {
35- result . push ( content ) ;
33+ walkUrls ( parsed , ( node , url ) => {
34+ if ( filter ( url , options ) ) {
35+ return false
3636 }
37+
38+ return result . push ( url ) ;
3739 } ) ;
3840
3941 return result ;
4042} ;
4143
42- const walkDeclsWithUrl = ( css , filter ) => {
44+ const walkDeclsWithUrl = ( css , filter , options ) => {
4345 const result = [ ] ;
4446
4547 css . walkDecls ( ( decl ) => {
4648 if ( decl . value . includes ( 'url(' ) ) {
4749 const parsed = valueParser ( decl . value ) ;
48- const values = filterUrls ( parsed , filter ) ;
50+ const values = filterUrls ( parsed , filter , options ) ;
4951
5052 if ( values . length ) {
5153 result . push ( {
@@ -60,31 +62,62 @@ const walkDeclsWithUrl = (css, filter) => {
6062 return result ;
6163} ;
6264
63- const filterValues = value => ! / ^ \w + : \/ \/ / . test ( value ) &&
64- ! value . startsWith ( '//' ) &&
65- ! value . startsWith ( '#' ) &&
66- ! value . startsWith ( 'data:' ) ;
65+ const URL = / ^ \w + : \/ \/ / ;
66+
67+ const filter = ( url , options ) => {
68+ if ( URL . test ( url ) ) {
69+ return true ;
70+ }
71+
72+ if ( url . startsWith ( '//' ) ) {
73+ return true ;
74+ }
75+
76+ if ( url . startsWith ( '//' ) ) {
77+ return true ;
78+ }
79+
80+ if ( url . startsWith ( '#' ) ) {
81+ return true ;
82+ }
83+
84+ if ( url . startsWith ( 'data:' ) ) {
85+ return true ;
86+ }
87+
88+ if ( typeof options . url === 'string' ) {
89+ return url . includes ( options . url ) ;
90+ }
91+
92+ if ( options . url instanceof RegExp ) {
93+ return options . url . test ( url ) ;
94+ }
95+
96+ if ( typeof options . url === 'function' ) {
97+ return options . url ( url ) ;
98+ }
99+
100+ return false ;
101+ }
67102
68103const flatten = arr => arr . reduce ( ( acc , d ) => [ ...acc , ...d ] , [ ] ) ;
69104const uniq = arr => arr . reduce (
70105 ( acc , d ) => ( acc . indexOf ( d ) === - 1 ? [ ...acc , d ] : acc ) ,
71106 [ ] ,
72107) ;
73108
74- module . exports = postcss . plugin ( 'postcss-icss-url' , ( ) => ( css , result ) => {
75- const traversed = walkDeclsWithUrl ( css , filterValues ) ;
109+ module . exports = postcss . plugin ( 'postcss-icss-url' , ( options ) => ( css , result ) => {
110+ const traversed = walkDeclsWithUrl ( css , filter , options ) ;
76111 const paths = uniq ( flatten ( traversed . map ( item => item . values ) ) ) ;
77112
78- // ICSS imports {String}
113+ // ICSS Imports {String}
79114 const urls = { } ;
80115 const aliases = { } ;
81116
82117 paths . forEach ( ( path , index ) => {
83- // ICSS Placeholder
118+ // (I)CSS Content Placeholder
84119 const alias = '${' + `CSS__URL__${ index } ` + '}' ;
85120
86- // console.log(alias)
87-
88121 // ICSS {String}
89122 // imports[`'${path}'`] = {
90123 // [alias]: "default"
@@ -100,15 +133,10 @@ module.exports = postcss.plugin('postcss-icss-url', () => (css, result) => {
100133
101134 item . decl . value = item . parsed . toString ( ) ;
102135 } ) ;
103-
136+
137+ // Add URLs to result.messages
104138 result . messages . push ( { urls } ) ;
105139
106- // result.messages.push({
107- // type: 'dependency',
108- // plugin: 'postcss-icss-url',
109- // urls
110- // })
111-
112140 // ICSS {String}
113141 // css.prepend(createICSSRules(imports, {}));
114142} ) ;
0 commit comments