# node-minifier

> a minify tool

Latest version **0.3.0** (published 2015-10-09) · 0 weekly downloads

## Install

```sh
npm install node-minifier
pnpm add node-minifier
yarn add node-minifier
bun add node-minifier
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2015-10-09 |
| First published | 2013-01-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.8.0 |
| Dependencies | 11 |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 43 |
| Author | colorhook |
| Maintainers | colorhook |
| Keywords | minify, js minifier, css minifier, image minifier, html minifier, css datauri |

## Links

- npm: https://www.npmjs.com/package/node-minifier
- Repository: https://github.com/colorhook/att
- Issues: http://github.com/colorhook/att/issues
- npm.io page: https://npm.io/package/node-minifier

## Dependencies (11)

- [csso](https://npm.io/package/csso.md) ~1.3.6
- [fileutil](https://npm.io/package/fileutil.md) ~1.0.0
- [gifsicle](https://npm.io/package/gifsicle.md) =0.1.2
- [uglify-js](https://npm.io/package/uglify-js.md) =2.4.8
- [node-advpng](https://npm.io/package/node-advpng.md) ~0.0.3
- [optipng-bin](https://npm.io/package/optipng-bin.md) =0.2.6
- [jpegtran-bin](https://npm.io/package/jpegtran-bin.md) =0.1.7
- [node-smushit](https://npm.io/package/node-smushit.md) ~0.5.0
- [html-minifier](https://npm.io/package/html-minifier.md) ~0.4.5
- [node-pngcrush](https://npm.io/package/node-pngcrush.md) *
- [node-pngquant-native](https://npm.io/package/node-pngquant-native.md) *

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2015-10-09
- 0.2.0 — 2014-08-20
- 0.1.9 — 2014-07-15
- 0.1.8 — 2014-07-15
- 0.1.7 — 2014-04-16
- 0.1.6 — 2014-01-07
- 0.1.5 — 2014-01-06
- 0.1.4 — 2013-12-25
- 0.1.3 — 2013-12-17
- 0.1.2 — 2013-12-03
- 0.1.1 — 2013-09-03
- 0.1.0 — 2013-08-17
- 0.0.6 — 2013-06-20
- 0.0.5 — 2013-06-19
- 0.0.4 — 2013-04-24
- … 3 more at https://npm.io/package/node-minifier/versions

## README

node-minifier
=====

What is node-minifier used for
-------
a minify tools for web development

单元测试
------
[![travis build status](https://api.travis-ci.org/colorhook/node-minifier.png)](https://www.travis-ci.org/colorhook/node-minifier)

Usage
-------

```
npm install node-minifier
```


> require `node-minifer`

```js
var minifier = require('node-minifier');
```

JS压缩
---------

* 使用`UglifyJS2`进行压缩
* 自动保留`/*! ... */`类型的注释
* 保留`$`, `exports`, `require`, `define`不被替换
* 可以自定义需要删除掉的方法调用

```js
//js中有个全局方法名为onMessageFromSWF, 这个方法名称不能被mangle, 可以把它定义成保留字。
//console的所有方法默认被移除，Y.log, Kissy.log, S.log也可以自定义移除。
//不要在console.log调用中改变逻辑，避免产生side effect
var minifiedJS = minifier.minifyJS(jscontent, {
  expect: ['onMessageFromSWF'],
  banner: '在文件头部加入的内容',
  footer: '在文件尾部加入的内容',
  remove: ['console', 'Y.log', 'Kissy.log', 'S.log']
});


//默认将中文转成Unicode编码，可以通过下面的方式不转码
this.minifyJS(jscontent, {
  ascii_only: false
});
```

JSON压缩
---------

`minifyJSON`方法可以对JSON代码进行压缩或格式化

```js
//压缩
var minifiedJSON = minifier.minifyJSON(content);
//格式化
var formatJSON = minifier.minifyJSON(content, {indent: 2});
```



CSS压缩
---------

```js
var minifiedCSS = minifier.minifyCSS(content);
```

HTML压缩
---------

```js
var minifiedHTML = minifier.minifyHTML(content);
```

对CSS进行DataURI (支持webp格式)
---------

```js
var datauriCSS = minifier.datauri(content, {
  input: 'the-dir-of-css-file'
});
```

如果CSS中定义了图片以绝对路径作为访问地址，需要额外定义绝对路径

```css
.btn{
  background: url(/img/btn.png);
}
```

```js
var datauriCSS = minifier.datauri(content, {
  input: 'the-dir-of-css-file',
  workspace: '/var/www/home-website/static/img'
});
```



图片压缩
---------
`minifyImage`方法会先尝试使用`optimage`方法压缩图片，如果失败，则降级到使用`smushit`方法压缩。

* `optimage` - 使用本地的压缩工具压缩。
* `smushit` - 使用[`smush.it`](http://smush.it/) API进行压缩。

```js
minifier.minifyImage('logo.png', 'logo.min.png', function(e, data){
  if(e){
    console.log(e);
  }else{
    console.log(data.msg);
  }
});
```


> 压缩jpg - 使用`jpegtran`压缩jpg

```js
minifier.optimage('input.jpg', 'output.jpg', function(err, data){
    console.log(data.saved);
});
```

> 压缩png - 使用`optipng`, `pngcrush`, `pngquant`, `advpng`压缩png

```js
minifier.optimage('input.png', 'output.png', function(err, data){
    console.log(data.saved);
});
```

> 压缩gif - 使用`gifsicle`压缩gif

```js
minifier.optimage('input.gif', 'output.gif', function(err, data){
    console.log(data.saved);
});
```

> 图片压缩统计

| 图片类型（数量）  | 压缩前(byte) | smush.it压缩掉(byte) | pagespeed压缩掉 | optimage压缩掉(byte)  | smush.it平均压缩% | optimage平均压缩% | pagespeed平均压缩% | smush.it总计压缩% | optimage总计压缩% | pagespeed总计压缩% |  
| :---------        | :--------    | :---------           | :---------      | :---------            | :---------        | :---------        | :---------         | :---------        | :---------        | :---------         |
|gif(7)             | 862177       | 26780                | 17074           | 23327                 | 22.49%            | 14.28%            | 6.14%              | 3.11%             | 2.71%             | 1.98%              |
|png(21)            | 108978       | 20544                | 14286           | 39633                 | 46.73%            | 54.48%            | 38.29%             | 18.85%            | 36.37%            | 13.11%             |
|jpg(30)            | 2497858      | 156327               | 210469          | 210469                | 3.48%             | 11.6%             | 11.6%              | 6.26%             | 8.43%             | 8.43%              |

PS: `smush.it`和`pagespeed`的压缩服务会将`gif`格式转换成`png`格式再使用`pngcrush`工具压缩，所以压缩比总体可能会高于`optimage`方法。

---
_Source: https://npm.io/package/node-minifier · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
