Skip to content

Commit 0611dc4

Browse files
committed
Initial commit
1 parent 04c625b commit 0611dc4

6 files changed

Lines changed: 231 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ typings/
5858
.env
5959

6060
# next.js build output
61-
.next
61+
.next

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "node"

index.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
})();

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "minifly",
3+
"version": "1.0.0",
4+
"description": "🗜️ Minify different types of files easily",
5+
"main": "index.js",
6+
"bin": {
7+
"minifly": "index.js"
8+
},
9+
"scripts": {
10+
"test": "xo"
11+
},
12+
"keywords": [
13+
"minifly",
14+
"minify",
15+
"minifier",
16+
"html",
17+
"css",
18+
"js",
19+
"javascript",
20+
"tenser",
21+
"clean-css",
22+
"htmlmin",
23+
"fast",
24+
"globby"
25+
],
26+
"author": "Antoni Kepinski <a@kepinski.me> (https://kepinski.me)",
27+
"repository": "https://github.com/xxczaki/minifly",
28+
"bugs": {
29+
"url": "https://github.com/xxczaki/minifly/issues"
30+
},
31+
"homepage": "https://github.com/xxczaki/minifly",
32+
"license": "MIT",
33+
"dependencies": {
34+
"clean-css": "^4.2.1",
35+
"globby": "^9.2.0",
36+
"html-minifier": "^4.0.0",
37+
"meow": "^5.0.0",
38+
"ora": "^3.4.0",
39+
"terser": "^3.17.0",
40+
"upath": "^1.1.2"
41+
},
42+
"devDependencies": {
43+
"xo": "*"
44+
}
45+
}

readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Minifly 🗜️
2+
3+
> Minify different types of files easily
4+
5+
[![Build Status](https://travis-ci.org/xxczaki/minifly.svg?branch=master)](https://travis-ci.org/xxczaki/minifly)
6+
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
7+
8+
# Highlights
9+
10+
- Zero-config
11+
- Fast and easy to use
12+
- Uses async/await
13+
- Minifies files concurrently
14+
- Single source file (containing ~120 lines of code)
15+
- Supports [multiple file types](#supported-files)
16+
17+
# Install
18+
19+
```bash
20+
npm install --global minifly
21+
```
22+
You can also use `npx`:
23+
```bash
24+
npx minifly
25+
```
26+
27+
# Usage
28+
29+
```bash
30+
Usage
31+
$ minifly <options>
32+
33+
Options
34+
--ignore, -i Ignore specific files or directories
35+
36+
Examples
37+
$ minifly
38+
$ minifly --ignore 'index.js,dist/*.css'
39+
```
40+
41+
## Supported files
42+
43+
| Type | Minifier |
44+
| ------------- | ------------- |
45+
| HTML (*.html) | [html-minifier](https://github.com/kangax/html-minifier) |
46+
| CSS (*.css) | [clean-css](https://github.com/jakubpawlowicz/clean-css) |
47+
| JavaScript (*.js) | [terser](https://github.com/terser-js/terser) |
48+
49+
More file types will be supported soon :unicorn:
50+
51+
## License
52+
53+
MIT

0 commit comments

Comments
 (0)