A simple project packaging tool written in nodejs

Source: Internet
Author: User

Module loading and definition of a projectCodeYes:

Copy code The Code is as follows: XX. Define ('ns', ['tool/cooker'], function (){
});
// Or
XX. Define ('ns. ns2', 'tool/cookie, tool/abc', function (){
})
// Or
XX. Define ('ns', function (){
})

The JS packaging tool used is to scan the file, then match the module to be loaded, and then load the module code first.
The code of the main nodejs packaging tool is as follows:

Copy code The Code is as follows: // common module
VaR util = require ('til '),
FS = require ('fs '),
Getdeps = require ('./getdeps '),
Uglify = require ('./uglify-js '),
Removebomchar = require ('./removebom'). removebomchar,
Path = require ('path ');

VaR packagedobj ={}; // whether it has been packaged

Module. Exports = function (filepath, rootpath, opts ){
Opts = opts || {};

VaR STR = jscombo (filepath, rootpath );
If (OPTs. Unzip ){
Return STR;
} Else {
Return uglify (STR );
}
};

Function jscombo (filepaths, rootpath ){
If (util. isarray (filepaths )){
Return filepaths. Map (function (filepath ){
Filepath = path. Join (rootpath, filepath );

// Package only once
If (packagedobj [filepath]) {
Return '';
}
Packagedobj [filepath] = 1;

// Existence
If (FS. existssync (filepath )){
// Read content Asynchronously
VaR STR = FS. readfilesync (filepath, 'utf-8 ');
// Remove the BOM Header
STR = removebomchar (STR );
VaR result = getdeps (STR, rootpath );
VaR content = result. content;
Content = '//' + filepath + '\ n' + content;

// Recursive Packaging
If (result. List ){
Return jscombo (result. List, rootpath) + content;
}
// Return content
Return content;
} Else {
// Error message indicating file does not exist
Console. Error ('jscombo error: '+ filepath +' does not exsist! The path is: '+ rootpath );
Return '; alert ("' + filepath + 'does not exsist! ");';
}
}). Join ('; \ n ');

} Else {
Return jscombo ([filepaths], rootpath );
}
}

I have never studied nodejs carefully before. It is all about document scanning and writing, so the code is very cool ~

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.