index.js 754 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const path = require('path');
  3. const resolveFrom = require('resolve-from');
  4. const callerPath = require('caller-path');
  5. module.exports = moduleId => {
  6. if (typeof moduleId !== 'string') {
  7. throw new TypeError('Expected a string');
  8. }
  9. const filePath = resolveFrom(path.dirname(callerPath()), moduleId);
  10. // Delete itself from module parent
  11. if (require.cache[filePath] && require.cache[filePath].parent) {
  12. let i = require.cache[filePath].parent.children.length;
  13. while (i--) {
  14. if (require.cache[filePath].parent.children[i].id === filePath) {
  15. require.cache[filePath].parent.children.splice(i, 1);
  16. }
  17. }
  18. }
  19. // Delete module from cache
  20. delete require.cache[filePath];
  21. // Return fresh module
  22. return require(filePath);
  23. };