LocalModulesHelpers.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const LocalModule = require("./LocalModule");
  7. const LocalModulesHelpers = exports;
  8. const lookup = (parent, mod) => {
  9. if(mod.charAt(0) !== ".") return mod;
  10. var path = parent.split("/"),
  11. segs = mod.split("/");
  12. path.pop();
  13. for(let i = 0; i < segs.length; i++) {
  14. const seg = segs[i];
  15. if(seg === "..") path.pop();
  16. else if(seg !== ".") path.push(seg);
  17. }
  18. return path.join("/");
  19. };
  20. LocalModulesHelpers.addLocalModule = (state, name) => {
  21. if(!state.localModules) state.localModules = [];
  22. const m = new LocalModule(state.module, name, state.localModules.length);
  23. state.localModules.push(m);
  24. return m;
  25. };
  26. LocalModulesHelpers.getLocalModule = (state, name, namedModule) => {
  27. if(!state.localModules) return null;
  28. if(namedModule) {
  29. // resolve dependency name relative to the defining named module
  30. name = lookup(namedModule, name);
  31. }
  32. for(let i = 0; i < state.localModules.length; i++) {
  33. if(state.localModules[i].name === name)
  34. return state.localModules[i];
  35. }
  36. return null;
  37. };
  38. module.exports = LocalModulesHelpers;