-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconfig.ts
64 lines (56 loc) · 2.18 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
import { componentTrackingPreprocessor, defaultComponentTrackingOptions } from './preprocessors';
import type { SentryPreprocessorGroup, SentrySvelteConfigOptions, SvelteConfig } from './types';
const defaultSentryOptions: 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 = {
...defaultSentryOptions,
...sentryOptions,
componentTracking: {
...defaultSentryOptions.componentTracking,
...sentryOptions?.componentTracking,
},
};
const originalPreprocessors = getOriginalPreprocessorArray(originalConfig);
// Bail if users already added the preprocessor
if (originalPreprocessors.find((p: PreprocessorGroup) => !!(p as SentryPreprocessorGroup).sentryId)) {
return originalConfig;
}
const mergedPreprocessors = [...originalPreprocessors];
if (mergedOptions.componentTracking.trackComponents) {
mergedPreprocessors.unshift(componentTrackingPreprocessor(mergedOptions.componentTracking));
}
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 [];
}