Near the Spring Festival, the project is over. No thing to do, so I want to learn node.js, before writing a is for the needs of the laboratory project, with C # wrote a JS code compression merged small plug-ins, then think can use node refactoring, so on to practice practicing bar, the following small series directly to everyone on the code.
The code looks like this:
/* Conforms to the COMMONJS specification/
var writepath = ' min.js ',/* default output to this directory min.js file * *
fs = require (' FS '),
r1 =/^ (. +) $/mg,/* Branch * /
r2 =/\s{2,}/g,/* go to the space *
/r3 =/([^\\]) \/\/.*/g,/* go to the line comment/
r4 =/\/\*.*?\*\//g,/* go to block note
/str = ';
Module.exports.run = function (input) {
Input.foreach (function (item) {/
* * Merge order is required, so read the file synchronously */
var data = Fs.readfilesync (item, ' UTF8 '),
lines = Data.match (r1);/* row array//////////
Lines.foreach (function (item {
item = item.replace (R3, function ($) {return $});
str = str + item;
}
); str = str.replace (R2, '). Replace (R4, ");
/* Async Write to target file
/fs.appendfile (Writepath, str, {encoding: ' UTF8 '}, function (err) {
if (err) {throw err};
Console.log (' complete ... ');
};
Although the content is small, but still want to use this to adapt to COMMOMJS modular programming, so the above module ^_^, filename: compress.js.
Here is the code that references it:
var a = require ('./compress.js ');/* Loading Compress module './' means to find local file under same directory * * *
var input = process.argv;/* Gets the console input array (process reference current processes) */
FileName: run.js
Console run:
$ node Run a.js b.js c.js ....
You can put a.js b.js c.js (relative path, also use absolute path, path positioning and other languages similar) compressed into the default min.js.
Of course, this compression is not perfect, the space is not complete enough, also no based on the flow (fortunately compressed JS files are not ^_^), and professional compression plug-ins such as: Uglify.js can not be compared to ╮(╯▽╰)╭ but practice practicing is still possible ...