-
-
Save mgttt/aaf27e631154abc95c265ecc28289e54 to your computer and use it in GitHub Desktop.
Require a module, without going to Node.js's require cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _invalidateRequireCacheForFile = function(mmm){ | |
try{ | |
var p=require.resolve(mmm); | |
delete require.cache[p]; | |
}catch(ex){}; | |
}; | |
var try_require=function(mmm,nocache){ | |
if(nocache) _invalidateRequireCacheForFile(mmm); | |
try{ | |
return require(mmm); | |
}catch(ex){ | |
//TODO 判断如果是网络的就抓回到 tmp再require | |
//如果是本地的,尝试到 ./src/ ../上面再找找 | |
return null; | |
} | |
}; | |
module.exports=try_require; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment