formatFilename.js 725 B

1234567891011121314151617181920212223
  1. /**
  2. * Prettify a filename from error stacks into the desired format.
  3. * @param {string} filename The filename to be formatted.
  4. * @returns {string} The formatted filename.
  5. */
  6. function formatFilename(filename) {
  7. // Strip away protocol and domain for compiled files
  8. const htmlMatch = /^https?:\/\/(.*)\/(.*)/.exec(filename);
  9. if (htmlMatch && htmlMatch[1] && htmlMatch[2]) {
  10. return htmlMatch[2];
  11. }
  12. // Strip everything before the first directory for source files
  13. const sourceMatch = /\/.*?([^./]+[/|\\].*)$/.exec(filename);
  14. if (sourceMatch && sourceMatch[1]) {
  15. return sourceMatch[1].replace(/\?$/, '');
  16. }
  17. // Unknown filename type, use it as is
  18. return filename;
  19. }
  20. module.exports = formatFilename;