minify
Version:
Minifier of js, css, html and img
154 lines (120 loc) • 4.21 kB
Markdown
[]: https://img.shields.io/npm/v/minify.svg?style=flat
[]: https://github.com/coderaiser/minify/actions
[]: https://github.com/coderaiser/minify/workflows/CI/badge.svg
[]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
[]: https://nodei.co/npm/minify.png?stars
[]: http://npmjs.org/package/minify
[]: https://tldrlegal.com/license/mit-license "MIT License"
[]: https://coveralls.io/github/coderaiser/minify?branch=master
[]: https://coveralls.io/repos/coderaiser/minify/badge.svg?branch=master&service=github
[](http://coderaiser.github.io/minify "Minify") - a minifier of js, css, html and img files.
To use `minify` as middleware try [Mollify](https://github.com/coderaiser/node-mollify "Mollify").
```
npm i minify -g
```
```sh
Usage: minify [options]
Options:
-h, --help display this help and exit
-v, --version display version and exit
--js minify javascript
--css minify css
--html minify html
--auto auto detect format
```
The bash command below creates a code snippet saved as `hello.js`.
Simply copy + paste the code starting with cat, including the EOT on the last line, and press <enter>.
```sh
$ cat << EOT > hello.js
const hello = 'world';
for (let i = 0; i < hello.length; i++) {
console.log(hello[i]);
}
EOT
```
Use the command `minify` followed by the path to and name of the js file intended to be minified. This will minify the code and output it to the screen.
```sh
$ minify hello.js
const hello="world";for(let l=0;l<hello.length;l++)console.log(hello[l]);
```
You can capture the output with the following:
```sh
$ minify hello.js > hello.min.js
```
You can pass input using `cat`:
```sh
cat << EOT | bin/minify.js --js
> const hello = 'world';
>
> for (let i = 0; i < hello.length; i++) {
> console.log(hello[i]);
> }
> EOT
const a='world';for(let i=0;i<a.length;i++)console.log(a[i]);
```
`Minify` can be used with `async-await` and [try-to-catch](https://github.com/coderaiser/try-to-catch):
```js
import {minify} from 'minify';
import tryToCatch from 'try-to-catch';
const options = {
html: {
removeAttributeQuotes: false,
removeOptionalTags: false,
},
};
const [error, data] = await tryToCatch(minify, './client.js', options);
if (error)
return console.error(error.message);
console.log(data);
```
For cli use these options can be provided in a JSON file named `.minify.json` like so:
```json
{
"html": {
"removeAttributeQuotes": false
},
"css": {
"compatibility": "*"
},
"js": {
"removeUnusedVariables": true,
"removeConsole": false
},
"img": {
"maxSize": 4096
}
}
```
`minify` walking up parent directories to locate and read it’s configuration file `.minify.json`.
Full documentation for options that each file type accepts can be found on the pages of the libraries used by minify to process the files:
- HTML: https://github.com/kangax/html-minifier
- CSS: https://github.com/jakubpawlowicz/clean-css
- JS: [@putout/minify](https://github.com/putoutjs/minify)
- IMG: https://github.com/Filirom1/css-base64-images
Minify sets a few defaults for HTML that may differ from the base `html-minifier` settings:
```json
{
"removeComments": true,
"removeCommentsFromCDATA": true,
"removeCDATASectionsFromCDATA": true,
"collapseWhitespace": true,
"collapseBooleanAttributes": true,
"removeAttributeQuotes": true,
"removeRedundantAttributes": true,
"useShortDoctype": true,
"removeEmptyAttributes": true,
"removeEmptyElements": false,
"removeOptionalTags": true,
"removeScriptTypeAttributes": true,
"removeStyleLinkTypeAttributes": true,
"minifyJS": true,
"minifyCSS": true
}
```
MIT