在JavaScript中,模塊之間可能出現相互引用的狀況,例如如今有三個模塊,他們之間的相互引用關係以下,大體的引用關係能夠表示爲 A -> B -> C -> A,要完成模塊A,它依賴於模塊C,可是模塊C反過來又依賴於模塊A,此時就出現了循環require。javascript
// a.js const B = require('./b.js'); console.log('B in A', B); const A = { name: 'A', childName: B.name, }; module.exports = A;
// b.js const C = require('./c.js'); console.log('C in B', C); const B = { name: 'B', childName: C.name, } module.exports = B;
// c.js const A = require('./a.js'); console.log('A in C', A); const C = { name: 'C', childName: A.name, }; module.exports = C;
那JS引擎會一直循環require下去嗎?答案是不會的,若是咱們以a.js
爲入口執行程序,C在引用A時,a.js
已經執行,不會再從新執行a.js
,所以c.js
得到的A對象是一個空對象(由於a.js
還沒執行完成)。java
在正常狀況下,JS引擎是能夠解析循環require的情形的。可是在一些低版本的小程序中,竟然出現程序一直循環require的狀況,最終致使棧溢出而報錯,實在是天坑。node
那如何解決呢,很遺憾,目前並未找到完美的方法來解決,只能找到程序中的循環require的代碼,並進行修改。爲了快速定位程序中的循環引用,寫了一段NodeJs檢測代碼來檢測進行檢測。小程序
const fs = require('fs'); const path = require('path'); const fileCache = {}; const requireLink = []; if (process.argv.length !== 3) { console.log(`please run as: node ${__filename.split(path.sep).pop()} file/to/track`); return; } const filePath = process.argv[2]; const absFilePath = getFullFilePath(filePath); if (absFilePath) { resolveRequires(absFilePath, 0); } else { console.error('file not exist:', filePath); } /** * 遞歸函數,解析文件的依賴 * @param {String} file 引用文件的路徑 * @param {Number} level 文件所在的引用層級 */ function resolveRequires(file, level) { requireLink[level] = file; for (let i = 0; i < level; i ++) { if (requireLink[i] === file) { console.log('**** require circle detected ****'); console.log(requireLink.slice(0, level + 1)); console.log(); return; } } const requireFiles = getRequireFiles(file); requireFiles.forEach(file => resolveRequires(file, level + 1)); } /** * 獲取文件依賴的文件 * @param {String} filePath 引用文件的路徑 */ function getRequireFiles(filePath) { if (!fileCache[filePath]) { try { const fileBuffer = fs.readFileSync(filePath); fileCache[filePath] = fileBuffer.toString(); } catch(err) { console.log('read file failed', filePath); return []; } } const fileContent = fileCache[filePath]; // 引入模塊的幾種形式 const requirePattern = /require\s*\(['"](.*?)['"]\)/g; const importPattern1 = /import\s+.*?\s+from\s+['"](.*?)['"]/g; const importPattern2 = /import\s+['"](.*?)['"]/g; const requireFilePaths = []; const baseDir = path.dirname(filePath); let match = null; while ((match = requirePattern.exec(fileContent)) !== null) { requireFilePaths.push(match[1]); } while ((match = importPattern1.exec(fileContent)) !== null) { requireFilePaths.push(match[1]); } while ((match = importPattern2.exec(fileContent)) !== null) { requireFilePaths.push(match[1]); } return requireFilePaths.map(fp => getFullFilePath(fp, baseDir)).filter(fp => !!fp); } /** * 獲取文件的完整絕對路徑 * @param {String} filePath 文件路徑 * @param {String} baseDir 文件路徑的相對路徑 */ function getFullFilePath(filePath, baseDir) { if (baseDir) { filePath = path.resolve(baseDir, filePath); } else { filePath = path.resolve(filePath); } if (fs.existsSync(filePath)) { const stat = fs.statSync(filePath); if (stat.isDirectory() && fs.existsSync(path.join(filePath, 'index.js'))) { return path.join(filePath, 'index.js'); } else if (stat.isFile()){ return filePath; } } else if (fs.existsSync(filePath + '.js')) { return filePath + '.js'; } return ''; }