-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
43 lines (34 loc) · 1.37 KB
/
build.js
File metadata and controls
43 lines (34 loc) · 1.37 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
const fs = require('fs');
const path = require('path');
const { minify: minifyHTML } = require('html-minifier-terser');
const { minify: minifyJS } = require('terser');
const CleanCSS = require('clean-css');
const SRC = path.join(__dirname, 'src');
const OUTPUT = path.join(__dirname, 'connectivity-check.html');
async function build() {
const html = fs.readFileSync(path.join(SRC, 'index.html'), 'utf8');
const css = fs.readFileSync(path.join(SRC, 'styles.css'), 'utf8');
const js = fs.readFileSync(path.join(SRC, 'app.js'), 'utf8');
const minCSS = new CleanCSS({ level: 2 }).minify(css).styles;
const minJSResult = await minifyJS(js, { compress: true, mangle: true });
const minJSCode = minJSResult.code;
const commitHash = process.env.COMMIT_SHA || '';
const buildHashHtml = commitHash ? ` · build ${commitHash.slice(0, 7)}` : '';
let combined = html
.replace('/* __STYLES__ */', minCSS)
.replace('/* __SCRIPT__ */', minJSCode)
.replace('<!-- __BUILD_HASH__ -->', buildHashHtml);
combined = await minifyHTML(combined, {
collapseWhitespace: true,
removeComments: true,
minifyCSS: false,
minifyJS: false,
});
fs.writeFileSync(OUTPUT, combined, 'utf8');
const size = fs.statSync(OUTPUT).size;
console.log(`Built ${OUTPUT} (${(size / 1024).toFixed(1)} KB)`);
}
build().catch(err => {
console.error(err);
process.exit(1);
});