Node module loading

In Node, modules are divided into two categories

  • The built-in modules provided by Node are called core modules
  • User-written custom modules, called file modules

And if you introduce a module in Node, you mainly go through three steps

  1. path analysis
  2. file location
  3. Compile and execute

Prioritize cache loading

In Node, if the module is introduced through the require method for the second time, it will be cached first. Node will put the object compiled and executed for the first time into the cache to improve efficiency.
As for the core module and the file module, the inspection of the core module takes precedence over the file module. For example, if you define an http yourself, then it must not be found.

path analysis

Because in Node, modules are introduced through require. For different modules, the search and positioning are different in varying degrees.

(1) Core modules: mainly fs, http and other modules.

The priority of the core module is second only to cache loading. It is the fastest loading process in Node source code compilation.

(2) File modules in path form: similar torequire('./index.css')

This kind of file module will be converted to a real path by Node, and the real path will be used as an index to store the compiled result in the cache, which will be faster when loaded again and again.

(3) Custom modules: similar torequire('mongose')

This kind of module is also generally downloaded and installed through npm, and its search is the most time-consuming. And its search rules are very similar to the prototype chain in JS, but instead of __propto__, it searches up layer by layer through node_modules until the target file is found.

file location

(1) File extension analysis

CommonJS allows no file extension in the identifier. At this time, Node will spell it according to .js, .json, and .node in sequence, and then locate it to try to find it.
Since Node is single-threaded, this process may cause performance issues

(2) Directory analysis package
Sometimes, require may import a folder or a directory. At this time, Node will find the package.json file in this folder, parse the description object through JSON.parse(), and locate the file name specified by the main attribute.
If it is not found, it will search for the index in the directory by default, and search again according to .js, .json, and .node.
If none of these are found, then G, throwing an exception that the search failed.

module compilation

In Node, each file module is an object, and their definitions are as follows:

function Modal(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 = []
}

For different file types:

  • .js file: Through the fs module, the file is read synchronously and then compiled.
  • .node file: loaded and executed through C++'s dlopen()
  • .json file: Parse and return results through JSON.parse()
  • Other extensions: are loaded as .js files

Where are the three things require, exports, and module?

Not only these three, but also __filename, __dirname. Where did they all come from?

In fact, during the compilation process, Node will package the content of the JS file, adding a (function at the head (exports,require,module,__filename,__dirname) {\n, and adding a function at the end \n}).

(function (exports,require,module,__filename,__dirname) {
    
    
  const math = require('math')
})()

Generate corresponding methods for each module, and then pass in these properties and methods of the current module object. So these variables are not defined, but can be used.

おすすめ

転載: blog.csdn.net/weixin_46726346/article/details/131802263