Projects are loaded by modules, similar to the use of require. js. Therefore, the module loading and defining part of the code of a js packaging tool project are as follows:
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:
The Code is as follows:
// General 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 ~