Skip to content

Instantly share code, notes, and snippets.

@bravelincy
Last active August 20, 2017 07:57
Show Gist options
  • Save bravelincy/0eb9dcbff387a2f60c47816efc5f0bda to your computer and use it in GitHub Desktop.
Save bravelincy/0eb9dcbff387a2f60c47816efc5f0bda to your computer and use it in GitHub Desktop.
require node modules and don't cache anything
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