LocalModulesHelpers.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 lookup = (parent, mod) => {
  8. if (mod.charAt(0) !== ".") return mod;
  9. var path = parent.split("/");
  10. var segments = mod.split("/");
  11. path.pop();
  12. for (let i = 0; i < segments.length; i++) {
  13. const seg = segments[i];
  14. if (seg === "..") {
  15. path.pop();
  16. } else if (seg !== ".") {
  17. path.push(seg);
  18. }
  19. }
  20. return path.join("/");
  21. };
  22. exports.addLocalModule = (state, name) => {
  23. if (!state.localModules) {
  24. state.localModules = [];
  25. }
  26. const m = new LocalModule(name, state.localModules.length);
  27. state.localModules.push(m);
  28. return m;
  29. };
  30. exports.getLocalModule = (state, name, namedModule) => {
  31. if (!state.localModules) return null;
  32. if (namedModule) {
  33. // resolve dependency name relative to the defining named module
  34. name = lookup(namedModule, name);
  35. }
  36. for (let i = 0; i < state.localModules.length; i++) {
  37. if (state.localModules[i].name === name) {
  38. return state.localModules[i];
  39. }
  40. }
  41. return null;
  42. };