Use UglifyJS under node to compress and merge JS files, nodeuglifyjs
Currently, the latest version of UglifyJS is 2.8.13. The main function is JavaScript compression and merging. Go to the tutorial below:
Installation:
<span style="font-size:18px;color:#006600;">npm install uglify-js -g</span>
Install uglifyjs as a global variable so that you can use it anywhere.
<Span style = "color: #006600;"> The following is a Chinese explanation of the shell command: * source-map [string] to generate the source map file. *-Source-map-root [string]: Specifies the location of the source map generated. *-Source-map-url [string]: Specifies the Website access address of source map. *-Source-map-include-sources: set the source file to be included in the source map. *-In-source-map: Custom source map, used for source map generated by other tools. *-Screw-ie8, used to generate code that is fully compatible with the IE6-8. *-Expr: parses an expression or JSON. *-P,-prefix [string], skipping the prefix of the original file name is used to specify the relative path of the source file, source map, and output file. *-O,-output [string], and output to the file. *-B,-beautify [string], and output formatted files. *-M,-mangle [string], output the file after the variable name is replaced. *-R,-reserved [string], retain the variable name, and exclude the mangle process. *-C,-compress [string], output the compressed file. *-D,-define [string], global definition. *-E,-enclose [string], merge all code into a function, and provide a configurable parameter list. *-Comments [string]: Add annotation parameters, such as @ license and @ preserve. *-Preamble [string]: adds a description. *-Stats: displays the running status. *-Acorn: Use Acorn for parsing. *-Spidermonkey: parses files in the SpiderMonkey format, such as JSON. *-Self: Pack UglifyJS2 together as the dependent library. *-Wrap: combines all the code into a function. *-Export-all, used together with-wrap, is automatically output to the global environment. *-Lint: displays environment exceptions. *-V,-verbose, print the running log details. *-V,-version: print the version number. *-Noerr: Ignore the incorrect command line parameters. </Span>
# UglifyJS2 usage
> Two methods are available for UglifyJS2.
1. shell Command call
2. api call
Shell command:
Merge and compress the start. js and test. js JS files.
~> Uglifyjs start. js test. js-o new. min. js -- source-map new. min. js. map
API call:
var fs = require('fs');var uglifyjs = require("uglify-js");var result = uglifyjs.minify("../test.js",{ mangle:false});
The above basic core method minify. Let's take a look at this method separately.
This is a very intelligent method with two parameters in total
First parameter *
The first parameter can be a string, path, and path array;
1. String Parameters
The javascript code we wrote can be directly used as a string parameter function, but a second parameter is required to tell the function that this is the javascript source code string.
Var result = uglifyjs. minify ("var fun = function () {alert ('itkingone blog') ;};", {mangle: false, fromString: true ,});
The first parameter is passed in the javascript source code. The formString: true in the second parameter indicates the minify function. The preceding parameter is the javascript source code to be compressed.
String path
This is a method supported by the function by default. You can use a single parameter to directly specify a path to the javascript file to be compressed. Of course, you can use two parameters.
var result = uglifyjs.minify("../test.js");
The preceding function execution will compress test. js in my parent directory. The default parameter indicates the file path.
Multiple paths are specified in the array.
This parameter can be a parameter, but it is an array ['path 1', 'path 2 ', 'path 3'] similar to this, the result is to compress all the javascript code in the above path and return it to the result object. We will separately discuss the return value of result later.
var result = uglifyjs.minify([ "../test.js", "../mian.js"]);
Second parameter *
Parameter description
RomString attribute (default false) specifies that the string in the first parameter is the javascript source code.
The default value of the mangle attribute is true. If it is set to false, obfuscation compression is not performed.
The width and max-line-len attributes are described as follows. The length of the compressed file should be used here.
The outSourceMap attribute is used to specify the value of the file attribute after the result. map string is converted to the Object value returned by the function.
The sourceRoot attribute is used to specify the value of the sourceRoot attribute after the result. map string is converted to Object.
Returned result *
The returned result is an object. The code attribute corresponds to the compressed script.
{"Code": "Here is the compressed javascript script", "map": null}
The above method of compressing and merging JS files using UglifyJS under node is all the content that I have shared with you. I hope you can give me a reference and support me a lot.