-
Notifications
You must be signed in to change notification settings - Fork 0
/
downs.js
99 lines (80 loc) · 2.41 KB
/
downs.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
'use strict';
const fs = require('fs');
const path = require('path');
const {
getHTML,
wFile
} = require('./helpers');
function dnsm(options) {
const {
dist, url, indexPath, jsPath, cssPath, js, css
} = {...dnsm.defaults, ...options};
if (!url) {
throw new Error('url is required');
}
if (!fs.existsSync(dist)) {
fs.mkdirSync(dist, {recursive: true});
}
fetchHTML(url, path.join(dist, indexPath), data => {
if (js) {
parseScript(data, url + jsPath, dist);
}
if (css) {
downCss(data, url + cssPath, dist);
}
});
}
dnsm.defaults = {
dist: 'dist',
url: '',
indexPath: 'index.html',
jsPath: 'static/js/',
cssPath: 'static/css/',
js: true,
css: false
};
function fetchHTML(src, dest, callback) {
getHTML(src).then(data => {
if (data === undefined) {
return console.log(`Failed to load content from \`${src}\``);
}
wFile(dest, data);
if (typeof callback === 'function') {
callback(data, src, dest);
}
});
}
function downCss(content, cssBase, distFolder) {
content.replace(/<link[^>]href=['"]?([^hf][^'"]+?\.css)[^>]*\/?\s*>/g, (all, href) => {
const match = href.match(/([^/]+\.css)/);
const name = match && match[1];
if (name) {
fetchHTML(cssBase + name, path.join(distFolder, 'css', name));
fetchHTML(cssBase + name + '.map', path.join(distFolder, 'css', name + '.map'));
}
});
}
function parseScript(content, jsBase, distFolder) {
content.replace(/<script[^>]*src\s*=['"]?([^hf][^'"]+?\.js)[^>]*><\/script>/g, (all, src) => {
const name = src.match(/([^/]+\.js)/)[1];
if (name) {
fetchHTML(jsBase + name, path.join(distFolder, 'js', name), data => {
fetchChunk(data, jsBase, distFolder);
});
fetchHTML(jsBase + name + '.map', path.join(distFolder, 'js', name + '.map'));
}
});
}
function fetchChunk(content, jsBase, distFolder) {
const match = content.match(/(\{[^}]+\})\[[^]+\]\s*\+\s*['"]\.js/);
const result = match && match[1];
if (!result) {
return;
}
const names = result.match(/(\d+):\s*['"]([^'"]+)['"]/g);
const files = names.map(item => item.replace(':"', '.').replace('"', '') + '.js');
const filemaps = files.map(item => item + '.map');
files.forEach(item => fetchHTML(jsBase + item, path.join(distFolder, 'js', item)));
filemaps.forEach(item => fetchHTML(jsBase + item, path.join(distFolder, 'js', item)));
}
module.exports = dnsm;