Node. js implements the JS file merging tool and node. jsjs file Merging

Source: Internet
Author: User

Node. js implements the JS file merging tool and node. jsjs file Merging

As the Spring Festival approaches, the project is over and there is nothing to do, so I want to learn node. js, I wrote a small plug-in that uses c # To Compress and merge js Code to meet the needs of lab projects. Later I thought I could use node reconstruction, so I would like to train my hands, below is a small series of code for everyone.

The Code is as follows:

/* Comply with CommonJs specifications */var writePath = 'min. js',/* output to the local directory by default min. */fs = require ('fs'), r1 =/^ (. +) $/mg,/* Branch */r2 =/\ s {2,}/g,/* remove space */r3 =/([^ \]) \/\/. */g,/* Comment out */r4 = /\/\*. *? \ * \ // G,/* unblock comment */str = ''; module. exports. run = function (input) {input. forEach (function (item) {/* merge requires the order, so the file is synchronously read */var data = fs. readFileSync (item, 'utf8'), lines = data. match (r1);/* array of rows * // * concatenate a string */lines. forEach (function (item) {item = item. replace (r3, function ($1, $2) {return $2 ;}); str = str + item ;}) ;}; str = str. replace (r2 ,''). replace (r4, '');/* asynchronously write to the target file */fs. appendFile (writePath, str, {encoding: 'utf8'}, function (err) {if (err) {throw err}; console. log ('complete ........ ');});};

Although the content is small, I still want to adapt to commomJS's modular programming, So I split the above module into ^ _ ^, file name: compress. js.

The following code references it:

Var a = require ('. /compress. js ');/* load the compress module '. /'indicates searching for local files in the same directory */var input = process. argv;/* Get the console input array (process references the current process) */. run (input. slice (2)/* ignore the first two arrays */);

File Name: run. js

Console running:

$ Node run a. js B. js c. js ....

You can compress a. js B. js c. js (relative path, or absolute path, similar to other languages) to the default min. js.

Of course, this compression is not perfect, the space is not completely enough, and it is not based on the stream (Fortunately, JavaScript files are not compressed much ^_^), and professional compression plug-ins such as: uglify. javascript cannot be more practical than NLP ......

Articles you may be interested in:
  • Implementation of file replication methods and instances in Node. js
  • Node. js implements code for reading file content line by line
  • Node. js file operations
  • Node. js solves the problem of obtaining the true image file type.
  • Node. js boot script file

Related Article

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.