-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
load.js
41 lines (38 loc) · 964 Bytes
/
load.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
let get = require('./get')
function findLinks(html, url) {
let files = html.match(/[^"]+\.css"|[^']\.css'/g)
if (!files) {
throw new Error("Can't find CSS links at " + url)
}
return files.map(i => {
let path = i.slice(0, -1)
if (/^https?:/.test(path)) {
return path
} else if (path.startsWith('//')) {
return 'https:' + path
} else {
return path.replace(/^\.?\/?/, url)
}
})
}
module.exports = async function load(succeed, urls, callback) {
let used = new Set()
await Promise.all(
urls.map(async url => {
used.add(url)
if (url.endsWith('.css')) {
callback(await get(url), url)
} else {
let files = findLinks(await get(url), url)
succeed(url)
await Promise.all(
files.map(async file => {
if (used.has(file)) return
used.add(file)
callback(await get(file), file)
})
)
}
})
)
}