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 be89c72
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 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,
);
}
1 change: 1 addition & 0 deletions packages/svelte/src/index.ts
Expand Up @@ -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';
Expand Down
18 changes: 16 additions & 2 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,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 = {
/**
Expand Down
5 changes: 3 additions & 2 deletions 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([
Expand Down Expand Up @@ -53,6 +53,7 @@ describe('withSentryConfig', () => {
compilerOptions: {
enableSourcemap: true,
},
// eslint-disable-next-line deprecation/deprecation
preprocess: componentTrackingPreprocessor(),
};

Expand Down
1 change: 1 addition & 0 deletions packages/svelte/test/preprocessors.test.ts
@@ -1,3 +1,4 @@
/* eslint-disable deprecation/deprecation */
import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from '../src/preprocessors';

function expectComponentCodeToBeModified(
Expand Down

0 comments on commit be89c72

Please sign in to comment.