-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(svelte): Add withSentryConfig
function to wrap User Svelte Configuration
#5936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8c7c821
add withSentryConfig function
Lms24 59c0e84
add tests
Lms24 be89c72
cleanup
Lms24 f1f3c45
s/id/sentry_id
Lms24 cd4a253
array -> map
Lms24 d9689c3
s/sentry_id/sentryId
Lms24 1a7aa33
make dedupe test more explicit
Lms24 490c413
move eslint disable after test modification
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; | ||
|
||
import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from './preprocessors'; | ||
import { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from './types'; | ||
|
||
const DEFAULT_SENTRY_OPTIONS: SentrySvelteConfigOptions = { | ||
componentTracking: defaultComponentTrackingOptions, | ||
}; | ||
|
||
/** | ||
* Add Sentry options to the Svelte config to be exported from the user's `svelte.config.js` file. | ||
* | ||
* @param originalConfig The existing config to be exported prior to adding Sentry | ||
* @param sentryOptions The configuration of the Sentry-added options | ||
* | ||
* @return The wrapped and modified config to be exported | ||
*/ | ||
export function withSentryConfig( | ||
originalConfig: SvelteConfig, | ||
sentryOptions?: SentrySvelteConfigOptions, | ||
): SvelteConfig { | ||
const mergedOptions = { | ||
...DEFAULT_SENTRY_OPTIONS, | ||
...sentryOptions, | ||
}; | ||
|
||
const originalPreprocessors = getOriginalPreprocessorArray(originalConfig); | ||
|
||
// Map is insertion-order-preserving. It's important to add preprocessors | ||
// to this map in the right order we want to see them being executed. | ||
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map | ||
const sentryPreprocessors = new Map<string, SentryPreprocessorGroup>(); | ||
|
||
const shouldTrackComponents = mergedOptions.componentTracking && mergedOptions.componentTracking.trackComponents; | ||
if (shouldTrackComponents) { | ||
// TODO(v8): Remove eslint rule | ||
// eslint-disable-next-line deprecation/deprecation | ||
const firstPassPreproc: SentryPreprocessorGroup = componentTrackingPreprocessor(mergedOptions.componentTracking); | ||
sentryPreprocessors.set(firstPassPreproc.sentryId || '', firstPassPreproc); | ||
} | ||
|
||
// We prioritize user-added preprocessors, so we don't insert sentry processors if they | ||
// have already been added by users. | ||
originalPreprocessors.forEach((p: SentryPreprocessorGroup) => { | ||
if (p.sentryId) { | ||
sentryPreprocessors.delete(p.sentryId); | ||
} | ||
}); | ||
|
||
const mergedPreprocessors = [...sentryPreprocessors.values(), ...originalPreprocessors]; | ||
|
||
return { | ||
...originalConfig, | ||
preprocess: mergedPreprocessors, | ||
}; | ||
} | ||
|
||
/** | ||
* Standardizes the different ways the user-provided preprocessor option can be specified. | ||
* 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 | ||
*/ | ||
function getOriginalPreprocessorArray(originalConfig: SvelteConfig): PreprocessorGroup[] { | ||
if (originalConfig.preprocess) { | ||
if (Array.isArray(originalConfig.preprocess)) { | ||
return originalConfig.preprocess; | ||
} | ||
return [originalConfig.preprocess]; | ||
} | ||
return []; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { withSentryConfig } from '../src/config'; | ||
import { componentTrackingPreprocessor, FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID } from '../src/preprocessors'; | ||
import { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from '../src/types'; | ||
|
||
describe('withSentryConfig', () => { | ||
it.each([ | ||
[ | ||
'no preprocessors specified', | ||
{ | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
}, | ||
], | ||
[ | ||
'a single preprocessor specified', | ||
{ | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
preprocess: {}, | ||
}, | ||
], | ||
[ | ||
'an array of preprocessors specified', | ||
{ | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
preprocess: [{}, {}, {}], | ||
}, | ||
], | ||
])('adds our preprocessors by default to the provided svelte config with %s', (_, originalConfig: SvelteConfig) => { | ||
const wrappedConfig = withSentryConfig(originalConfig); | ||
const originalPreprocs = originalConfig.preprocess; | ||
const originalNumberOfPreprocs = originalPreprocs | ||
? Array.isArray(originalPreprocs) | ||
? originalPreprocs.length | ||
: 1 | ||
: 0; | ||
|
||
expect(Array.isArray(wrappedConfig.preprocess)).toBe(true); | ||
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: expect.any(Array) }); | ||
expect(wrappedConfig.preprocess).toHaveLength(originalNumberOfPreprocs + 1); | ||
expect((wrappedConfig.preprocess as SentryPreprocessorGroup[])[0].sentryId).toEqual( | ||
FIRST_PASS_COMPONENT_TRACKING_PREPROC_ID, | ||
); | ||
}); | ||
|
||
it("doesn't add Sentry preprocessors that were already added by the users", () => { | ||
// eslint-disable-next-line deprecation/deprecation | ||
const sentryPreproc = componentTrackingPreprocessor(); | ||
const originalConfig = { | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
preprocess: sentryPreproc, | ||
}; | ||
|
||
const wrappedConfig = withSentryConfig(originalConfig); | ||
|
||
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: [sentryPreproc] }); | ||
}); | ||
|
||
it('handles multiple wraps correctly by only adding our preprocessors once', () => { | ||
const originalConfig = { | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
}; | ||
|
||
const wrappedConfig = withSentryConfig(withSentryConfig(withSentryConfig(originalConfig))); | ||
|
||
expect(wrappedConfig).toEqual({ ...originalConfig, preprocess: expect.any(Array) }); | ||
expect(wrappedConfig.preprocess).toHaveLength(1); | ||
}); | ||
|
||
it("doesn't add component tracking preprocessors if the feature is deactivated", () => { | ||
const originalConfig = { | ||
compilerOptions: { | ||
enableSourcemap: true, | ||
}, | ||
preprocess: [{}], | ||
}; | ||
|
||
const sentryOptions: SentrySvelteConfigOptions = { componentTracking: { trackComponents: false } }; | ||
const wrappedConfig = withSentryConfig(originalConfig, sentryOptions); | ||
|
||
expect(wrappedConfig).toEqual(originalConfig); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.