Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Oct 12, 2022
1 parent 59c0e84 commit ea286f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
34 changes: 15 additions & 19 deletions 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,
Expand All @@ -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];

Expand All @@ -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
Expand All @@ -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,
);
}
17 changes: 17 additions & 0 deletions 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
Expand All @@ -6,6 +7,22 @@ export interface SentryPreprocessorGroup extends PreprocessorGroup {
id?: string;
}

/**
* 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;
};

// Alternatively, we could use a direct from svelte/compiler/preprocess
// TODO: figure out what's better and roll with that

Expand Down

0 comments on commit ea286f6

Please sign in to comment.