From be89c725ac984ea1ba0b96917848909f6a7da11a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 12 Oct 2022 14:46:15 +0200 Subject: [PATCH] cleanup --- packages/svelte/src/config.ts | 34 ++++++++++------------ packages/svelte/src/index.ts | 1 + packages/svelte/src/types.ts | 18 ++++++++++-- packages/svelte/test/config.test.ts | 5 ++-- packages/svelte/test/preprocessors.test.ts | 1 + 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/svelte/src/config.ts b/packages/svelte/src/config.ts index 50634cc7d30a..f6244db8c5a7 100644 --- a/packages/svelte/src/config.ts +++ b/packages/svelte/src/config.ts @@ -1,18 +1,7 @@ -import { CompileOptions } from 'svelte/types/compiler'; import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from './preprocessors'; -import { ComponentTrackingInitOptions, SentryPreprocessorGroup } from './types'; - -export type SvelteConfig = { - [key: string]: unknown; - preprocess?: PreprocessorGroup[] | PreprocessorGroup; - compilerOptions?: CompileOptions; -}; - -export type SentrySvelteConfigOptions = { - componentTracking?: ComponentTrackingInitOptions; -}; +import { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from './types'; const DEFAULT_SENTRY_OPTIONS: SentrySvelteConfigOptions = { componentTracking: defaultComponentTrackingOptions, @@ -36,19 +25,16 @@ export function withSentryConfig( }; const originalPreprocessors = getOriginalPreprocessorArray(originalConfig); - const sentryPreprocessors: SentryPreprocessorGroup[] = []; + const allSentryPreprocessors: SentryPreprocessorGroup[] = []; const shouldTrackComponents = mergedOptions.componentTracking && mergedOptions.componentTracking.trackComponents; if (shouldTrackComponents) { // TODO(v8): Remove eslint rule // eslint-disable-next-line deprecation/deprecation - sentryPreprocessors.push(componentTrackingPreprocessor(mergedOptions.componentTracking)); + allSentryPreprocessors.push(componentTrackingPreprocessor(mergedOptions.componentTracking)); } - const dedupedSentryPreprocessors = sentryPreprocessors.filter( - sentryPreproc => - originalPreprocessors.find(p => (p as SentryPreprocessorGroup).id === sentryPreproc.id) === undefined, - ); + const dedupedSentryPreprocessors = dedupePreprocessors(allSentryPreprocessors, originalPreprocessors); const mergedPreprocessors = [...dedupedSentryPreprocessors, ...originalPreprocessors]; @@ -60,7 +46,7 @@ export function withSentryConfig( /** * Standardizes the different ways the user-provided preprocessor option can be specified. - * Users can specify an array of preprocessors, one single one or nothing at all. + * Users can specify an array of preprocessors, a single one or no preprocessor. * * @param originalConfig the user-provided svelte config oject * @return an array of preprocessors or an empty array if no preprocessors were specified @@ -74,3 +60,13 @@ function getOriginalPreprocessorArray(originalConfig: SvelteConfig): Preprocesso } return []; } + +function dedupePreprocessors( + sentryPreprocessors: SentryPreprocessorGroup[], + originalPreprocessors: PreprocessorGroup[], +): PreprocessorGroup[] { + return sentryPreprocessors.filter( + sentryPreproc => + originalPreprocessors.find(p => (p as SentryPreprocessorGroup).id === sentryPreproc.id) === undefined, + ); +} diff --git a/packages/svelte/src/index.ts b/packages/svelte/src/index.ts index bc82551784d6..ac5aa49a5298 100644 --- a/packages/svelte/src/index.ts +++ b/packages/svelte/src/index.ts @@ -8,6 +8,7 @@ export * from '@sentry/browser'; export { init } from './sdk'; // TODO(v8): Remove this export +// eslint-disable-next-line deprecation/deprecation export { componentTrackingPreprocessor } from './preprocessors'; export { trackComponent } from './performance'; diff --git a/packages/svelte/src/types.ts b/packages/svelte/src/types.ts index 73dceaa4b043..40628d6b974b 100644 --- a/packages/svelte/src/types.ts +++ b/packages/svelte/src/types.ts @@ -1,3 +1,4 @@ +import { CompileOptions } from 'svelte/types/compiler'; import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; // Adds an id property to the preprocessor object we can use to check for duplication @@ -6,8 +7,21 @@ export interface SentryPreprocessorGroup extends PreprocessorGroup { id?: string; } -// Alternatively, we could use a direct from svelte/compiler/preprocess -// TODO: figure out what's better and roll with that +/** + * The object exported from `svelte.config.js` + */ +export type SvelteConfig = { + [key: string]: unknown; + preprocess?: PreprocessorGroup[] | PreprocessorGroup; + compilerOptions?: CompileOptions; +}; + +/** + * Options users can provide to `withSentryConfig` to customize what Sentry adds too the Svelte config + */ +export type SentrySvelteConfigOptions = { + componentTracking?: ComponentTrackingInitOptions; +}; export type SpanOptions = { /** diff --git a/packages/svelte/test/config.test.ts b/packages/svelte/test/config.test.ts index 89e5712110a7..b37e7883f4a0 100644 --- a/packages/svelte/test/config.test.ts +++ b/packages/svelte/test/config.test.ts @@ -1,6 +1,6 @@ -import { withSentryConfig, SvelteConfig, SentrySvelteConfigOptions } from '../src/config'; +import { withSentryConfig } from '../src/config'; import { componentTrackingPreprocessor, FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID } from '../src/preprocessors'; -import { SentryPreprocessorGroup } from '../src/types'; +import { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from '../src/types'; describe('withSentryConfig', () => { it.each([ @@ -53,6 +53,7 @@ describe('withSentryConfig', () => { compilerOptions: { enableSourcemap: true, }, + // eslint-disable-next-line deprecation/deprecation preprocess: componentTrackingPreprocessor(), }; diff --git a/packages/svelte/test/preprocessors.test.ts b/packages/svelte/test/preprocessors.test.ts index afa100c63cac..79a1f1c9e416 100644 --- a/packages/svelte/test/preprocessors.test.ts +++ b/packages/svelte/test/preprocessors.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable deprecation/deprecation */ import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from '../src/preprocessors'; function expectComponentCodeToBeModified(