module.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const path = require('path');
  3. const utils = require('@sentry/utils');
  4. /** normalizes Windows paths */
  5. function normalizeWindowsPath(path) {
  6. return path
  7. .replace(/^[A-Z]:/, '') // remove Windows-style prefix
  8. .replace(/\\/g, '/'); // replace all `\` instances with `/`
  9. }
  10. /** Creates a function that gets the module name from a filename */
  11. function createGetModuleFromFilename(
  12. basePath = process.argv[1] ? utils.dirname(process.argv[1]) : process.cwd(),
  13. isWindows = path.sep === '\\',
  14. ) {
  15. const normalizedBase = isWindows ? normalizeWindowsPath(basePath) : basePath;
  16. return (filename) => {
  17. if (!filename) {
  18. return;
  19. }
  20. const normalizedFilename = isWindows ? normalizeWindowsPath(filename) : filename;
  21. // eslint-disable-next-line prefer-const
  22. let { dir, base: file, ext } = path.posix.parse(normalizedFilename);
  23. if (ext === '.js' || ext === '.mjs' || ext === '.cjs') {
  24. file = file.slice(0, ext.length * -1);
  25. }
  26. if (!dir) {
  27. // No dirname whatsoever
  28. dir = '.';
  29. }
  30. const n = dir.lastIndexOf('/node_modules');
  31. if (n > -1) {
  32. return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`;
  33. }
  34. // Let's see if it's a part of the main module
  35. // To be a part of main module, it has to share the same base
  36. if (dir.startsWith(normalizedBase)) {
  37. let moduleName = dir.slice(normalizedBase.length + 1).replace(/\//g, '.');
  38. if (moduleName) {
  39. moduleName += ':';
  40. }
  41. moduleName += file;
  42. return moduleName;
  43. }
  44. return file;
  45. };
  46. }
  47. exports.createGetModuleFromFilename = createGetModuleFromFilename;
  48. //# sourceMappingURL=module.js.map