| 14 | * Initialize source map support for production builds |
| 15 | */ |
| 16 | export function initializeSourceMaps(): void { |
| 17 | if (process.env.NODE_ENV !== "production") { |
| 18 | // Only needed in production builds |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | console.debug("Initializing CSP-compatible source map support for production build") |
| 23 | |
| 24 | // Set up global error handler |
| 25 | window.addEventListener("error", async (event) => { |
| 26 | if (event.error && event.error instanceof Error) { |
| 27 | try { |
| 28 | // Apply source maps to the error |
| 29 | const enhancedError = await enhanceErrorWithSourceMaps(event.error) |
| 30 | |
| 31 | // Log the enhanced error |
| 32 | console.error("Source mapped error:", enhancedError) |
| 33 | |
| 34 | // Don't prevent default handling - let the ErrorBoundary catch it |
| 35 | } catch (e) { |
| 36 | console.error("Error enhancing error with source maps:", e) |
| 37 | } |
| 38 | } |
| 39 | }) |
| 40 | |
| 41 | // Set up unhandled promise rejection handler |
| 42 | window.addEventListener("unhandledrejection", async (event) => { |
| 43 | if (event.reason && event.reason instanceof Error) { |
| 44 | try { |
| 45 | // Apply source maps to the error |
| 46 | const enhancedError = await enhanceErrorWithSourceMaps(event.reason) |
| 47 | |
| 48 | // Log the enhanced error |
| 49 | console.error("Source mapped rejection:", enhancedError) |
| 50 | } catch (e) { |
| 51 | console.error("Error enhancing rejection with source maps:", e) |
| 52 | } |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | // Preload source maps for all scripts |
| 57 | try { |
| 58 | const scripts = document.getElementsByTagName("script") |
| 59 | for (let i = 0; i < scripts.length; i++) { |
| 60 | const script = scripts[i] |
| 61 | if (script.src) { |
| 62 | // Try multiple source map locations |
| 63 | const possibleMapUrls = [ |
| 64 | `${script.src}.map`, |
| 65 | `${script.src}?source-map=true`, |
| 66 | script.src.replace(/\.js$/, ".js.map"), |
| 67 | script.src.replace(/\.js$/, ".map.json"), |
| 68 | script.src.replace(/\.js$/, ".sourcemap"), |
| 69 | ] |
| 70 | |
| 71 | // Preload all possible source map locations |
| 72 | for (const mapUrl of possibleMapUrls) { |
| 73 | const link = document.createElement("link") |