Skip to content

Commit 4e6c02c

Browse files
authored
feat(sveltekit): Add bundle size optimizations to plugin options (#13318)
Makes it possible to add bundle size optimizations along with source maps options to the SvelteKit plugin options like this: ```js sourceMapsUploadOptions: { authToken: 'token', org: 'org', project: 'project', }, bundleSizeOptimizations: { excludePerformanceMonitoring: true, excludeTracing: true }, ``` A bit of refactoring was done as well in the PR: - exported all types necessary for the plugin from `./types` - create a function `generateVitePluginOptions` which merges all SvelteKit plugin options correctly to create the Vite Plugin options (+ tests for this function) part of #13011
1 parent 345dd74 commit 4e6c02c

File tree

7 files changed

+504
-190
lines changed

7 files changed

+504
-190
lines changed

packages/astro/src/integration/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type BundleSizeOptimizationOptions = {
8787
/**
8888
* If set to true, the plugin will try to tree-shake performance monitoring statements out.
8989
* Note that the success of this depends on tree shaking generally being enabled in your build.
90-
* Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startTransaction()).
90+
* Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startSpan()).
9191
*/
9292
excludeTracing?: boolean;
9393

packages/sveltekit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"@sentry/svelte": "8.25.0",
4747
"@sentry/types": "8.25.0",
4848
"@sentry/utils": "8.25.0",
49-
"@sentry/vite-plugin": "2.20.1",
49+
"@sentry/vite-plugin": "2.22.0",
5050
"magic-string": "0.30.7",
5151
"magicast": "0.2.8",
5252
"sorcery": "0.11.0"
Lines changed: 42 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,10 @@
1+
import { dropUndefinedKeys } from '@sentry/utils';
12
import type { Plugin } from 'vite';
2-
3-
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
43
import type { AutoInstrumentSelection } from './autoInstrument';
54
import { makeAutoInstrumentationPlugin } from './autoInstrument';
6-
import type { SupportedSvelteKitAdapters } from './detectAdapter';
75
import { detectAdapter } from './detectAdapter';
86
import { makeCustomSentryVitePlugins } from './sourceMaps';
9-
10-
/**
11-
* Options related to source maps upload to Sentry
12-
*/
13-
type SourceMapsUploadOptions = {
14-
/**
15-
* If this flag is `true`, the Sentry plugins will automatically upload source maps to Sentry.
16-
* @default true`.
17-
*/
18-
autoUploadSourceMaps?: boolean;
19-
20-
/**
21-
* Options for the Sentry Vite plugin to customize and override the release creation and source maps upload process.
22-
* See [Sentry Vite Plugin Options](https://github.com/getsentry/sentry-javascript-bundler-plugins/tree/main/packages/vite-plugin#configuration) for a detailed description.
23-
*/
24-
sourceMapsUploadOptions?: {
25-
/**
26-
* The auth token to use when uploading source maps to Sentry.
27-
*
28-
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
29-
*
30-
* To create an auth token, follow this guide:
31-
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
32-
*/
33-
authToken?: string;
34-
35-
/**
36-
* The organization slug of your Sentry organization.
37-
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
38-
*/
39-
org?: string;
40-
41-
/**
42-
* The project slug of your Sentry project.
43-
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
44-
*/
45-
project?: string;
46-
47-
/**
48-
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
49-
* It will not collect any sensitive or user-specific data.
50-
*
51-
* @default true
52-
*/
53-
telemetry?: boolean;
54-
55-
/**
56-
* Options related to sourcemaps
57-
*/
58-
sourcemaps?: {
59-
/**
60-
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
61-
*
62-
* If this option is not specified, sensible defaults based on your adapter and svelte.config.js
63-
* setup will be used. Use this option to override these defaults, for instance if you have a
64-
* customized build setup that diverges from SvelteKit's defaults.
65-
*
66-
* The globbing patterns must follow the implementation of the `glob` package.
67-
* @see https://www.npmjs.com/package/glob#glob-primer
68-
*/
69-
assets?: string | Array<string>;
70-
71-
/**
72-
* A glob or an array of globs that specifies which build artifacts should not be uploaded to Sentry.
73-
*
74-
* @default [] - By default no files are ignored. Thus, all files matching the `assets` glob
75-
* or the default value for `assets` are uploaded.
76-
*
77-
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
78-
*/
79-
ignore?: string | Array<string>;
80-
81-
/**
82-
* A glob or an array of globs that specifies the build artifacts that should be deleted after the artifact
83-
* upload to Sentry has been completed.
84-
*
85-
* @default [] - By default no files are deleted.
86-
*
87-
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
88-
*/
89-
filesToDeleteAfterUpload?: string | Array<string>;
90-
};
91-
92-
/**
93-
* Options related to managing the Sentry releases for a build.
94-
*
95-
* Note: Managing releases is optional and not required for uploading source maps.
96-
*/
97-
release?: {
98-
/**
99-
* Unique identifier for the release you want to create.
100-
* This value can also be specified via the SENTRY_RELEASE environment variable.
101-
*
102-
* Defaults to automatically detecting a value for your environment. This includes values for Cordova, Heroku,
103-
* AWS CodeBuild, CircleCI, Xcode, and Gradle, and otherwise uses the git HEAD's commit SHA (the latter requires
104-
* access to git CLI and for the root directory to be a valid repository).
105-
*
106-
* If you didn't provide a value and the plugin can't automatically detect one, no release will be created.
107-
*/
108-
name?: string;
109-
110-
/**
111-
* Whether the plugin should inject release information into the build for the SDK to pick it up when
112-
* sending events.
113-
*
114-
* Defaults to `true`.
115-
*/
116-
inject?: boolean;
117-
};
118-
119-
/**
120-
* Options to further customize the Sentry Vite Plugin (@sentry/vite-plugin) behavior directly.
121-
* Options specified in this object take precedence over the options specified in
122-
* the `sourcemaps` and `release` objects.
123-
*
124-
* @see https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options which lists all available options.
125-
*
126-
* Warning: Options within this object are subject to change at any time.
127-
* We DO NOT guarantee semantic versioning for these options, meaning breaking
128-
* changes can occur at any time within a major SDK version.
129-
*
130-
* Furthermore, some options are untested with SvelteKit specifically. Use with caution.
131-
*/
132-
unstable_sentryVitePluginOptions?: Partial<SentryVitePluginOptions>;
133-
};
134-
};
135-
136-
type AutoInstrumentOptions = {
137-
/**
138-
* The Sentry plugin will automatically instrument certain parts of your SvelteKit application at build time.
139-
* Set this option to `false` to disable this behavior or what is instrumentated by passing an object.
140-
*
141-
* Auto instrumentation includes:
142-
* - Universal `load` functions in `+page.(js|ts)` files
143-
* - Server-only `load` functions in `+page.server.(js|ts)` files
144-
*
145-
* @default true (meaning, the plugin will instrument all of the above)
146-
*/
147-
autoInstrument?: boolean | AutoInstrumentSelection;
148-
};
149-
150-
export type SentrySvelteKitPluginOptions = {
151-
/**
152-
* If this flag is `true`, the Sentry plugins will log some useful debug information.
153-
* @default false.
154-
*/
155-
debug?: boolean;
156-
157-
/**
158-
* Specify which SvelteKit adapter you're using.
159-
* By default, the SDK will attempt auto-detect the used adapter at build time and apply the
160-
* correct config for source maps upload or auto-instrumentation.
161-
*
162-
* Currently, the SDK supports the following adapters:
163-
* - node (@sveltejs/adapter-node)
164-
* - auto (@sveltejs/adapter-auto) only Vercel
165-
* - vercel (@sveltejs/adapter-auto) only Serverless functions, no edge runtime
166-
*
167-
* Set this option, if the SDK detects the wrong adapter or you want to use an adapter
168-
* that is not in this list. If you specify 'other', you'll most likely need to configure
169-
* source maps upload yourself.
170-
*
171-
* @default {} the SDK attempts to auto-detect the used adapter at build time
172-
*/
173-
adapter?: SupportedSvelteKitAdapters;
174-
} & SourceMapsUploadOptions &
175-
AutoInstrumentOptions;
7+
import type { CustomSentryVitePluginOptions, SentrySvelteKitPluginOptions } from './types';
1768

1779
const DEFAULT_PLUGIN_OPTIONS: SentrySvelteKitPluginOptions = {
17810
autoUploadSourceMaps: true,
@@ -211,18 +43,50 @@ export async function sentrySvelteKit(options: SentrySvelteKitPluginOptions = {}
21143
);
21244
}
21345

214-
if (mergedOptions.autoUploadSourceMaps && process.env.NODE_ENV !== 'development') {
46+
const sentryVitePluginsOptions = generateVitePluginOptions(mergedOptions);
47+
48+
if (sentryVitePluginsOptions) {
49+
const sentryVitePlugins = await makeCustomSentryVitePlugins(sentryVitePluginsOptions);
50+
51+
sentryPlugins.push(...sentryVitePlugins);
52+
}
53+
54+
return sentryPlugins;
55+
}
56+
57+
/**
58+
* This function creates the options for the custom Sentry Vite plugin.
59+
* The options are derived from the Sentry SvelteKit plugin options, where the `_unstable` options take precedence.
60+
*
61+
* only exported for testing
62+
*/
63+
export function generateVitePluginOptions(
64+
svelteKitPluginOptions: SentrySvelteKitPluginOptions,
65+
): CustomSentryVitePluginOptions | null {
66+
let sentryVitePluginsOptions: CustomSentryVitePluginOptions | null = null;
67+
68+
// Bundle Size Optimizations
69+
if (svelteKitPluginOptions.bundleSizeOptimizations) {
70+
sentryVitePluginsOptions = {
71+
bundleSizeOptimizations: {
72+
...svelteKitPluginOptions.bundleSizeOptimizations,
73+
},
74+
};
75+
}
76+
77+
// Source Maps
78+
if (svelteKitPluginOptions.autoUploadSourceMaps && process.env.NODE_ENV !== 'development') {
21579
const { unstable_sentryVitePluginOptions, ...sourceMapsUploadOptions } =
216-
mergedOptions.sourceMapsUploadOptions || {};
80+
svelteKitPluginOptions.sourceMapsUploadOptions || {};
21781

218-
const sentryVitePluginsOptions = {
219-
...sourceMapsUploadOptions,
82+
sentryVitePluginsOptions = {
83+
...(sentryVitePluginsOptions ? sentryVitePluginsOptions : {}),
22084

85+
...sourceMapsUploadOptions,
22186
...unstable_sentryVitePluginOptions,
222-
223-
adapter: mergedOptions.adapter,
87+
adapter: svelteKitPluginOptions.adapter,
22488
// override the plugin's debug flag with the one from the top-level options
225-
debug: mergedOptions.debug,
89+
debug: svelteKitPluginOptions.debug,
22690
};
22791

22892
if (sentryVitePluginsOptions.sourcemaps) {
@@ -238,11 +102,7 @@ export async function sentrySvelteKit(options: SentrySvelteKitPluginOptions = {}
238102
...unstable_sentryVitePluginOptions?.release,
239103
};
240104
}
241-
242-
const sentryVitePlugins = await makeCustomSentryVitePlugins(sentryVitePluginsOptions);
243-
244-
sentryPlugins.push(...sentryVitePlugins);
245105
}
246106

247-
return sentryPlugins;
107+
return dropUndefinedKeys(sentryVitePluginsOptions);
248108
}

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import type { Plugin } from 'vite';
1111

1212
import MagicString from 'magic-string';
1313
import { WRAPPED_MODULE_SUFFIX } from './autoInstrument';
14-
import type { SupportedSvelteKitAdapters } from './detectAdapter';
1514
import type { GlobalSentryValues } from './injectGlobalValues';
1615
import { VIRTUAL_GLOBAL_VALUES_FILE, getGlobalValueInjectionCode } from './injectGlobalValues';
1716
import { getAdapterOutputDir, getHooksFileName, loadSvelteConfig } from './svelteConfig';
17+
import type { CustomSentryVitePluginOptions } from './types';
1818

1919
// sorcery has no types, so these are some basic type definitions:
2020
type Chain = {
@@ -25,10 +25,6 @@ type Sorcery = {
2525
load(filepath: string): Promise<Chain>;
2626
};
2727

28-
type CustomSentryVitePluginOptions = SentryVitePluginOptions & {
29-
adapter: SupportedSvelteKitAdapters;
30-
};
31-
3228
// storing this in the module scope because `makeCustomSentryVitePlugin` is called multiple times
3329
// and we only want to generate a uuid once in case we have to fall back to it.
3430
const releaseName = detectSentryRelease();

0 commit comments

Comments
 (0)