Last active
August 20, 2017 07:57
-
-
Save bravelincy/0eb9dcbff387a2f60c47816efc5f0bda to your computer and use it in GitHub Desktop.
require node modules and don't cache anything
This file contains hidden or 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 path = require('path') | |
var context = path.dirname(module.parent.filename) | |
// only for relative path uri | |
module.exports = function (uri) { | |
var filename = path.resolve(context, uri) | |
var cachedModule = getCachedModule(filename) | |
if (cachedModule) { | |
cleanModulesCache(cachedModule) | |
} | |
return require(filename) | |
} | |
// clean self cache for dynamic module.parent | |
cleanModulesCache(module) | |
function getCachedModule (filename) { | |
return require.cache[filename] | |
} | |
// clean caches recursively | |
function cleanModulesCache (modules) { | |
modules = [].concat(modules) | |
modules.forEach(function (module) { | |
delete require.cache[module.filename] | |
if (module.children.length) { | |
cleanModulesCache(module.children) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment