UglifyJS在Node.js下的安装

第一步:

打开命令行窗口(已配置环境变量,不明白的话可以看回这篇文章),输入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.


阅读更多
上一篇node.js安装
下一篇10种方式实现跨域资源的共享
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭