npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-minifier

v0.3.0

Published

a minify tool

Downloads

51

Readme

node-minifier

What is node-minifier used for

a minify tools for web development

单元测试

travis build status

Usage

npm install node-minifier

require node-minifer

var minifier = require('node-minifier');

JS压缩

  • 使用UglifyJS2进行压缩
  • 自动保留/*! ... */类型的注释
  • 保留$, exports, require, define不被替换
  • 可以自定义需要删除掉的方法调用
//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代码进行压缩或格式化

//压缩
var minifiedJSON = minifier.minifyJSON(content);
//格式化
var formatJSON = minifier.minifyJSON(content, {indent: 2});

CSS压缩

var minifiedCSS = minifier.minifyCSS(content);

HTML压缩

var minifiedHTML = minifier.minifyHTML(content);

对CSS进行DataURI (支持webp格式)

var datauriCSS = minifier.datauri(content, {
  input: 'the-dir-of-css-file'
});

如果CSS中定义了图片以绝对路径作为访问地址,需要额外定义绝对路径

.btn{
  background: url(/img/btn.png);
}
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 API进行压缩。
minifier.minifyImage('logo.png', 'logo.min.png', function(e, data){
  if(e){
    console.log(e);
  }else{
    console.log(data.msg);
  }
});

压缩jpg - 使用jpegtran压缩jpg

minifier.optimage('input.jpg', 'output.jpg', function(err, data){
    console.log(data.saved);
});

压缩png - 使用optipng, pngcrush, pngquant, advpng压缩png

minifier.optimage('input.png', 'output.png', function(err, data){
    console.log(data.saved);
});

压缩gif - 使用gifsicle压缩gif

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.itpagespeed的压缩服务会将gif格式转换成png格式再使用pngcrush工具压缩,所以压缩比总体可能会高于optimage方法。