Skip to content

Commit

Permalink
Add support for JPG, PNG and SVG files, optimize concurrency & other …
Browse files Browse the repository at this point in the history
…stuff 🚀
  • Loading branch information
xxczaki committed Apr 23, 2019
1 parent d809bf3 commit 6b91f81
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
66 changes: 53 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,39 @@
'use strict';

const fs = require('fs');
const path = require('path');
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 imageminSvgo = require('imagemin-svgo');

const cli = meow(`
Usage
$ minifly <options>
Options
Options
--output, -o Output directory [default: minifly]
--ignore, -i Ignore specific files or directories
Examples
$ minifly
$ minifly --ignore 'index.js,dist/*.css'
$ minifly --ignore 'index.js,dist/*.css' -o dist
`, {
flags: {
output: {
type: 'string',
alias: 'o',
default: 'minifly'
},
ignore: {
type: 'string',
alias: 'i'
Expand All @@ -36,9 +48,9 @@ const cli = meow(`

await globby(['*', '{,!(node_modules)/**/}', '!*.min.*', `!{${cli.flags.ignore}}`]).then(async files => {
const minifyHtml = async () => {
const html = await files.filter(name => path.extname(name).substr(1) === 'html');
const html = await files.filter(name => hasExt(name, 'html'));

html.map(async file => {
await html.map(async file => {
const contents = await fs.readFileSync(file, 'utf8');

if (contents === '') {
Expand All @@ -58,7 +70,10 @@ const cli = meow(`
minifyURLs: true
});

fs.writeFile(upath.changeExt(file, '.min.html'), output, err => {
const path = await `${cli.flags.output}/` + file.substring(0, file.lastIndexOf('/'));
await makeDir(path);

fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, '.min.html')}`, output, err => {
if (err) {
spinner.fail('Error!\n' + err);
}
Expand All @@ -67,9 +82,9 @@ const cli = meow(`
};

const minifyCss = async () => {
const css = await files.filter(name => path.extname(name).substr(1) === 'css');
const css = await files.filter(name => hasExt(name, 'css'));

css.map(async file => {
await css.map(async file => {
const contents = await fs.readFileSync(file, 'utf8');

if (contents === '') {
Expand All @@ -78,7 +93,10 @@ const cli = meow(`

const output = await new CleanCSS().minify(contents);

fs.writeFile(upath.changeExt(file, '.min.css'), output.styles, err => {
const path = await `${cli.flags.output}/` + file.substring(0, file.lastIndexOf('/'));
await makeDir(path);

fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, '.min.css')}`, output.styles, err => {
if (err) {
spinner.fail('Error!\n' + err);
}
Expand All @@ -87,9 +105,9 @@ const cli = meow(`
};

const minifyJs = async () => {
const js = await files.filter(name => path.extname(name).substr(1) === 'js');
const js = await files.filter(name => hasExt(name, 'js'));

js.map(async file => {
await js.map(async file => {
const contents = await fs.readFileSync(file, 'utf8');

if (contents === '') {
Expand All @@ -111,15 +129,37 @@ const cli = meow(`
}
});

fs.writeFile(upath.changeExt(file, '.min.js'), output.code, err => {
const path = await `${cli.flags.output}/` + file.substring(0, file.lastIndexOf('/'));
await makeDir(path);

fs.writeFile(`${cli.flags.output}/${upath.changeExt(file, '.min.js')}`, output.code, err => {
if (err) {
spinner.fail('Error!\n' + err);
}
});
});
};

Promise.all([minifyHtml(), minifyCss(), minifyJs()]).then(() => {
const minifyImages = async () => {
const images = await files.filter(name => hasExt(name, ['jpg', 'png', 'svg']));

await imagemin(images, `${cli.flags.output}/images`, {
plugins: [
imageminMozjpeg(),
imageminPngquant(),
imageminSvgo()
]
});
};

const actions = [
() => minifyHtml(),
() => minifyCss(),
() => minifyJs(),
() => minifyImages()
];

await pAll(actions, {concurrency: 4}).then(() => {
spinner.succeed('Done!');
});
}).catch(error => {
Expand Down
25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.0",
"description": "🗜️ Minify different types of files easily",
"main": "index.js",
"files": [
"index.js"
],
"bin": {
"minifly": "index.js"
},
Expand All @@ -17,26 +20,38 @@
"css",
"js",
"javascript",
"images",
"jpg",
"png",
"svg",
"terser",
"clean-css",
"html-minifier",
"imagemin",
"fast",
"globby",
"concurrently"
],
"author": "Antoni Kepinski <a@kepinski.me> (https://kepinski.me)",
"repository": "https://github.com/xxczaki/minifly",
"bugs": {
"url": "https://github.com/xxczaki/minifly/issues"
},
"homepage": "https://github.com/xxczaki/minifly",
"repository": "https://github.com/xxczaki/minifly",
"bugs": {
"url": "https://github.com/xxczaki/minifly/issues"
},
"homepage": "https://github.com/xxczaki/minifly",
"license": "MIT",
"dependencies": {
"clean-css": "^4.2.1",
"globby": "^9.2.0",
"has-ext": "^1.0.1",
"html-minifier": "^4.0.0",
"imagemin": "^6.1.0",
"imagemin-mozjpeg": "^8.0.0",
"imagemin-pngquant": "^7.0.0",
"imagemin-svgo": "^7.0.0",
"make-dir": "^3.0.0",
"meow": "^5.0.0",
"ora": "^3.4.0",
"p-all": "^2.1.0",
"terser": "^3.17.0",
"upath": "^1.1.2"
},
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
- Fast and easy to use
- Uses async/await
- Minifies files concurrently
- Single source file (containing ~120 lines of code)
- Supports [multiple file types](#supported-files)

# Install
Expand Down Expand Up @@ -45,6 +44,9 @@ Examples
| HTML (*.html) | [html-minifier](https://github.com/kangax/html-minifier) |
| CSS (*.css) | [clean-css](https://github.com/jakubpawlowicz/clean-css) |
| JavaScript (*.js) | [terser](https://github.com/terser-js/terser) |
| JPG (*.jpg) | [imagemin-mozjpeg](https://github.com/imagemin/imagemin-mozjpeg) |
| PNG (*.png) | [imagemin-pngquant](https://github.com/imagemin/imagemin-pngquant) |
| SVG (*.svg) | [imagemin-svgo](https://github.com/imagemin/imagemin-svgo) |

More file types will be supported soon :unicorn:

Expand Down

0 comments on commit 6b91f81

Please sign in to comment.