summaryrefslogtreecommitdiff
path: root/tools/minify.js
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-11-28 05:45:20 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-12-18 00:00:27 +0300
commit7bb6daa4bf09947142e38ff468b7f62baae110fd (patch)
treec120e9ec800525116ad922e6c4cff21064a40538 /tools/minify.js
parent16d47968b4938f3b60b97f374d45ad39bb0071b1 (diff)
esp8266 relay controller wip
Diffstat (limited to 'tools/minify.js')
-rwxr-xr-xtools/minify.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/tools/minify.js b/tools/minify.js
new file mode 100755
index 0000000..105c4eb
--- /dev/null
+++ b/tools/minify.js
@@ -0,0 +1,64 @@
+#!/usr/bin/env node
+
+const {minify: minifyJs} = require('terser')
+const {minify: minifyHtml} = require('html-minifier-terser')
+const CleanCSS = require('clean-css');
+const parseArgs = require('minimist')
+const {promises: fs} = require('fs')
+
+const argv = process.argv.slice(2)
+if (!argv.length) {
+ console.log(`usage: ${process.argv[1]} --type js|css|html filename`)
+ process.exit(1)
+}
+
+async function read() {
+ const chunks = []
+ for await (const chunk of process.stdin)
+ chunks.push(chunk)
+ return Buffer.concat(chunks).toString('utf-8')
+}
+
+const args = parseArgs(argv, {
+ string: ['type'],
+})
+
+;(async () => {
+ if (!['js', 'css', 'html'].includes(args.type))
+ throw new Error('invalid type')
+
+ const content = await read()
+
+ switch (args.type) {
+ case 'html':
+ console.log(await minifyHtml(content, {
+ collapseBooleanAttributes: true,
+ collapseInlineTagWhitespace: true,
+ collapseWhitespace: true,
+ conservativeCollapse: true,
+ html5: true,
+ includeAutoGeneratedTags: true,
+ keepClosingSlash: false,
+ minifyCSS: true,
+ minifyJS: true,
+ minifyURLs: false,
+ preserveLineBreaks: true,
+ removeComments: true,
+ removeAttributeQuotes: false,
+ sortAttributes: false,
+ sortClassName: false,
+ useShortDoctype: true,
+ }))
+ break
+
+ case 'css':
+ console.log(new CleanCSS({level:2}).minify(content).styles)
+ break
+
+ case 'js':
+ console.log((await minifyJs(content, {
+ ecma: 5
+ })).code)
+ break
+ }
+})()