-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.js
77 lines (66 loc) · 2.6 KB
/
run.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
const fs = require("fs");
const path = require('path');
const UglifyJS = require("uglify-js");
const DEBUG = !!process.env.DEBUG;
const DIST = process.env.DIST;
const devFileStr = fs.readFileSync("./dev.html", "utf-8");
const [html, js] = devFileStr
.split("<script>")
.map((part) => part.replace("</script>", "").trim());
/** @type {UglifyJS.MinifyOptions} */
let minifyOpts = {
mangle: {
eval: true,
toplevel: false
},
compress: {
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true,
drop_console: true,
unsafe: true,
},
output: {
quote_style: 3
}
};
const readableMinified = UglifyJS.minify(js, minifyOpts).code;
minifyOpts.mangle.toplevel = true;
const minified = UglifyJS.minify(js, minifyOpts).code;
const prefix = "data:text/html;charset=utf-8,";
const htmlStart = encodeURIComponent(html + '<script>eval(atob("');
const code = Buffer.from(minified).toString("base64");
const htmlEnd = encodeURIComponent('"))</script>');
const url = prefix + htmlStart + code + htmlEnd;
if (DEBUG) {
console.log({
js,
readableMinified,
minified,
jsLength: js.length,
minifiedLength: minified.length,
encodedLength: code.length,
minifySaved: js.length - minified.length,
urlLength: url.length,
});
}
console.log(url);
// Write output files
const fileStr = html + '<script>' + minified + '</script>';
// if we aren't build the distribution builds, just output the index.html file in current working directory
if (!DIST) {
fs.writeFileSync('./index.html', fileStr)
process.exit(0);
}
// otherwise, we'll build the distribution version of the file and write it to the dist/ dir
const outFile = path.join(__dirname, 'dist', DIST+'.html');
const footerJsPreTag = `<p>Minified javascript code (${minified.length} bytes)</p><pre id=minified></pre>`;
const footerUrlPreTag = `<p>Encoded data url (${url.length} bytes)</p><pre id=url></pre>`;
const footerAnchorsTags = `<p><a href="https://url-snake.netlify.app/">Mobile friendly version</a> · <a href="https://url-snake.netlify.app/golf.html">Code golf'd version</a> · <a href="https://github.com/lukebatchelor/url-snake/tree/main">Github</a></p>`;
const footerScriptsAndStyles = `<script>minified.innerText = '${minified}';url.innerText='${url}'</script><style>pre{max-width: 60vmin;white-space:pre-line;word-break:break-all;background:#999;padding:1em;}style,script{display: none;}</style>`
const footerStr = footerJsPreTag + footerUrlPreTag + footerAnchorsTags + footerScriptsAndStyles;
fs.writeFileSync(outFile, fileStr + footerStr);