-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·186 lines (157 loc) · 4.47 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
'use strict';
const os = require('os');
const fs = require('fs').promises;
const meow = require('meow');
const ora = require('ora');
const globby = require('globby');
const makeDir = require('make-dir');
const hasExt = require('has-ext');
const upath = require('upath');
const pAll = require('p-all');
const {minify} = require('html-minifier');
const CleanCSS = require('clean-css');
const Terser = require('terser');
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
const imageminGifsicle = require('imagemin-gifsicle');
const imageminSvgo = require('imagemin-svgo');
const cli = meow(`
Usage
$ minifly <options>
Options
--output, -o Output directory (Default: minifly)
--ignore, -i Ignore specific files or directories
--minExt, -m File extensions of minified files (Default: .min)
--concurrency, -c Max number of minifiers running at the same time (Default: CPU cores)
Examples
$ minifly
$ minifly -i 'index.js,dist/*.css' -o dist
$ minifly --minExt ''
`, {
flags: {
output: {
type: 'string',
alias: 'o',
default: 'minifly'
},
ignore: {
type: 'string',
alias: 'i'
},
minExt: {
type: 'string',
alias: 'm',
default: 'min'
},
concurrency: {
type: 'string',
alias: 'c'
}
}
});
(async () => {
const spinner = ora('Minifying...').start();
await globby(['*', '{,!(node_modules)/**/}', '!*.min.*', `!{${cli.flags.ignore}}`]).then(async files => {
await makeDir(cli.flags.output);
const minifyHtml = async () => {
const html = await files.filter(name => hasExt(name, 'html'));
await html.map(async file => {
const contents = await fs.readFile(file, 'utf8');
if (contents === '') {
return;
}
const output = await minify(contents, {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
});
const path = `${cli.flags.output}/` + file.slice(-1, file.lastIndexOf('/'));
await makeDir(path);
fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, `${cli.flags.minExt}.html`)}`, output).catch(err => {
if (err) {
spinner.fail('Error!\n' + err);
}
});
});
};
const minifyCss = async () => {
const css = await files.filter(name => hasExt(name, 'css'));
await css.map(async file => {
const contents = await fs.readFile(file, 'utf8');
if (contents === '') {
return;
}
const output = await new CleanCSS().minify(contents);
const path = `${cli.flags.output}/` + file.slice(-1, file.lastIndexOf('/'));
await makeDir(path);
fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, `${cli.flags.minExt}.css`)}`, output.styles).catch(err => {
if (err) {
spinner.fail('Error!\n' + err);
}
});
});
};
const minifyJs = async () => {
const js = await files.filter(name => hasExt(name, 'js'));
await js.map(async file => {
const contents = await fs.readFile(file, 'utf8');
if (contents === '') {
return;
}
const output = await Terser.minify(contents, {
warnings: false,
compress: {
comparisons: false
},
parse: {},
mangle: true,
output: {
comments: false,
/* eslint-disable camelcase */
ascii_only: true
/* eslint-enable camelcase */
}
});
const path = `${cli.flags.output}/` + file.slice(-1, file.lastIndexOf('/'));
await makeDir(path);
fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, `${cli.flags.minExt}.js`)}`, output.code).catch(err => {
if (err) {
spinner.fail('Error!\n' + err);
}
});
});
};
const minifyImages = async () => {
const images = await files.filter(name => hasExt(name, ['jpg', 'png', 'svg', 'gif']));
await imagemin(images, {
destination: `${cli.flags.output}/images`,
plugins: [
imageminMozjpeg(),
imageminPngquant(),
imageminGifsicle(),
imageminSvgo()
]
});
};
const actions = [
() => minifyHtml(),
() => minifyCss(),
() => minifyJs(),
() => minifyImages()
];
await pAll(actions, {concurrency: Number(cli.flags.concurrency) || os.cpus().length}).then(() => {
spinner.succeed('Done!');
});
}).catch(error => {
spinner.fail('Error!\n' + error);
});
})();