rewriteFramesIntegration.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { _optionalChain } from '@sentry/utils';
  2. import * as path from 'path';
  3. import { defineIntegration } from '@sentry/core';
  4. import { rewriteFramesIntegration as rewriteFramesIntegration$1, RewriteFrames } from '@sentry/integrations';
  5. import { escapeStringForRegex } from '@sentry/utils';
  6. const globalWithInjectedValues = global
  7. ;
  8. const customRewriteFramesIntegration = ((options) => {
  9. // This value is injected at build time, based on the output directory specified in the build config. Though a default
  10. // is set there, we set it here as well, just in case something has gone wrong with the injection.
  11. const distDirName = globalWithInjectedValues.__rewriteFramesDistDir__;
  12. if (distDirName) {
  13. // nextjs always puts the build directory at the project root level, which is also where you run `next start` from, so
  14. // we can read in the project directory from the currently running process
  15. const distDirAbsPath = path.resolve(distDirName).replace(/(\/|\\)$/, ''); // We strip trailing slashes because "app:///_next" also doesn't have one
  16. // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- user input is escaped
  17. const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex(distDirAbsPath));
  18. return rewriteFramesIntegration$1({
  19. iteratee: frame => {
  20. frame.filename = _optionalChain([frame, 'access', _ => _.filename, 'optionalAccess', _2 => _2.replace, 'call', _3 => _3(SOURCEMAP_FILENAME_REGEX, 'app:///_next')]);
  21. return frame;
  22. },
  23. ...options,
  24. });
  25. }
  26. // Do nothing if we can't find a distDirName
  27. return {
  28. // eslint-disable-next-line deprecation/deprecation
  29. name: RewriteFrames.id,
  30. // eslint-disable-next-line @typescript-eslint/no-empty-function
  31. setupOnce: () => {},
  32. processEvent: event => event,
  33. };
  34. }) ;
  35. const rewriteFramesIntegration = defineIntegration(customRewriteFramesIntegration);
  36. export { customRewriteFramesIntegration, rewriteFramesIntegration };
  37. //# sourceMappingURL=rewriteFramesIntegration.js.map