Skip to content

Commit 0d2f90d

Browse files
committed
Use whatwg-url over deprecated url apis, and swap out http-https for axios.
- Removes deprecated url apis - Fixes unlight#25 - And Axios is supported in both node.js and browser.
1 parent 8a63862 commit 0d2f90d

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

index.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const postcss = require('postcss');
2-
const hh = require('http-https');
2+
const axios = require('axios');
33
const isUrl = require('is-url');
44
const trim = require('lodash.trim');
5-
const resolveRelative = require('resolve-relative-url');
65
const assign = require('lodash.assign');
7-
const url = require('url');
86

97
const defaults = {
108
recursive: true,
@@ -25,7 +23,7 @@ function postcssImportUrl(options) {
2523
const params = space(atRule.params);
2624
let remoteFile = cleanupRemoteFile(params[0]);
2725
if (parentRemoteFile) {
28-
remoteFile = resolveRelative(remoteFile, parentRemoteFile);
26+
remoteFile = new URL(remoteFile, parentRemoteFile).href;
2927
}
3028
if (!isUrl(remoteFile)) {
3129
return;
@@ -84,13 +82,16 @@ function cleanupRemoteFile(value) {
8482
}
8583

8684
function resolveUrls(to, from) {
87-
return 'url("' + resolveRelative(cleanupRemoteFile(to), from) + '")';
85+
return 'url("' + new URL(cleanupRemoteFile(to), from).href + '")';
8886
}
8987

9088
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+
};
9495
if (options.modernBrowser) {
9596
reqOptions.headers['user-agent'] =
9697
'Mozilla/5.0 AppleWebKit/538.0 Chrome/88.0.0.0 Safari/538';
@@ -99,25 +100,16 @@ function createPromise(remoteFile, options) {
99100
reqOptions.headers['user-agent'] = String(options.userAgent);
100101
}
101102
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 => {
108105
resolve({
109-
body: body,
106+
body: response.data,
110107
parent: remoteFile,
111108
});
109+
})
110+
.catch(error => {
111+
return reject(error);
112112
});
113-
});
114-
request.on('error', reject);
115-
request.end();
116113
}
117114
return new Promise(executor);
118115
}
119-
120-
function urlParse(remoteFile) {
121-
const reqOptions = url.parse(remoteFile);
122-
return reqOptions;
123-
}

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
"test:w": "gulp test:w"
2222
},
2323
"dependencies": {
24-
"http-https": "^1.0.0",
24+
"axios": "^1.4.0",
2525
"is-url": "^1.2.4",
2626
"lodash.assign": "^4.2.0",
27-
"lodash.trim": "^4.5.1",
28-
"resolve-relative-url": "^1.0.0"
27+
"lodash.trim": "^4.5.1"
2928
},
3029
"peerDependencies": {
3130
"postcss": "^8.0.0"

0 commit comments

Comments
 (0)