1
1
const postcss = require ( 'postcss' ) ;
2
- const hh = require ( 'http-https ' ) ;
2
+ const axios = require ( 'axios ' ) ;
3
3
const isUrl = require ( 'is-url' ) ;
4
4
const trim = require ( 'lodash.trim' ) ;
5
- const resolveRelative = require ( 'resolve-relative-url' ) ;
6
5
const assign = require ( 'lodash.assign' ) ;
7
- const url = require ( 'url' ) ;
8
6
9
7
const defaults = {
10
8
recursive : true ,
@@ -25,7 +23,7 @@ function postcssImportUrl(options) {
25
23
const params = space ( atRule . params ) ;
26
24
let remoteFile = cleanupRemoteFile ( params [ 0 ] ) ;
27
25
if ( parentRemoteFile ) {
28
- remoteFile = resolveRelative ( remoteFile , parentRemoteFile ) ;
26
+ remoteFile = new URL ( remoteFile , parentRemoteFile ) . href ;
29
27
}
30
28
if ( ! isUrl ( remoteFile ) ) {
31
29
return ;
@@ -84,13 +82,16 @@ function cleanupRemoteFile(value) {
84
82
}
85
83
86
84
function resolveUrls ( to , from ) {
87
- return 'url("' + resolveRelative ( cleanupRemoteFile ( to ) , from ) + '")' ;
85
+ return 'url("' + new URL ( cleanupRemoteFile ( to ) , from ) . href + '")' ;
88
86
}
89
87
90
88
function createPromise ( remoteFile , options ) {
91
- const reqOptions = urlParse ( remoteFile ) ;
92
- reqOptions . headers = { } ;
93
- reqOptions . headers [ 'connection' ] = 'keep-alive' ;
89
+ const reqOptions = {
90
+ url : remoteFile ,
91
+ headers : {
92
+ connection : 'keep-alive' ,
93
+ } ,
94
+ } ;
94
95
if ( options . modernBrowser ) {
95
96
reqOptions . headers [ 'user-agent' ] =
96
97
'Mozilla/5.0 AppleWebKit/538.0 Chrome/88.0.0.0 Safari/538' ;
@@ -99,25 +100,16 @@ function createPromise(remoteFile, options) {
99
100
reqOptions . headers [ 'user-agent' ] = String ( options . userAgent ) ;
100
101
}
101
102
function executor ( resolve , reject ) {
102
- const request = hh . get ( reqOptions , response => {
103
- let body = '' ;
104
- response . on ( 'data' , chunk => {
105
- body += chunk . toString ( ) ;
106
- } ) ;
107
- response . on ( 'end' , ( ) => {
103
+ axios ( reqOptions )
104
+ . then ( response => {
108
105
resolve ( {
109
- body : body ,
106
+ body : response . data ,
110
107
parent : remoteFile ,
111
108
} ) ;
109
+ } )
110
+ . catch ( error => {
111
+ return reject ( error ) ;
112
112
} ) ;
113
- } ) ;
114
- request . on ( 'error' , reject ) ;
115
- request . end ( ) ;
116
113
}
117
114
return new Promise ( executor ) ;
118
115
}
119
-
120
- function urlParse ( remoteFile ) {
121
- const reqOptions = url . parse ( remoteFile ) ;
122
- return reqOptions ;
123
- }
0 commit comments