UglifyJS在Node.js下的安装

原创 2012年02月17日 14:41:11

第一步:

打开命令行窗口(已配置环境变量,不明白的话可以看回这篇文章),输入npm install uglify-js -g


该窗口表示安装成功,接着配置环境变量,输入红色区域信息


C:\Users\Administrator\AppData\Roaming\npm


安装完成。验证功能

uglifyjs TY.js -> TY-min.js


详细参数配置:

Supported options:

  • -b or --beautify — output indented code; when passed, additional options control the beautifier:
    • -i N or --indent N — indentation level (number of spaces)
    • -q or --quote-keys — quote keys in literal objects (by default, only keys that cannot be identifier names will be quotes).
  • --ascii — pass this argument to encode non-ASCII characters as \uXXXX sequences. By default UglifyJS won’t bother to do it and will output Unicode characters instead. (the output is always encoded in UTF8, but if you pass this option you’ll only get ASCII).
  • -nm or --no-mangle — don’t mangle names.
  • -nmf or --no-mangle-functions – in case you want to mangle variable names, but not touch function names.
  • -ns or --no-squeeze — don’t call ast_squeeze() (which does various optimizations that result in smaller, less readable code).
  • -mt or --mangle-toplevel — mangle names in the toplevel scope too (by default we don’t do this).
  • --no-seqs — when ast_squeeze() is called (thus, unless you pass --no-squeeze) it will reduce consecutive statements in blocks into a sequence. For example, “a = 10; b = 20; foo();” will be written as “a=10,b=20,foo();”. In various occasions, this allows us to discard the block brackets (since the block becomes a single statement). This is ON by default because it seems safe and saves a few hundred bytes on some libs that I tested it on, but pass --no-seqs to disable it.
  • --no-dead-code — by default, UglifyJS will remove code that is obviously unreachable (code that follows a returnthrow,break or continue statement and is not a function/variable declaration). Pass this option to disable this optimization.
  • -nc or --no-copyright — by default, uglifyjs will keep the initial comment tokens in the generated code (assumed to be copyright information etc.). If you pass this it will discard it.
  • -o filename or --output filename — put the result in filename. If this isn’t given, the result goes to standard output (or see next one).
  • --overwrite — if the code is read from a file (not from STDIN) and you pass --overwrite then the output will be written in the same file.
  • --ast — pass this if you want to get the Abstract Syntax Tree instead of JavaScript as output. Useful for debugging or learning more about the internals.
  • -v or --verbose — output some notes on STDERR (for now just how long each operation takes).
  • -d SYMBOL[=VALUE] or --define SYMBOL[=VALUE] — will replace all instances of the specified symbol where used as an identifier (except where symbol has properly declared by a var declaration or use as function parameter or similar) with the specified value. This argument may be specified multiple times to define multiple symbols - if no value is specified the symbol will be replaced with the value true, or you can specify a numeric value (such as 1024), a quoted string value (such as =”object”= or =’https://github.com’), or the name of another symbol or keyword (such as =null or document). This allows you, for example, to assign meaningful names to key constant values but discard the symbolic names in the uglified version for brevity/efficiency, or when used wth care, allows UglifyJS to operate as a form of conditional compilation whereby defining appropriate values may, by dint of the constant folding and dead code removal features above, remove entire superfluous code blocks (e.g. completely remove instrumentation or trace code for production use). Where string values are being defined, the handling of quotes are likely to be subject to the specifics of your command shell environment, so you may need to experiment with quoting styles depending on your platform, or you may find the option --define-from-module more suitable for use.
  • -define-from-module SOMEMODULE — will load the named module (as per the NodeJS require() function) and iterate all the exported properties of the module defining them as symbol names to be defined (as if by the --define option) per the name of each property (i.e. without the module name prefix) and given the value of the property. This is a much easier way to handle and document groups of symbols to be defined rather than a large number of --define options.
  • --unsafe — enable other additional optimizations that are known to be unsafe in some contrived situations, but could still be generally useful. For now only these:
    • foo.toString() ==> foo+””
    • new Array(x,…) ==> [x,…]
    • new Array(x) ==> Array(x)
  • --max-line-len (default 32K characters) — add a newline after around 32K characters. I’ve seen both FF and Chrome croak when all the code was on a single line of around 670K. Pass –max-line-len 0 to disable this safety feature.
  • --reserved-names — some libraries rely on certain names to be used, as pointed out in issue #92 and #81, so this option allow you to exclude such names from the mangler. For example, to keep names require and $super intact you’d specify –reserved-names “require,$super”.
  • --inline-script – when you want to include the output literally in an HTML <script> tag you can use this option to prevent</script from showing up in the output.
  • --lift-vars – when you pass this, UglifyJS will apply the following transformations (see the notes in API,ast_lift_variables):
    • put all var declarations at the start of the scope
    • make sure a variable is declared only once
    • discard unused function arguments
    • discard unused inner (named) functions
    • finally, try to merge assignments into that one var declaration, if possible.


Windows下安装和使用UglifyJS

现在NodeJS也有windows版本了,所以写了本文,我们用原生的NodeJS来运行UglifyJS。 一、下载并安装NodeJS for Windows 0.6.5 或进入NodeJS官网下载最...

基于node的uglifyjs压缩JS

首先安装nodejs(windows下安装nodejs) 1.下载安装文件 下载地址:官网http://www.nodejs.org/download/ 2.安装文件 3...
  • ywj5200
  • ywj5200
  • 2016年12月29日 15:59
  • 1713

node下使用UglifyJS压缩合并JS文件

现在UglifyJS的最新版本为 2.8.13,主要功能为JS的压缩和合并,下面直接进入教程: 安装: npm install uglify-js -g 将uglifyjs安装为全局变量,方便我...

js压缩工具uglify-js--nodejs常用模块(3)

模块介绍 uglify-js是主流的js合并压缩工具,jquery现在用的就是这个工具。支持功能有:文件的合并,生产map文件,去注释去空格,混淆压缩缺点:这个工具只针对js,至少官方没有说明支...

Windows下安装和使用UglifyJS

现在NodeJS也有windows版本了,所以写了本文,我们用原生的NodeJS来运行UglifyJS。 一、下载并安装NodeJS for Windows 0.6.5 或进入NodeJS官网下载最...

Javascript 严格模式详解 strict 模式

今天使用jquery.validate.js 发现如下错误:0x800a13b3 - JavaScript 运行时错误: strict 模式下不允许访问函数或参数对象的“caller”属性 查询...

使用nodejs插件UglifyJS2压缩js文件

  • 2017年06月10日 22:43
  • 64KB
  • 下载

js压缩工具uglifyJS java版

  • 2011年10月25日 16:46
  • 43KB
  • 下载

用UglifyJS2合并压缩混淆JS代码

转自:http://blog.fens.me/nodejs-uglifyjs2-js/ 这文章我没有仔细读,主要是想保留uglifyjs的参数那块 前言 做Web前端...

用UglifyJS2合并压缩混淆JS代码

前言 做Web前端开发,总是要考虑页面的打开速度,如果文件数量越少、文件长度越小,就可以直接的提升网页的访问速度。 但在开发的时候,为了保证代码的可读性,我们写的程序文件会很多而且很大,这...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UglifyJS在Node.js下的安装
举报原因:
原因补充:

(最多只允许输入30个字)