@@ -4,15 +4,15 @@ import valueParser from 'postcss-value-parser';
4
4
// ICSS {String}
5
5
// import { createICSSRules } from "icss-utils";
6
6
7
- const walkUrls = ( parsed , callback ) => {
7
+ const walkUrls = ( parsed , cb ) => {
8
8
parsed . walk ( ( node ) => {
9
9
if ( node . type === 'function' && node . value === 'url' ) {
10
10
const content = node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
11
11
? node . nodes [ 0 ] . value
12
12
: valueParser . stringify ( node . nodes ) ;
13
13
14
14
if ( content . trim ( ) . length !== 0 ) {
15
- callback ( node , content ) ;
15
+ cb ( node , content ) ;
16
16
}
17
17
18
18
// do not traverse inside url
@@ -22,30 +22,32 @@ const walkUrls = (parsed, callback) => {
22
22
} ;
23
23
24
24
const 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 ) } ] ;
27
27
} ) ;
28
28
} ;
29
29
30
- const filterUrls = ( parsed , filter ) => {
30
+ const filterUrls = ( parsed , filter , options ) => {
31
31
const result = [ ] ;
32
32
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
36
36
}
37
+
38
+ return result . push ( url ) ;
37
39
} ) ;
38
40
39
41
return result ;
40
42
} ;
41
43
42
- const walkDeclsWithUrl = ( css , filter ) => {
44
+ const walkDeclsWithUrl = ( css , filter , options ) => {
43
45
const result = [ ] ;
44
46
45
47
css . walkDecls ( ( decl ) => {
46
48
if ( decl . value . includes ( 'url(' ) ) {
47
49
const parsed = valueParser ( decl . value ) ;
48
- const values = filterUrls ( parsed , filter ) ;
50
+ const values = filterUrls ( parsed , filter , options ) ;
49
51
50
52
if ( values . length ) {
51
53
result . push ( {
@@ -60,31 +62,62 @@ const walkDeclsWithUrl = (css, filter) => {
60
62
return result ;
61
63
} ;
62
64
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
+ }
67
102
68
103
const flatten = arr => arr . reduce ( ( acc , d ) => [ ...acc , ...d ] , [ ] ) ;
69
104
const uniq = arr => arr . reduce (
70
105
( acc , d ) => ( acc . indexOf ( d ) === - 1 ? [ ...acc , d ] : acc ) ,
71
106
[ ] ,
72
107
) ;
73
108
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 ) ;
76
111
const paths = uniq ( flatten ( traversed . map ( item => item . values ) ) ) ;
77
112
78
- // ICSS imports {String}
113
+ // ICSS Imports {String}
79
114
const urls = { } ;
80
115
const aliases = { } ;
81
116
82
117
paths . forEach ( ( path , index ) => {
83
- // ICSS Placeholder
118
+ // (I)CSS Content Placeholder
84
119
const alias = '${' + `CSS__URL__${ index } ` + '}' ;
85
120
86
- // console.log(alias)
87
-
88
121
// ICSS {String}
89
122
// imports[`'${path}'`] = {
90
123
// [alias]: "default"
@@ -100,15 +133,10 @@ module.exports = postcss.plugin('postcss-icss-url', () => (css, result) => {
100
133
101
134
item . decl . value = item . parsed . toString ( ) ;
102
135
} ) ;
103
-
136
+
137
+ // Add URLs to result.messages
104
138
result . messages . push ( { urls } ) ;
105
139
106
- // result.messages.push({
107
- // type: 'dependency',
108
- // plugin: 'postcss-icss-url',
109
- // urls
110
- // })
111
-
112
140
// ICSS {String}
113
141
// css.prepend(createICSSRules(imports, {}));
114
142
} ) ;
0 commit comments