Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const path = require('path');
  3. const resolveFrom = require('resolve-from');
  4. const parentModule = require('parent-module');
  5. module.exports = moduleId => {
  6. if (typeof moduleId !== 'string') {
  7. throw new TypeError('Expected a string');
  8. }
  9. const parentPath = parentModule(__filename);
  10. const cwd = parentPath ? path.dirname(parentPath) : __dirname;
  11. const filePath = resolveFrom(cwd, moduleId);
  12. const oldModule = require.cache[filePath];
  13. // Delete itself from module parent
  14. if (oldModule && oldModule.parent) {
  15. let i = oldModule.parent.children.length;
  16. while (i--) {
  17. if (oldModule.parent.children[i].id === filePath) {
  18. oldModule.parent.children.splice(i, 1);
  19. }
  20. }
  21. }
  22. delete require.cache[filePath]; // Delete module from cache
  23. const parent = require.cache[parentPath]; // If `filePath` and `parentPath` are the same, cache will already be deleted so we won't get a memory leak in next step
  24. return parent === undefined ? require(filePath) : parent.require(filePath); // In case cache doesn't have parent, fall back to normal require
  25. };