You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
754 B

4 years ago
  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. };