node's handy tip--Compression codeDownload ImageCompress Code
I believe many front-end students will be on the line before the compression JS code, now Gulp Webpack Grunt ... can be easily achieved. But the problem comes, these will not, it is necessary to face dozens of JS files back and forth "copy-compress-Create-paste", this is too impersonal.
It can be easily implemented with Node + uglify-js . (If you will click Node)
1. First look at the table of contents:
|--uglifyjs
|--js
|--test1.js
|--test2.js
|--uglify.js //This is the key editor of this article
2. Open the Uglifyjs folder
Uglify-js
3. Edit Uglify.js
3.1 First talk about ideas and things to achieve
1. Place files that need to be compressed in a JS folder
2. Store the name under an array
3. Add JS folder relative path and. js suffix for this set of arrays (in fact this step can be done together in the above array)
4, iterate over the array, each path is var result = uglifyjs.minify (* * per file path * *,* * compression option **)
5, will get the Result.code use FS Writefilesync write into Fs.writefilesync (* * filename * *, Result.code, ' Utf-8 ', function ( ERR) {})
3.2 is really very simple, the following is all the code
/** * Created by QRL on 2016/8/21.*/varUGLIFYJS = require (' Uglify-js '));varFS = require (' FS ');varJS = [' test1 ', ' test2 '];//just write a name and automatically add the./js to each of the preceding and added. JS suffixvarAddjs =function(JS) {varNewjs = []; Js.map (function(x) {x= './js/' +x+ '. js '; Newjs.push (x); }) returnNewjs;}varJsoptions =Addjs (JS); //Compression options varMin_options ={mangle:true,//file with output variable name replacedcompress: {sequences:true,//use the comma operator to add a continuous simple statementProperties:true,//use a good rewrite property access, such as foo["Bar"]→foo.barDead_code:true,//Remove Unreachable CodeDrop_debugger:true,//To remove the debugger and debug statementsConditionals:true,//apply optimizations for if-else and conditional expressionsEvaluate:true,//try to calculate a constant expressionBooleans:true,//multiple optimizations for Boolean contexts, such as!! A? B:c→a? B:cLoops:true,//optimization for do,while and for loops when we can statically determine the value of a conditionUnused:true,//remove functions and variables that are not referencedHoist_funs:true,//Promotion Function DeclarationHoist_vars:true,//(default: False)-raises the Var declaration (because it generally looks as if it increases the size of the output, so it defaults to false)If_return:true,//This is optimized for If/return and If/continueJoin_vars:true,//add a continuous var statementCascade:true,//for sequences, transform X, x into xandx = something (), some small optimizations for x into x = something ()Drop_console:true,//the default is False. Passing in true discards the call to the console. Function .Comparisons:true,//apply some specific optimizations for binary nodes, such as:! (a <= b) →a > B (only when insecure), try to deny binary nodes, such as. A =!b &&!c &&!d &&!e→a=! (b| | c| | d| | e) and so on.Unsafe:false //Apply "unsafe" conversions}}//Write file encapsulation functionfunctionwritefs (min, code) {fs.writefile (min, Code,' Utf-8 ',function(err) {if(ERR)Throwerr; Console.log (' Success '); })}varUglify =function(options) {Options= Options | | {}; Options.type= (Options.type | | ' Sign '). toLowerCase (); Options.outname= (Options.outname | | './js/out.min.js '); vardata =Options.data; if(Options.type = = ' sign '){ for(varIinchdata) { varresult =uglifyjs.minify (Data[i], min_options); /*remove the. js suffix*/ varmin = Data[i].replace ('. js ', '); Writefs (min+ '. Min.js ', Result.code); } }Else{min_options.wrap=true; Min_options.outsourcemap= ' Out.js.map '; varresult =uglifyjs.minify (data, min_options); Writefs (Options.outname, Result.code); }}uglify ({data:jsoptions, type:' Sign ',//Use the status sign flag for individual compression and the remainder for merge compressionOutname: './js/out.min.js '
});
The simple test under the JS folder comes out Min.js compressed file.
Download Image
See GitHub Https://api.github.com/emojis on a variety of emoji, thinking of a "open connection-picture Save as", if one or two is OK, good guy, incredibly there are more than 800, Shen to panic.
So I wanted to use node's HTTP + FS implementation to read the address
Implementation ideas
1. Create a folder First
2. Pass addresses to HTTPS for processing through JSON objects
3. Save the data obtained and write the data using WriteFile
4. Open Folder It's all there.
/** * Created by Qrlon 2016/8/13 0013.*/varHTTP = require (' http ');varHTTPS = require (' HTTPS ');varFS = require (' FS ');//Redefine Create folderfunctionmkdirsync (URL, mode, CB) {varPath = require (' path '), arr= Url.split ('/')); Mode= Mode | | 0777; CB= CB | |function(){}; if(arr[0]=== '. ')) {arr.shift (); } if(arr[0] = = ' ... ') {Arr.splice (0, 2, arr[0]+ "/" +arr[1]) } functionInner (cur) {if(!fs.existssync (cur)) {//does not exist, creates afs.mkdirsync (cur, mode)}if(arr.length) {Inner (cur+ "/" +Arr.shift ()); }Else{CB (); }} arr.length&&Inner (Arr.shift ());}//Create a folderMkdirsync ('./data ', ',function(e) {if(e) {Console.log (' Something went wrong '); }Else{Console.log ("Created successfully")}}) Mkdirsync ('./img ', ',function(e) {if(e) {Console.log (' Something went wrong '); }Else{Console.log ("Created successfully") }})varurl = ' Http://a33d82f76271fa5e5ac8.b0.upaiyun.com/apicloud/818900b273d2591478a6f3fd8a9ef0ac.txt '; http.get (URL,function(res) {vardata = '; Res.setencoding (' Utf-8 ');//defining file EncodingsRes.on (' Data ',function(chunk) {data+=Chunk; }); Res.on (' End ',function() {Fs.writefile ('./data/data.json ', data, ' Utf-8 ',function(err) {if(ERR)Throwerr; Console.log (' File write succeeded '); }); varJsondata =json.parse (data) Console.log (jsondata) for(varXinchjsondata) {Emjin (x, jsondata[x]); } })})functionemjin (x, url) {https.get (URL,function(res) {varImgdata = ' '; Res.setencoding (' binary ');//Note that this needs to be set to binary or the image will not be opened afterRes.on (' Data ',function(chunk) {Imgdata+=Chunk; }); Res.on (' End ',function() {Fs.writefile ('./img/' +x+ '. png ', imgdata, ' binary ',function(err) {if(ERR)Throwerr; Console.log (' Success '); }) }) })}
I would have used https://api.github.com/emojis this, but because of the problem before HTTP (I did not learn HTTP service)
So copy the content and cut it to http://a33d82f76271fa5e5ac8.b0.upaiyun.com/apicloud/818900b273d2591478a6f3fd8a9ef0ac.txt.
The above download requires a better network, or it will fail.
The above is just a very simple function, but a little, slowly learning.
Use node with uglify-js compression js file, bulk download pictures to local