|
| 1 | +import { buildOptionsImport, ENV_FALLBACK_OPTIONS_FN, resolveInstrumentFile } from './instrumentFile'; |
| 2 | +import { applyAutoInstrumentTransforms, type ProgramBody } from './transform'; |
| 3 | +import { resolveWranglerConfig, type WranglerConfig } from './wranglerConfig'; |
| 4 | + |
| 5 | +// Vite normalizes module IDs to posix separators even on Windows, while |
| 6 | +// `path.resolve` yields backslashes there — normalize before comparing. |
| 7 | +function normalizePath(path: string): string { |
| 8 | + return path.replace(/\\/g, '/'); |
| 9 | +} |
| 10 | + |
| 11 | +// Extensions the entry-module match may tolerate swapping (e.g. wrangler's |
| 12 | +// `main` says `.ts` but the served module is `.js`). Anything else — `.css`, |
| 13 | +// `.html`, … — sharing the entry's basename must never be treated as the entry. |
| 14 | +const JS_EXTENSION_REGEX = /\.[cm]?[jt]sx?$/; |
| 15 | + |
| 16 | +export function sentryCloudflareAutoInstrumentPlugin() { |
| 17 | + let wranglerConfig: WranglerConfig | undefined; |
| 18 | + let entryFilePath: string | undefined; |
| 19 | + |
| 20 | + let optionsFn = ENV_FALLBACK_OPTIONS_FN; |
| 21 | + let optionsImport: string | undefined; |
| 22 | + |
| 23 | + return { |
| 24 | + name: 'sentry-cloudflare-auto-instrument', |
| 25 | + |
| 26 | + configResolved(config: { root: string; logger?: { warn(msg: string): void } }): void { |
| 27 | + const result = resolveWranglerConfig(config.root); |
| 28 | + if (!result) { |
| 29 | + config.logger?.warn('[sentry] No parseable wrangler config found — auto-instrumentation disabled.'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + wranglerConfig = result.config; |
| 34 | + if (wranglerConfig.main) { |
| 35 | + // `main` is already absolute (wrangler resolves it); just normalize |
| 36 | + // separators so the entry-module comparison holds on Windows. |
| 37 | + entryFilePath = normalizePath(wranglerConfig.main); |
| 38 | + } |
| 39 | + |
| 40 | + if (entryFilePath) { |
| 41 | + const instrumentFilePath = resolveInstrumentFile(entryFilePath); |
| 42 | + if (instrumentFilePath) { |
| 43 | + const built = buildOptionsImport(entryFilePath, instrumentFilePath); |
| 44 | + optionsFn = built.optionsFn; |
| 45 | + optionsImport = built.importStmt; |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + |
| 50 | + transform( |
| 51 | + this: { parse(code: string): ProgramBody; warn?(msg: string): void; environment?: { name?: string } }, |
| 52 | + code: string, |
| 53 | + id: string, |
| 54 | + ): { code: string; map: unknown } | undefined { |
| 55 | + if (!wranglerConfig || !entryFilePath) return undefined; |
| 56 | + |
| 57 | + // The worker entry never belongs to the client (browser) environment. |
| 58 | + // Skipping it keeps a same-basename sibling (e.g. a `src/index.tsx` |
| 59 | + // client entry next to a `src/index.ts` worker) out of the browser bundle. |
| 60 | + if (this.environment?.name === 'client') return undefined; |
| 61 | + |
| 62 | + // Vite may append query/hash params to the module ID. |
| 63 | + const normalizedId = normalizePath(id.replace(/[?#].*$/, '')); |
| 64 | + if (normalizedId !== entryFilePath) { |
| 65 | + // Tolerate a differing JS-flavored extension (e.g. `.js` vs `.ts`). |
| 66 | + if (!JS_EXTENSION_REGEX.test(normalizedId) || !JS_EXTENSION_REGEX.test(entryFilePath)) return undefined; |
| 67 | + if (normalizedId.replace(JS_EXTENSION_REGEX, '') !== entryFilePath.replace(JS_EXTENSION_REGEX, '')) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + let ast: ProgramBody; |
| 73 | + try { |
| 74 | + ast = this.parse(code); |
| 75 | + } catch { |
| 76 | + // Raw TypeScript or syntax error — esbuild hasn't run yet (unlikely) |
| 77 | + // or the file is genuinely broken. Either way, skip silently. |
| 78 | + return undefined; |
| 79 | + } |
| 80 | + |
| 81 | + const result = applyAutoInstrumentTransforms(code, ast, { |
| 82 | + optionsFn, |
| 83 | + optionsImport, |
| 84 | + }); |
| 85 | + |
| 86 | + return result ?? undefined; |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
0 commit comments