|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const meow = require('meow'); |
| 8 | +const ora = require('ora'); |
| 9 | +const globby = require('globby'); |
| 10 | +const upath = require('upath'); |
| 11 | +const {minify} = require('html-minifier'); |
| 12 | +const CleanCSS = require('clean-css'); |
| 13 | +const Terser = require('terser'); |
| 14 | + |
| 15 | +const cli = meow(` |
| 16 | + Usage |
| 17 | + $ minifly <options> |
| 18 | + |
| 19 | + Options |
| 20 | + --ignore, -i Ignore specific files or directories |
| 21 | + |
| 22 | + Examples |
| 23 | + $ minifly |
| 24 | + $ minifly --ignore 'index.js,dist/*.css' |
| 25 | +`, { |
| 26 | + flags: { |
| 27 | + ignore: { |
| 28 | + type: 'string', |
| 29 | + alias: 'i' |
| 30 | + } |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +(async () => { |
| 35 | + const spinner = ora('Minifying...').start(); |
| 36 | + |
| 37 | + await globby(['*', '{,!(node_modules)/**/}', '!*.min.*', `!{${cli.flags.ignore}}`]).then(async files => { |
| 38 | + const minifyHtml = async () => { |
| 39 | + const html = await files.filter(name => path.extname(name).substr(1) === 'html'); |
| 40 | + |
| 41 | + html.map(async file => { |
| 42 | + const contents = await fs.readFileSync(file, 'utf8'); |
| 43 | + |
| 44 | + if (contents === '') { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const output = await minify(contents, { |
| 49 | + removeComments: true, |
| 50 | + collapseWhitespace: true, |
| 51 | + removeRedundantAttributes: true, |
| 52 | + useShortDoctype: true, |
| 53 | + removeEmptyAttributes: true, |
| 54 | + removeStyleLinkTypeAttributes: true, |
| 55 | + keepClosingSlash: true, |
| 56 | + minifyJS: true, |
| 57 | + minifyCSS: true, |
| 58 | + minifyURLs: true |
| 59 | + }); |
| 60 | + |
| 61 | + fs.writeFile(upath.changeExt(file, '.min.html'), output, err => { |
| 62 | + if (err) { |
| 63 | + spinner.fail('Error!\n' + err); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + const minifyCss = async () => { |
| 70 | + const css = await files.filter(name => path.extname(name).substr(1) === 'css'); |
| 71 | + |
| 72 | + css.map(async file => { |
| 73 | + const contents = await fs.readFileSync(file, 'utf8'); |
| 74 | + |
| 75 | + if (contents === '') { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const output = await new CleanCSS().minify(contents); |
| 80 | + |
| 81 | + fs.writeFile(upath.changeExt(file, '.min.css'), output.styles, err => { |
| 82 | + if (err) { |
| 83 | + spinner.fail('Error!\n' + err); |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + }; |
| 88 | + |
| 89 | + const minifyJs = async () => { |
| 90 | + const js = await files.filter(name => path.extname(name).substr(1) === 'js'); |
| 91 | + |
| 92 | + js.map(async file => { |
| 93 | + const contents = await fs.readFileSync(file, 'utf8'); |
| 94 | + |
| 95 | + if (contents === '') { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const output = await Terser.minify(contents, { |
| 100 | + warnings: false, |
| 101 | + compress: { |
| 102 | + comparisons: false |
| 103 | + }, |
| 104 | + parse: {}, |
| 105 | + mangle: true, |
| 106 | + output: { |
| 107 | + comments: false, |
| 108 | + /* eslint-disable camelcase */ |
| 109 | + ascii_only: true |
| 110 | + /* eslint-enable camelcase */ |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + fs.writeFile(upath.changeExt(file, '.min.js'), output.code, err => { |
| 115 | + if (err) { |
| 116 | + spinner.fail('Error!\n' + err); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + }; |
| 121 | + |
| 122 | + Promise.all([minifyHtml(), minifyCss(), minifyJs()]).then(() => { |
| 123 | + spinner.succeed('Done!'); |
| 124 | + }); |
| 125 | + }).catch(error => { |
| 126 | + spinner.fail('Error!\n' + error); |
| 127 | + }); |
| 128 | +})(); |
0 commit comments