Last active
August 10, 2016 20:28
-
-
Save madox2/e9e21ef9db801028d2be1f11bfa476a3 to your computer and use it in GitHub Desktop.
intercepting module in node
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
const M = require('module') | |
const _load = M._load; | |
M._load = function(request, ...rest) { | |
const _module = _load(request, ...rest) | |
if (request === './module1.js') { | |
return function(...args) { | |
console.log(`intercepting ${request}`) | |
_module(...args) | |
} | |
} | |
return _module; | |
} | |
require('./module1.js')() | |
require('./module2.js')() |
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 = () => console.log('hello from module1') |
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 = () => console.log('hello from module2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment