get-module-name.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getModuleName;
  6. {
  7. const originalGetModuleName = getModuleName;
  8. exports.default = getModuleName = function getModuleName(rootOpts, pluginOpts) {
  9. var _pluginOpts$moduleId, _pluginOpts$moduleIds, _pluginOpts$getModule, _pluginOpts$moduleRoo;
  10. return originalGetModuleName(rootOpts, {
  11. moduleId: (_pluginOpts$moduleId = pluginOpts.moduleId) != null ? _pluginOpts$moduleId : rootOpts.moduleId,
  12. moduleIds: (_pluginOpts$moduleIds = pluginOpts.moduleIds) != null ? _pluginOpts$moduleIds : rootOpts.moduleIds,
  13. getModuleId: (_pluginOpts$getModule = pluginOpts.getModuleId) != null ? _pluginOpts$getModule : rootOpts.getModuleId,
  14. moduleRoot: (_pluginOpts$moduleRoo = pluginOpts.moduleRoot) != null ? _pluginOpts$moduleRoo : rootOpts.moduleRoot
  15. });
  16. };
  17. }
  18. function getModuleName(rootOpts, pluginOpts) {
  19. const {
  20. filename,
  21. filenameRelative = filename,
  22. sourceRoot = pluginOpts.moduleRoot
  23. } = rootOpts;
  24. const {
  25. moduleId,
  26. moduleIds = !!moduleId,
  27. getModuleId,
  28. moduleRoot = sourceRoot
  29. } = pluginOpts;
  30. if (!moduleIds) return null;
  31. if (moduleId != null && !getModuleId) {
  32. return moduleId;
  33. }
  34. let moduleName = moduleRoot != null ? moduleRoot + "/" : "";
  35. if (filenameRelative) {
  36. const sourceRootReplacer = sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : "";
  37. moduleName += filenameRelative.replace(sourceRootReplacer, "").replace(/\.(\w*?)$/, "");
  38. }
  39. moduleName = moduleName.replace(/\\/g, "/");
  40. if (getModuleId) {
  41. return getModuleId(moduleName) || moduleName;
  42. } else {
  43. return moduleName;
  44. }
  45. }