-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
170 lines (149 loc) · 4.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const postcss = require('postcss');
const hh = require('http-https');
const isUrl = require('is-url');
const trim = require('lodash.trim');
const resolveRelative = require('resolve-relative-url');
const assign = require('lodash.assign');
const url = require('url');
const defaults = {
recursive: true,
resolveUrls: false,
modernBrowser: false,
userAgent: null,
dataUrls: false,
};
const space = postcss.list.space;
const urlRegexp = /url\(["']?.+?['"]?\)/g;
function postcssImportUrl(options) {
options = assign({}, defaults, options || {});
async function importUrl(tree, _, parentRemoteFile) {
parentRemoteFile = parentRemoteFile || tree.source.input.file;
const imports = [];
tree.walkAtRules('import', function checkAtRule(atRule) {
const params = space(atRule.params);
let remoteFile = cleanupRemoteFile(params[0]);
if (parentRemoteFile) {
remoteFile = resolveRelative(remoteFile, parentRemoteFile);
}
if (!isUrl(remoteFile)) {
return;
}
imports[imports.length] = createPromise(remoteFile, options).then(
async r => {
let newNode = postcss.parse(r.body);
let hasLayer = params.find(param => param.includes('layer'));
let hasSupports = params.find(param => param.includes('supports'));
const mediaQueries = params
.slice(hasLayer ? (hasSupports ? 3 : 2) : 1)
.join(' ');
if (mediaQueries) {
const mediaNode = postcss.atRule({
name: 'media',
params: mediaQueries,
source: atRule.source,
});
mediaNode.append(newNode);
newNode = mediaNode;
} else {
newNode.source = atRule.source;
}
if (hasSupports) {
const supportQuery = params.find(param =>
param.includes('supports'),
);
let init = supportQuery.indexOf('(');
let fin = supportQuery.indexOf(')');
let query = supportQuery.substr(init + 1, fin - init - 1);
const supportsNode = postcss.atRule({
name: 'supports',
params: `(${query})`,
source: atRule.source,
});
supportsNode.append(newNode);
newNode = supportsNode;
} else {
newNode.source = atRule.source;
}
if (hasLayer) {
const layer = params.find(param => param.includes('layer'));
let init = layer.indexOf('(');
let fin = layer.indexOf(')');
let layerName = layer.substr(init + 1, fin - init - 1);
const layerNode = postcss.atRule({
name: 'layer',
params: layerName,
source: newNode.source,
});
layerNode.append(newNode);
newNode = layerNode;
}
if (options.resolveUrls) {
// Convert relative paths to absolute paths
newNode = newNode.replaceValues(urlRegexp, { fast: 'url(' }, url =>
resolveUrls(url, remoteFile),
);
}
const importedTree = await (options.recursive
? importUrl(newNode, null, r.parent)
: Promise.resolve(newNode));
if (options.dataUrls) {
atRule.params = `url(data:text/css;base64,${Buffer.from(importedTree.toString()).toString('base64')})`;
} else {
atRule.replaceWith(importedTree);
}
},
);
});
await Promise.all(imports);
return tree;
}
return {
postcssPlugin: 'postcss-import-url',
Once: importUrl,
};
}
module.exports = postcssImportUrl;
module.exports.postcss = true;
function cleanupRemoteFile(value) {
if (value.substr(0, 3) === 'url') {
value = value.substr(3);
}
value = trim(value, '\'"()');
return value;
}
function resolveUrls(to, from) {
return 'url("' + resolveRelative(cleanupRemoteFile(to), from) + '")';
}
function createPromise(remoteFile, options) {
const reqOptions = urlParse(remoteFile);
reqOptions.headers = {};
reqOptions.headers['connection'] = 'keep-alive';
if (options.modernBrowser) {
reqOptions.headers['user-agent'] =
'Mozilla/5.0 AppleWebKit/538.0 Chrome/88.0.0.0 Safari/538';
}
if (options.userAgent) {
reqOptions.headers['user-agent'] = String(options.userAgent);
}
function executor(resolve, reject) {
const request = hh.get(reqOptions, response => {
let body = '';
response.on('data', chunk => {
body += chunk.toString();
});
response.on('end', () => {
resolve({
body: body,
parent: remoteFile,
});
});
});
request.on('error', reject);
request.end();
}
return new Promise(executor);
}
function urlParse(remoteFile) {
const reqOptions = url.parse(remoteFile);
return reqOptions;
}