Rumah > Artikel > hujung hadapan web > Artikel untuk bercakap tentang analisis laluan modul dalam Node.js
Artikel ini akan membawa anda melalui analisis laluan modul dalam Node.js dan memperkenalkan kaedah analisis laluan modul Node Saya harap ia akan membantu semua orang.
require
Kes /Users/rainbow/Documents/前端/脚手架开发/rainbow-test
console.log(require.resolve(".")); // /Users/rainbow/Documents/前端/脚手架开发/rainbow-test/bin/index.js 输出bin/index.js的绝对路径 console.log(require.resolve.paths(".")); // [ '/Users/rainbow/Documents/前端/脚手架开发/rainbow-test/bin' ] 输出的文件可能在的路径的数组
console.log(require.resolve("yargs")); // /Users/rainbow/Documents/前端/脚手架开发/rainbow-test/node_modules/yargs/index.cjs console.log(require.resolve.paths("yargs")); /* [ '/Users/rainbow/Documents/前端/脚手架开发/rainbow-test/bin/node_modules', '/Users/rainbow/Documents/前端/脚手架开发/rainbow-test/node_modules', '/Users/rainbow/Documents/前端/脚手架开发/node_modules', '/Users/rainbow/Documents/前端/node_modules', '/Users/rainbow/Documents/node_modules', '/Users/rainbow/node_modules', '/Users/node_modules', '/node_modules', '/Users/rainbow/.node_modules', '/Users/rainbow/.node_libraries', '/usr/local/Cellar/node/14.3.0_1/lib/node' ] */
1 Nodejs resolusi laluan modul projek dilaksanakan melalui require.resolve
kaedah.
Module._resolveFileName
Module._resolveFileName
Proses teras ialah: Module._resolveLookupPahts
untuk menjana laluan yang mungkin untuk node_modules Jika laluan masuk ialah '/test/lerna/cli.js', tambahkan node_moduels
tatasusunan laluanModule._findPath
Cache pertanyaan (Menggabungkan permintaan dan laluan melalui Module._findPath
untuk menjana
x00
dan menggabungkan cacheKey
dan Module._resolveLookupPahts
jika Jika path
wujud, panggil request
untuk mendapatkan laluan sebenar fail basePath
(kunci ialah cacheKey) (Module._pathCache ialah peta) fs.realPahtSync
Module._pathCache
Proses teras: Cache pertanyaan (kunci cache ialah p. Itulah laluan yang dihasilkan. dalam Module._findPath) fs.realPahtSync
5. Prinsip pelaksanaan require.resolve.paths
ialah: Module._resolveLookupPaths
Jika ia require.resolve.paths
(laluan akar), terus kembali
/
['/node_modules']
require('yargs')
memerlukan kaedah
Apa yang sebenarnya digunakan ialah kaedah
Module._load
Module.prototype.require = function(id) { //id = 'yargs' validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string'); } requireDepth++; try { return Module._load(id, this, /* isMain */ false); } finally { requireDepth--; } };kaedah
// 参数 id = 'yargs' this={ paths: Module._nodeModulePaths(process.cwd()) }
Module._nodeModulePaths
Analisis algoritma teras bagi gelung:
// 进入mac电脑所在的逻辑: // from => /Users/rainbow/Documents/前端/脚手架开发/lerna源码/lernas //'from' is the __dirname of the module. Module._nodeModulePaths = function(from) { from = path.resolve(from); // Return early not only to avoid unnecessary work, but to *avoid* returning // an array of two items for a root: [ '//node_modules', '/node_modules' ] if (from === '/') return ['/node_modules']; const paths = []; // 关键算法代码 for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) { const code = from.charCodeAt(i); if (code === CHAR_FORWARD_SLASH) { if (p !== nmLen) paths.push(from.slice(0, last) + '/node_modules'); last = i; p = 0; } else if (p !== -1) { if (nmChars[p] === code) { ++p; } else { p = -1; } } } // Append /node_modules to handle root paths. paths.push('/node_modules'); return paths; };Kaedah
Module._load
Module._load(id, this, /* isMain */ false)
const filename = Module._resolveFilename(request, parent, isMain);
require.resolve
Penyelesaian laluan modul projek adalah melaluiDicapai dengan cara ini.
Node.js
require.resolve dilaksanakan melalui kaedah require.resolve
,
Module._resolveFileName
// node.js内置模块require的源代码 function resolve(request, options) { validateString(request, 'request'); return Module._resolveFilename(request, mod, false, options); //核心实现 } require.resolve = resolve; function paths(request) { validateString(request, 'request'); return Module._resolveLookupPaths(request, mod); //核心代码 } resolve.paths = paths;Proses Teras
Module._resolveFileName
Tentukan sama ada laluan ialah modul terbina dalam
Module._resolveLookupPahts
Module._findPath
return Module._resolveFilename(request, parent, isMain);
Module._resolveFilename = function(request, parent, isMain, options) { if (NativeModule.canBeRequiredByUsers(request)) { //是否为内置模块 return request; } let paths; // 让paths和环境变量中的paths结合 paths = Module._resolveLookupPaths(request, parent); //核心代码 if (parent && parent.filename) { // 读取filename对应的package.json文件,看是否有exports字段,当前filename = false const filename = trySelf(parent.filename, request); if (filename) { //false const cacheKey = request + '\x00' + (paths.length === 1 ? paths[0] : paths.join('\x00')); Module._pathCache[cacheKey] = filename; return filename; } } //关键代码,找到本地执行文件 // Look up the filename first, since that's the cache key. const filename = Module._findPath(request, paths, isMain, false); if (filename) return filename; // ... };
Module._resolveLookupPahts
方法
require.resolve.paths("yargs")
核心实现方法生成
[ '/Users/rainbow/Documents/前端/脚手架开发/rainbow-test/bin/node_modules', '/Users/rainbow/Documents/前端/脚手架开发/rainbow-test/node_modules', '/Users/rainbow/Documents/前端/脚手架开发/node_modules', '/Users/rainbow/Documents/前端/node_modules', '/Users/rainbow/Documents/node_modules', '/Users/rainbow/node_modules', '/Users/node_modules', '/node_modules', '/Users/rainbow/.node_modules', '/Users/rainbow/.node_libraries', '/usr/local/Cellar/node/14.3.0_1/lib/node' ]
Module._resolveLookupPaths = function(request, parent) { if (NativeModule.canBeRequiredByUsers(request)) { debug('looking for %j in []', request); return null; } // Check for node modules paths. if (request.charAt(0) !== '.' || (request.length > 1 && request.charAt(1) !== '.' && request.charAt(1) !== '/' && (!isWindows || request.charAt(1) !== '\'))){ let paths = modulePaths; if (parent != null && parent.paths && parent.paths.length) { paths = parent.paths.concat(paths); } debug('looking for %j in %j', request, paths); return paths.length > 0 ? paths : null; } // In REPL, parent.filename is null. if (!parent || !parent.id || !parent.filename) { // Make require('./path/to/foo') work - normally the path is taken // from realpath(__filename) but in REPL there is no filename const mainPaths = ['.']; debug('looking for %j in %j', request, mainPaths); return mainPaths; } debug('RELATIVE: requested: %s from parent.id %s', request, parent.id); const parentDir = [path.dirname(parent.filename)]; debug('looking for %j', parentDir); return parentDir; };
Module._findPath
核心流程
\x00
合并生成cacheKey
)(\x00
是空格的16进制)Module._resolveLookupPahts
方法生成的paths
数组,将path
与request
组成文件路径basePath
fs.realPahtSync
获取文件的真实路径fs.realPahtSync
更多node相关知识,请访问:nodejs 教程!!
Atas ialah kandungan terperinci Artikel untuk bercakap tentang analisis laluan modul dalam Node.js. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!