5
5
normalizeUrl ,
6
6
requestify ,
7
7
isUrlRequestable ,
8
+ isDataUrl ,
8
9
WEBPACK_IGNORE_COMMENT_REGEXP ,
9
10
} from "../utils" ;
10
11
@@ -47,7 +48,7 @@ function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
47
48
return matched && matched [ 2 ] === "true" ;
48
49
}
49
50
50
- function shouldHandleURL ( url , declaration , result ) {
51
+ function shouldHandleURL ( url , declaration , result , isSupportDataURLInNewURL ) {
51
52
if ( url . length === 0 ) {
52
53
result . warn ( `Unable to find uri in '${ declaration . toString ( ) } '` , {
53
54
node : declaration ,
@@ -56,14 +57,24 @@ function shouldHandleURL(url, declaration, result) {
56
57
return false ;
57
58
}
58
59
60
+ if ( isDataUrl ( url ) && isSupportDataURLInNewURL ) {
61
+ try {
62
+ decodeURIComponent ( url ) ;
63
+ } catch ( ignoreError ) {
64
+ return false ;
65
+ }
66
+
67
+ return true ;
68
+ }
69
+
59
70
if ( ! isUrlRequestable ( url ) ) {
60
71
return false ;
61
72
}
62
73
63
74
return true ;
64
75
}
65
76
66
- function parseDeclaration ( declaration , key , result ) {
77
+ function parseDeclaration ( declaration , key , result , isSupportDataURLInNewURL ) {
67
78
if ( ! needParseDeclaration . test ( declaration [ key ] ) ) {
68
79
return ;
69
80
}
@@ -130,7 +141,9 @@ function parseDeclaration(declaration, key, result) {
130
141
url = normalizeUrl ( url , isStringValue ) ;
131
142
132
143
// Do not traverse inside `url`
133
- if ( ! shouldHandleURL ( url , declaration , result ) ) {
144
+ if (
145
+ ! shouldHandleURL ( url , declaration , result , isSupportDataURLInNewURL )
146
+ ) {
134
147
// eslint-disable-next-line consistent-return
135
148
return false ;
136
149
}
@@ -186,7 +199,9 @@ function parseDeclaration(declaration, key, result) {
186
199
url = normalizeUrl ( url , isStringValue ) ;
187
200
188
201
// Do not traverse inside `url`
189
- if ( ! shouldHandleURL ( url , declaration , result ) ) {
202
+ if (
203
+ ! shouldHandleURL ( url , declaration , result , isSupportDataURLInNewURL )
204
+ ) {
190
205
// eslint-disable-next-line consistent-return
191
206
return false ;
192
207
}
@@ -229,7 +244,9 @@ function parseDeclaration(declaration, key, result) {
229
244
let url = normalizeUrl ( value , true ) ;
230
245
231
246
// Do not traverse inside `url`
232
- if ( ! shouldHandleURL ( url , declaration , result ) ) {
247
+ if (
248
+ ! shouldHandleURL ( url , declaration , result , isSupportDataURLInNewURL )
249
+ ) {
233
250
// eslint-disable-next-line consistent-return
234
251
return false ;
235
252
}
@@ -271,7 +288,13 @@ const plugin = (options = {}) => {
271
288
272
289
return {
273
290
Declaration ( declaration ) {
274
- const parsedURL = parseDeclaration ( declaration , "value" , result ) ;
291
+ const { isSupportDataURLInNewURL } = options ;
292
+ const parsedURL = parseDeclaration (
293
+ declaration ,
294
+ "value" ,
295
+ result ,
296
+ isSupportDataURLInNewURL
297
+ ) ;
275
298
276
299
if ( ! parsedURL ) {
277
300
return ;
@@ -292,10 +315,16 @@ const plugin = (options = {}) => {
292
315
const needKeep = await options . filter ( url ) ;
293
316
294
317
if ( ! needKeep ) {
318
+ // eslint-disable-next-line consistent-return
295
319
return ;
296
320
}
297
321
}
298
322
323
+ if ( isDataUrl ( url ) ) {
324
+ // eslint-disable-next-line consistent-return
325
+ return parsedDeclaration ;
326
+ }
327
+
299
328
const splittedUrl = url . split ( / ( \? ) ? # / ) ;
300
329
const [ pathname , query , hashOrQuery ] = splittedUrl ;
301
330
@@ -320,6 +349,7 @@ const plugin = (options = {}) => {
320
349
] ) ;
321
350
322
351
if ( ! resolvedUrl ) {
352
+ // eslint-disable-next-line consistent-return
323
353
return ;
324
354
}
325
355
0 commit comments