import MagicString from 'magic-string'; import type { ComponentTrackingInitOptions, PreprocessorGroup, SentryPreprocessorGroup, TrackComponentOptions, } from './types'; import { stringifyForCodeInjection } from '@sentry/core'; export const defaultComponentTrackingOptions: Required = { trackComponents: true, trackInit: true, trackUpdates: false, }; export const FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID = 'FIRST_PASS_COMPONENT_TRACKING_PREPROCESSOR'; /** * Svelte Preprocessor to inject Sentry performance monitoring related code * into Svelte components. */ export function componentTrackingPreprocessor(options?: ComponentTrackingInitOptions): PreprocessorGroup { const mergedOptions = { ...defaultComponentTrackingOptions, ...options }; const visitedFiles = new Set(); const visitedFilesMarkup = new Set(); const preprocessor: PreprocessorGroup = { // This markup hook is called once per .svelte component file, before the `script` hook is called // We use it to check if the passed component has a is important! Without any content, // the `script` hook wouldn't be executed for the added script tag. const s = new MagicString(content); s.prepend('\n'); return { code: s.toString(), map: s.generateMap().toString() }; } return { code: content }; }, // This script hook is called whenever a Svelte component's