-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
84 lines (77 loc) · 2.42 KB
/
build.js
File metadata and controls
84 lines (77 loc) · 2.42 KB
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
import path from 'path';
import fs from 'fs/promises';
import { minify as mHtml } from 'html-minifier-terser';
import CleanCSS from 'clean-css';
import { minify as mJS } from 'terser';
import chalk from 'chalk';
const dir=import.meta.dirname, log=console.log,
din=dir+'/TabPlus', dout=dir+'/TabPlusMin',
//--- Minifier Opts ---
cssOpts={returnPromise:true, level:2},
jsOpts={
ecma:2017,
format:{inline_script:false, comments:false},
compress:{passes:2, arguments:true, keep_fargs:false, keep_infinity:true, unsafe:true}
}, htmlOpts={
collapseBooleanAttributes:true, removeScriptTypeAttributes:true, removeStyleLinkTypeAttributes:true,
removeAttributeQuotes:true, removeRedundantAttributes:true, removeEmptyAttributes:true,
collapseWhitespace:true, removeComments:true, minifyURLs:true,
minifyCSS:new Object(cssOpts), minifyJS:new Object(jsOpts)
}, mCSS=new CleanCSS(cssOpts);
htmlOpts.minifyCSS.returnPromise=false;
htmlOpts.minifyJS.format.inline_script=true;
//--- Methods ---
async function rm(p) {
try {await fs.rm(p, {recursive:true})}
catch(e) {if(e.code!=='ENOENT') throw e}
}
async function minify(pin, pout, fn) {
try {
pin=path.join(pin,fn), pout=path.join(pout,fn);
let ext=path.extname(fn), hOpts=htmlOpts,f;
switch(ext) {
case '.html': case '.svg': case '.css': case '.js':
f=await fs.readFile(pin, {encoding:'utf8'});
switch(ext) {
case '.svg':
hOpts=new Object(hOpts);
hOpts.removeAttributeQuotes=false;
case '.html':
f=await mHtml(f, hOpts);
break; case '.css':
f=await mCSS.minify(f);
if(f.errors.length) throw f.errors.join(',');
f=f.styles;
break; case '.js':
f=(await mJS(f, jsOpts)).code;
if(f.endsWith(';')) f=f.slice(0,-1);
}
await fs.writeFile(pout, f);
log(chalk.cyan("- "+fn));
break; default:
await fs.copyFile(pin, pout);
log(chalk.dim("- "+fn));
}
} catch(e) {
log(chalk.red("- "+fn));
throw e;
}
}
async function recurse(func, pin=din, pout=dout) {
let pl=[], d=await fs.readdir(pin, {withFileTypes:true});
if(pout) {
try {await fs.mkdir(pout)} catch(e) {if(e.code!=='EEXIST') throw e}
}
for(let f of d) {
if(f.isFile()) pl.push(func(pin,pout,f.name));
else if(f.isDirectory()) pl.push(recurse(func,
path.join(pin,f.name), pout&&path.join(pout,f.name)));
}
await Promise.all(pl);
}
//--- Main Code ---
log(chalk.bgYellow("Clean"));
await rm(dout);
log(chalk.bgYellow("Minify HTML"));
await recurse(minify);
log(chalk.green("Done!"));