|
| 1 | +import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; |
| 2 | + |
| 3 | +import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from './preprocessors'; |
| 4 | +import { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from './types'; |
| 5 | + |
| 6 | +const DEFAULT_SENTRY_OPTIONS: SentrySvelteConfigOptions = { |
| 7 | + componentTracking: defaultComponentTrackingOptions, |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Add Sentry options to the Svelte config to be exported from the user's `svelte.config.js` file. |
| 12 | + * |
| 13 | + * @param originalConfig The existing config to be exported prior to adding Sentry |
| 14 | + * @param sentryOptions The configuration of the Sentry-added options |
| 15 | + * |
| 16 | + * @return The wrapped and modified config to be exported |
| 17 | + */ |
| 18 | +export function withSentryConfig( |
| 19 | + originalConfig: SvelteConfig, |
| 20 | + sentryOptions?: SentrySvelteConfigOptions, |
| 21 | +): SvelteConfig { |
| 22 | + const mergedOptions = { |
| 23 | + ...DEFAULT_SENTRY_OPTIONS, |
| 24 | + ...sentryOptions, |
| 25 | + }; |
| 26 | + |
| 27 | + const originalPreprocessors = getOriginalPreprocessorArray(originalConfig); |
| 28 | + |
| 29 | + // Map is insertion-order-preserving. It's important to add preprocessors |
| 30 | + // to this map in the right order we want to see them being executed. |
| 31 | + // see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map |
| 32 | + const sentryPreprocessors = new Map<string, SentryPreprocessorGroup>(); |
| 33 | + |
| 34 | + const shouldTrackComponents = mergedOptions.componentTracking && mergedOptions.componentTracking.trackComponents; |
| 35 | + if (shouldTrackComponents) { |
| 36 | + // TODO(v8): Remove eslint rule |
| 37 | + // eslint-disable-next-line deprecation/deprecation |
| 38 | + const firstPassPreproc: SentryPreprocessorGroup = componentTrackingPreprocessor(mergedOptions.componentTracking); |
| 39 | + sentryPreprocessors.set(firstPassPreproc.sentryId || '', firstPassPreproc); |
| 40 | + } |
| 41 | + |
| 42 | + // We prioritize user-added preprocessors, so we don't insert sentry processors if they |
| 43 | + // have already been added by users. |
| 44 | + originalPreprocessors.forEach((p: SentryPreprocessorGroup) => { |
| 45 | + if (p.sentryId) { |
| 46 | + sentryPreprocessors.delete(p.sentryId); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + const mergedPreprocessors = [...sentryPreprocessors.values(), ...originalPreprocessors]; |
| 51 | + |
| 52 | + return { |
| 53 | + ...originalConfig, |
| 54 | + preprocess: mergedPreprocessors, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Standardizes the different ways the user-provided preprocessor option can be specified. |
| 60 | + * Users can specify an array of preprocessors, a single one or no preprocessor. |
| 61 | + * |
| 62 | + * @param originalConfig the user-provided svelte config oject |
| 63 | + * @return an array of preprocessors or an empty array if no preprocessors were specified |
| 64 | + */ |
| 65 | +function getOriginalPreprocessorArray(originalConfig: SvelteConfig): PreprocessorGroup[] { |
| 66 | + if (originalConfig.preprocess) { |
| 67 | + if (Array.isArray(originalConfig.preprocess)) { |
| 68 | + return originalConfig.preprocess; |
| 69 | + } |
| 70 | + return [originalConfig.preprocess]; |
| 71 | + } |
| 72 | + return []; |
| 73 | +} |
0 commit comments