forked from itorr/world-ex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
105 lines (83 loc) · 3.7 KB
/
build.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
const levelsStyleText = `
#countrys>*[level="5"]{fill:#FF7E7E;}
#countrys>*[level="4"]{fill:#FFB57E;}
#countrys>*[level="3"]{fill:#FFE57E;}
#countrys>*[level="2"]{fill:#A8FFBE;}
#countrys>*[level="1"]{fill:#88AEFF;}
#countrys>*[level="w"]{fill:#edd1ff;}
`;
const replaceSVG = text => {
text = text.replace(/ transform="matrix\(1 0 0 1 (\d+)(?:\.\d+)? (\d+)(?:\.\d+)?\)" class="(.+)"/g, ' x="$1" y="$2" class="$3"')
text = text.replace(/<!--.+?-->/g, '')
text = text.replace(/\n+/g, '\n')
text = text.replace(/ xml:space="preserve"/g, '')
text = text.replace(/ style="enable-background:new 0 0 \d+ \d+;?"/g, '')
text = text.replace(/width="\d+px" height="\d+px"/g, '')
text = text.replace(/ x="0px" y="0px"/g, '')
text = text.replace(/ id="图层_1"/g, '')
text = text.replace(/ version="1.1"/g, '')
text = text.replace(/ xmlns:xlink="http:\/\/www\.w3\.org\/1999\/xlink"/g, '')
text = text.replace(/<rect y="0" class=".+?" width="2000" height="1210"\/?>/g, '')
text = text.replace(/<polygon id="(.+?)" class="(.+?)" points="([^"]+)\s{0,}"\/>/g, (all, id, c, p) => {
return `<path id="${id}" class="${c}" d="M${p.trim().replace(/[\n\r]/g, ' ').replace(/\s+/g, ' ')}z" />`
});
// <rect id="法国" x="1" y="10" class="st1" width="100" height="1000"/>
// <path id="法国" class="st1" d="M1 10h100v1000H1Z" />
text = text.replace(/<rect id="(.+?)" x="(\d+)" y="(\d+)" class="(.+?)" width="(\d+)" height="(\d+)"\/>/g, (all, id, x, y, c, w, h) => {
// console.log(x,y,w,h)
return `<path id="${id}" class="${c}" d="M${x} ${y}h${w}v${h}H${x}Z" />`
});
text = text.replace(/<style type="text\/css">/, '<style></style><style>' + levelsStyleText)
return text;
};
const ver = Math.floor(+new Date() / 10000).toString(36);
const replaceVersion = text => text.replace(/\{version\}/g, ver);
const {readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync} = require('fs');
let xml = readFileSync('nkd-ex.svg', 'utf8');
xml = replaceSVG(xml);
writeFileSync('world-fixed.svg', xml);
if (!existsSync('dist')) mkdirSync('dist');
let html = readFileSync('html/index.html', 'utf8');
html = html.replace(/<!--svg-->/, xml.replace(/^<\?xml version="1.0" encoding="utf-8"\?>\n/, ''));
html = html.replace(/\n\s+viewBox=/, ' viewBox=');
html = html.replace(/\n\s{0,}\n/g, '\n');
html = html.replace(/\s+"/g, '"');
html = html.replace(/\s+\/>/g, '/>');
html = html.replace(/(\d+)\s+([\dvV]+)/g, '$1 $2');
html = html.replace(/<style[\s\S]+<\/style>/ig, all => all.replace(/\n\s{0,}/g, ''));
html = replaceVersion(html);
const {minify} = require('html-minifier');
const options = {
includeAutoGeneratedTags: true,
removeAttributeQuotes: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortClassName: true,
useShortDoctype: true,
collapseWhitespace: true
};
html = minify(html, options);
writeFileSync('dist/index.html', html, 'utf8');
const UglifyJS = require('uglify-js');
let jsText = readFileSync('html/document.js', 'utf8');
jsText = replaceVersion(jsText);
jsText = jsText.replace(/<!--.+?-->/g, '');
jsText = jsText.replace(/^\s{0,}\/\/.+/g, '');
jsText = `(_=>{${jsText}})()`;
const minifiedJS = UglifyJS.minify({
'document.js': jsText
}, {
// drop_console: true,
// pass: 3
})
if (!minifiedJS.code) {
throw minifiedJS;
}
jsText = minifiedJS.code;
writeFileSync('dist/document.js', jsText, 'utf8');
let cssText = readFileSync('html/document.css', 'utf8');
let minifiedCSS = require("csso").minify(cssText, {restructure: true}).css;
writeFileSync('dist/document.css', minifiedCSS, 'utf8');
//copyFileSync('html/slice.woff', 'dist/slice.woff');