1
1
/* eslint-disable */
2
2
import postcss from 'postcss' ;
3
3
import valueParser from 'postcss-value-parser' ;
4
- // ICSS {String}
5
- // import { createICSSRules } from "icss-utils";
6
4
7
- const walkUrls = ( parsed , callback ) => {
5
+ const walkUrls = ( parsed , cb ) => {
8
6
parsed . walk ( ( node ) => {
9
7
if ( node . type === 'function' && node . value === 'url' ) {
10
8
const content = node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
11
9
? node . nodes [ 0 ] . value
12
10
: valueParser . stringify ( node . nodes ) ;
13
11
14
12
if ( content . trim ( ) . length !== 0 ) {
15
- callback ( node , content ) ;
13
+ cb ( node , content ) ;
16
14
}
17
15
18
16
// do not traverse inside url
@@ -22,30 +20,32 @@ const walkUrls = (parsed, callback) => {
22
20
} ;
23
21
24
22
const 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 ) } ] ;
27
25
} ) ;
28
26
} ;
29
27
30
- const filterUrls = ( parsed , filter ) => {
28
+ const filterUrls = ( parsed , filter , options ) => {
31
29
const result = [ ] ;
32
30
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
36
34
}
35
+
36
+ return result . push ( url ) ;
37
37
} ) ;
38
38
39
39
return result ;
40
40
} ;
41
41
42
- const walkDeclsWithUrl = ( css , filter ) => {
42
+ const walkDeclsWithUrl = ( css , filter , options ) => {
43
43
const result = [ ] ;
44
44
45
45
css . walkDecls ( ( decl ) => {
46
46
if ( decl . value . includes ( 'url(' ) ) {
47
47
const parsed = valueParser ( decl . value ) ;
48
- const values = filterUrls ( parsed , filter ) ;
48
+ const values = filterUrls ( parsed , filter , options ) ;
49
49
50
50
if ( values . length ) {
51
51
result . push ( {
@@ -60,10 +60,39 @@ const walkDeclsWithUrl = (css, filter) => {
60
60
return result ;
61
61
} ;
62
62
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
+ }
67
96
68
97
const flatten = arr => arr . reduce ( ( acc , d ) => [ ...acc , ...d ] , [ ] ) ;
69
98
@@ -72,24 +101,23 @@ const uniq = arr => arr.reduce(
72
101
[ ] ,
73
102
) ;
74
103
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 ) ;
77
106
const paths = uniq ( flatten ( traversed . map ( item => item . values ) ) ) ;
78
107
79
- // ICSS imports {String}
80
108
const aliases = { } ;
81
109
82
110
paths . forEach ( ( url , idx ) => {
83
- // ICSS Placeholder
111
+ // CSS Content Placeholder
84
112
const alias = '${' + `CSS__URL__${ idx } ` + '}' ;
85
-
113
+
86
114
aliases [ url ] = alias ;
87
115
88
116
result . messages . push ( {
89
117
type : 'import' ,
90
118
plugin : 'postcss-icss-url' ,
91
119
import : `import CSS__URL__${ idx } from '${ url } ';\n`
92
- } )
120
+ } ) ;
93
121
} ) ;
94
122
95
123
traversed . forEach ( ( item ) => {
@@ -98,3 +126,4 @@ module.exports = postcss.plugin('postcss-icss-url', () => (css, result) => {
98
126
item . decl . value = item . parsed . toString ( ) ;
99
127
} ) ;
100
128
} ) ;
129
+
0 commit comments