get-module-name.js 1.2 KB

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