-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
33 lines (24 loc) · 1.31 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
const fs = require('fs');
const UglifyJs = require("uglify-js");
const HtmlParser = require('node-html-parser');
if (!fs.existsSync('./dist')) fs.mkdirSync('./dist')
const callback = (error) => error ? console.error(error) : console.log('✔');
fs.copyFile('./src/ding.mp3', './dist/ding.mp3', callback);
fs.copyFile('./src/style.css', './dist/style.css', callback);
fs.copyFile('./src/favicon.ico', './dist/favicon.ico', callback);
fs.copyFile('./src/timerWorker.js', './dist/timerWorker.js', callback);
const jsCode = fs.readFileSync("./src/logic.js", "utf8");
const uglifyOptions = { mangle: { toplevel: true }, nameCache: {} };
const uglifyResult = UglifyJs.minify(jsCode, uglifyOptions);
fs.writeFileSync("./dist/logic.js", uglifyResult.code);
const mangleMap = uglifyOptions.nameCache.vars.props;
const htmlCode = fs.readFileSync('./src/index.html').toString();
const parsedHtml = HtmlParser.parse(htmlCode);
const elementsWithJs = parsedHtml.querySelectorAll('.wjs');
elementsWithJs.forEach((element) => {
const attributeValue = element.getAttribute('onclick');
const identifier = `$${attributeValue.substring(0, attributeValue.length - 2)}`;
const mangledIdentifier = `${mangleMap[identifier]}()`;
element.setAttribute('onclick', mangledIdentifier);
});
fs.writeFileSync('./dist/index.html', parsedHtml.toString());