Created
October 12, 2014 01:14
-
-
Save magicdawn/5d6f53e1d3020cec112f to your computer and use it in GitHub Desktop.
nodejs module 热更新
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
/* | |
require -> Module.prototype.require -> Module.load(request,module) | |
require.reslove -> Module._resolveFilename(request, module); | |
*/ | |
var Module = module.constructor | |
Module.prototype._require = function(request) { | |
var res = this.require(request) | |
// delete cache | |
var filename = Module._resolveFilename(request, this); | |
delete Module._cache[filename] | |
return res; | |
}; |
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
require('./_require') | |
var timer = setInterval(function() { | |
console.log(module._require("./test")); | |
},1000) | |
setTimeout(function() { | |
timer.unref() | |
}, 1000 * 30); |
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
// 修改module.exports 看输出 | |
module.exports = 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment