@@ -22,36 +22,41 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
22
22
var urlItems = [ ] ;
23
23
24
24
function replaceImportsInString ( str ) {
25
- var tokens = str . split ( / ( \S + ) / ) ;
26
- tokens = tokens . map ( function ( token ) {
27
- var importIndex = imports [ "$" + token ] ;
28
- if ( typeof importIndex === "number" ) {
29
- return "___CSS_LOADER_IMPORT___" + importIndex + "___" ;
30
- }
31
- return token ;
32
- } ) ;
33
- return tokens . join ( "" ) ;
25
+ if ( options . import ) {
26
+ var tokens = str . split ( / ( \S + ) / ) ;
27
+ tokens = tokens . map ( function ( token ) {
28
+ var importIndex = imports [ "$" + token ] ;
29
+ if ( typeof importIndex === "number" ) {
30
+ return "___CSS_LOADER_IMPORT___" + importIndex + "___" ;
31
+ }
32
+ return token ;
33
+ } ) ;
34
+ return tokens . join ( "" ) ;
35
+ }
36
+ return str ;
34
37
}
35
38
36
- css . walkAtRules ( "import" , function ( rule ) {
37
- var values = Tokenizer . parseValues ( rule . params ) ;
38
- var url = values . nodes [ 0 ] . nodes [ 0 ] ;
39
- if ( url . type === "url" ) {
40
- url = url . url ;
41
- } else if ( url . type === "string" ) {
42
- url = url . value ;
43
- } else throw rule . error ( "Unexpected format" + rule . params ) ;
44
- values . nodes [ 0 ] . nodes . shift ( ) ;
45
- var mediaQuery = Tokenizer . stringifyValues ( values ) ;
46
- if ( loaderUtils . isUrlRequest ( url , options . root ) && options . mode === "global" ) {
47
- url = loaderUtils . urlToRequest ( url , options . root ) ;
48
- }
49
- importItems . push ( {
50
- url : url ,
51
- mediaQuery : mediaQuery
39
+ if ( options . import ) {
40
+ css . walkAtRules ( "import" , function ( rule ) {
41
+ var values = Tokenizer . parseValues ( rule . params ) ;
42
+ var url = values . nodes [ 0 ] . nodes [ 0 ] ;
43
+ if ( url . type === "url" ) {
44
+ url = url . url ;
45
+ } else if ( url . type === "string" ) {
46
+ url = url . value ;
47
+ } else throw rule . error ( "Unexpected format" + rule . params ) ;
48
+ values . nodes [ 0 ] . nodes . shift ( ) ;
49
+ var mediaQuery = Tokenizer . stringifyValues ( values ) ;
50
+ if ( loaderUtils . isUrlRequest ( url , options . root ) && options . mode === "global" ) {
51
+ url = loaderUtils . urlToRequest ( url , options . root ) ;
52
+ }
53
+ importItems . push ( {
54
+ url : url ,
55
+ mediaQuery : mediaQuery
56
+ } ) ;
57
+ rule . remove ( ) ;
52
58
} ) ;
53
- rule . remove ( ) ;
54
- } ) ;
59
+ }
55
60
56
61
css . walkRules ( function ( rule ) {
57
62
if ( rule . selector === ":export" ) {
@@ -77,31 +82,39 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
77
82
exports [ exportName ] = replaceImportsInString ( exports [ exportName ] ) ;
78
83
} ) ;
79
84
85
+ function processNode ( item ) {
86
+ switch ( item . type ) {
87
+ case "value" :
88
+ item . nodes . forEach ( processNode ) ;
89
+ break ;
90
+ case "nested-item" :
91
+ item . nodes . forEach ( processNode ) ;
92
+ break ;
93
+ case "item" :
94
+ var importIndex = imports [ "$" + item . name ] ;
95
+ if ( typeof importIndex === "number" ) {
96
+ item . name = "___CSS_LOADER_IMPORT___" + importIndex + "___" ;
97
+ }
98
+ break ;
99
+ case "url" :
100
+ if ( options . url && ! / ^ # / . test ( item . url ) && loaderUtils . isUrlRequest ( item . url , options . root ) ) {
101
+ item . stringType = "" ;
102
+ delete item . innerSpacingBefore ;
103
+ delete item . innerSpacingAfter ;
104
+ var url = item . url ;
105
+ item . url = "___CSS_LOADER_URL___" + urlItems . length + "___" ;
106
+ urlItems . push ( {
107
+ url : url
108
+ } ) ;
109
+ }
110
+ break ;
111
+ }
112
+ }
113
+
80
114
css . walkDecls ( function ( decl ) {
81
115
var values = Tokenizer . parseValues ( decl . value ) ;
82
116
values . nodes . forEach ( function ( value ) {
83
- value . nodes . forEach ( function ( item ) {
84
- switch ( item . type ) {
85
- case "item" :
86
- var importIndex = imports [ "$" + item . name ] ;
87
- if ( typeof importIndex === "number" ) {
88
- item . name = "___CSS_LOADER_IMPORT___" + importIndex + "___" ;
89
- }
90
- break ;
91
- case "url" :
92
- if ( ! / ^ # / . test ( item . url ) && loaderUtils . isUrlRequest ( item . url , options . root ) ) {
93
- item . stringType = "" ;
94
- delete item . innerSpacingBefore ;
95
- delete item . innerSpacingAfter ;
96
- var url = item . url ;
97
- item . url = "___CSS_LOADER_URL___" + urlItems . length + "___" ;
98
- urlItems . push ( {
99
- url : url
100
- } ) ;
101
- }
102
- break ;
103
- }
104
- } ) ;
117
+ value . nodes . forEach ( processNode ) ;
105
118
} ) ;
106
119
decl . value = Tokenizer . stringifyValues ( values ) ;
107
120
} ) ;
@@ -129,18 +142,22 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
129
142
130
143
var parserOptions = {
131
144
root : root ,
132
- mode : options . mode
145
+ mode : options . mode ,
146
+ url : query . url !== false ,
147
+ import : query . import !== false
133
148
} ;
134
149
135
150
var pipeline = postcss ( [
136
151
localByDefault ( {
137
152
mode : options . mode ,
138
153
rewriteUrl : function ( global , url ) {
139
- if ( ! loaderUtils . isUrlRequest ( url , root ) ) {
140
- return url ;
141
- }
142
- if ( global ) {
143
- return loaderUtils . urlToRequest ( url , root ) ;
154
+ if ( parserOptions . url ) {
155
+ if ( ! loaderUtils . isUrlRequest ( url , root ) ) {
156
+ return url ;
157
+ }
158
+ if ( global ) {
159
+ return loaderUtils . urlToRequest ( url , root ) ;
160
+ }
144
161
}
145
162
return url ;
146
163
}
0 commit comments