The essence of influence is the output of spirit.
Antecedents to review
The previous article mainly shared a NodeJS module search mechanism, the search process roughly goes through path analysis, file location, compilation and execution of these three processes. Today we continue to talk about Node module parsing and NPM knowledge
Modular compilation
Each module in Node is an object, like this:
function module(id,parent){
this.id = id;
this.exports = {}
this.parent = parent;
if(parent && parent.children){
parent.children.push(this)
}
this.filename = null;
this.loaded = false;
this.children = [];
}
Copy the code
Once the file is located, Node creates a new module child object, which it loads and compiles according to the path. Of course, the loading method varies depending on the file type.
.js file
throughfs
The module is compiled after readingThe node file
Is written in C++ extension file, throughdlopen()
Method loads the file generated by the last compilationThe json file
throughfs
After reading, useJSON.parse
Parse and return the result
Node reads different types of files, such as json files:
// Native extion for .json
module._extentions['.json'] = function (module,filename) {
var content = NativeModule.require('fs').readFileSync(filename,'utf-8')
try{
module.exports = JSON.parse(content)
}catch(err){
err.message = filename + ':' + err.message
throw err;
}
}
Copy the code
How do JS modules compile?
Require,exports, and module exist in every JS file, but we didn’t define them. In the Node documentation, we can also find the __dirname and __filename variables. Where do they come from?
Node actually wraps the retrieved JS file. Added (function(exports,require,module,__filename,__dirname){in the header, added} in the tail). Such as:
(function(exports,require,module,__filename,__dirname){
var math = require('math')
exports.add = function(a,b){
return a+b
}
})
Copy the code
This isolates each module file from each other. Meanwhile, the wrapped code is executed through the native module’s runInThisContext() method, which returns a concrete object. Finally, the current module object’s exports,module, require properties are passed to function() as arguments. This way, these variables are not defined, but they do exist in the module file.
NPM and package
We probably use NPM a lot and know it as a package manager, but most of us don’t give much thought to the details.
-
What is a bag?
A package is actually an archive file, that is, a directory packaged directly as a.zip or.tar.gz format. Decompress the installation package to a directory. Conforming package directories should contain the following files
package.json
Package description filebin
Store executable binarieslib
Storing JS codedoc
documentationtest
Store unit test case code
-
Package description file
Pageage. json is used to describe information that is not relevant to the specific code.
-
The name package name
-
The description package introduction
-
Version version number
-
Keywords keywords
-
Maintainers List of maintainers
-
List of Contributors
-
License License List
-
Respositories host source code address
-
Dependencies depend on the package
-
Homepage Homepage
-
OS List of supported operating systems
-
Cpucpu architecture support list
-
Engine Supports a list of JS engines
-
Whether Builtin is built into the standard components of the underlying system
-
Directories Specifies a package directory
-
Implements a list of implemented specifications
-
Scripts Script description object
-
Common functions of NPM
NPM is so common in everyday development that I won’t describe it too much. Here’s how to distribute a package. The following commands are used to publish a package:
-
NPM init Initializes the package
-
NPM adduser Adds a user
-
NPM publish upload package
If you want to see all the packages that can be found in the current path, you can execute NPM ls
conclusion
How do JS modules compile
NPM and package
One last word
- Move your rich little hands and “like it.”
- Move your rich little hands, “click here”
- All see here, might as well “add a follow”
- Might as well “forward”, good things to remember to share
Click on the public account