|
| 1 | +import MagicString from 'magic-string'; |
| 2 | + |
| 3 | +import { ComponentTrackingInitOptions, PreprocessorGroup, TrackComponentOptions } from './types'; |
| 4 | + |
| 5 | +export const defaultComponentTrackingOptions: Required<ComponentTrackingInitOptions> = { |
| 6 | + trackComponents: true, |
| 7 | + trackInit: true, |
| 8 | + trackUpdates: true, |
| 9 | +}; |
| 10 | + |
| 11 | +/** |
| 12 | + * Svelte Preprocessor to inject Sentry performance monitoring related code |
| 13 | + * into Svelte components. |
| 14 | + */ |
| 15 | +export function componentTrackingPreprocessor(options?: ComponentTrackingInitOptions): PreprocessorGroup { |
| 16 | + const mergedOptions = { ...defaultComponentTrackingOptions, ...options }; |
| 17 | + |
| 18 | + const visitedFiles = new Set<string>(); |
| 19 | + |
| 20 | + return { |
| 21 | + // This script hook is called whenever a Svelte component's <script> |
| 22 | + // content is preprocessed. |
| 23 | + // `content` contains the script code as a string |
| 24 | + script: ({ content, filename, attributes }) => { |
| 25 | + // TODO: Not sure when a filename could be undefined. Using this 'unknown' fallback for the time being |
| 26 | + const finalFilename = filename || 'unknown'; |
| 27 | + |
| 28 | + if (!shouldInjectFunction(mergedOptions.trackComponents, finalFilename, attributes, visitedFiles)) { |
| 29 | + return { code: content }; |
| 30 | + } |
| 31 | + |
| 32 | + const { trackInit, trackUpdates } = mergedOptions; |
| 33 | + const trackComponentOptions: TrackComponentOptions = { |
| 34 | + trackInit, |
| 35 | + trackUpdates, |
| 36 | + componentName: getBaseName(finalFilename), |
| 37 | + }; |
| 38 | + |
| 39 | + const importStmt = 'import { trackComponent } from "@sentry/svelte";\n'; |
| 40 | + const functionCall = `trackComponent(${JSON.stringify(trackComponentOptions)});\n`; |
| 41 | + |
| 42 | + const s = new MagicString(content); |
| 43 | + s.prepend(functionCall).prepend(importStmt); |
| 44 | + |
| 45 | + const updatedCode = s.toString(); |
| 46 | + const updatedSourceMap = s.generateMap().toString(); |
| 47 | + |
| 48 | + return { code: updatedCode, map: updatedSourceMap }; |
| 49 | + }, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function shouldInjectFunction( |
| 54 | + trackComponents: Required<ComponentTrackingInitOptions['trackComponents']>, |
| 55 | + filename: string, |
| 56 | + attributes: Record<string, string | boolean>, |
| 57 | + visitedFiles: Set<string>, |
| 58 | +): boolean { |
| 59 | + // We do cannot inject our function multiple times into the same component |
| 60 | + // This can happen when a component has multiple <script> blocks |
| 61 | + if (visitedFiles.has(filename)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + visitedFiles.add(filename); |
| 65 | + |
| 66 | + // We can't inject our function call into <script context="module"> blocks |
| 67 | + // because the code inside is not executed when the component is instantiated but |
| 68 | + // when the module is first imported. |
| 69 | + // see: https://svelte.dev/docs#component-format-script-context-module |
| 70 | + if (attributes.context === 'module') { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + if (!trackComponents) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + if (Array.isArray(trackComponents)) { |
| 79 | + const componentName = getBaseName(filename); |
| 80 | + return trackComponents.some(allowed => allowed === componentName); |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +function getBaseName(filename: string): string { |
| 87 | + const segments = filename.split('/'); |
| 88 | + return segments[segments.length - 1].replace('.svelte', ''); |
| 89 | +} |
0 commit comments