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.

52 lines
1.2 KiB

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