Node.js Implement JS file merging gadget _node.js

Source: Internet
Author: User

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 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.