Skip to content

Commit cef94cb

Browse files
committed
Add sentrySolidStartVite plugin when using withSentry
1 parent a57a198 commit cef94cb

File tree

5 files changed

+122
-13
lines changed

5 files changed

+122
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './withSentry';
2+
export type { Nitro, SentrySolidStartConfigOptions } from './types';

packages/solidstart/src/config/withSentry.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1+
import { addSentryPluginToVite } from '../vite';
2+
import type { SentrySolidStartPluginOptions } from '../vite/types';
13
import {
24
addInstrumentationFileToBuild,
35
experimental_addInstrumentationFileTopLevelImportToServerEntry,
46
} from './addInstrumentation';
5-
import type {
6-
Nitro,
7-
SentrySolidStartConfigOptions,
8-
SolidStartInlineConfig,
9-
SolidStartInlineServerConfig,
10-
} from './types';
7+
import type { Nitro, SolidStartInlineConfig, SolidStartInlineServerConfig } from './types';
118

129
/**
1310
* Modifies the passed in Solid Start configuration with build-time enhancements such as
1411
* building the `instrument.server.ts` file into the appropriate build folder based on
1512
* build preset.
1613
*
1714
* @param solidStartConfig A Solid Start configuration object, as usually passed to `defineConfig` in `app.config.ts|js`
18-
* @param sentrySolidStartConfigOptions Options to configure the plugin
15+
* @param sentrySolidStartPluginOptions Options to configure the plugin
1916
* @returns The modified config to be exported and passed back into `defineConfig`
2017
*/
2118
export const withSentry = (
2219
solidStartConfig: SolidStartInlineConfig = {},
23-
sentrySolidStartConfigOptions: SentrySolidStartConfigOptions = {},
20+
sentrySolidStartPluginOptions: SentrySolidStartPluginOptions = {},
2421
): SolidStartInlineConfig => {
2522
const server = (solidStartConfig.server || {}) as SolidStartInlineServerConfig;
2623
const hooks = server.hooks || {};
24+
const vite =
25+
typeof solidStartConfig.vite === 'function'
26+
? (...args: unknown[]) => addSentryPluginToVite(solidStartConfig.vite(...args), sentrySolidStartPluginOptions)
27+
: addSentryPluginToVite(solidStartConfig.vite, sentrySolidStartPluginOptions);
2728

2829
let serverDir: string;
2930
let buildPreset: string;
3031

3132
return {
3233
...solidStartConfig,
34+
vite,
3335
server: {
3436
...server,
3537
hooks: {
3638
...hooks,
3739
async close() {
38-
if (sentrySolidStartConfigOptions.experimental_basicServerTracing) {
40+
if (sentrySolidStartPluginOptions.experimental_basicServerTracing) {
3941
await experimental_addInstrumentationFileTopLevelImportToServerEntry(serverDir, buildPreset);
4042
}
4143

packages/solidstart/src/vite/sentrySolidStartVite.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Plugin } from 'vite';
1+
import type { Plugin, UserConfig } from 'vite';
22
import { makeBuildInstrumentationFilePlugin } from './buildInstrumentationFile';
33
import { makeSourceMapsVitePlugin } from './sourceMaps';
44
import type { SentrySolidStartPluginOptions } from './types';
@@ -25,3 +25,16 @@ export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}
2525

2626
return sentryPlugins;
2727
};
28+
29+
/**
30+
* Helper to add the Sentry SolidStart vite plugin to a vite config.
31+
*/
32+
export const addSentryPluginToVite = (config: UserConfig = {}, options: SentrySolidStartPluginOptions): UserConfig => {
33+
const plugins = Array.isArray(config.plugins) ? [...config.plugins] : [];
34+
plugins.unshift(sentrySolidStartVite(options));
35+
36+
return {
37+
...config,
38+
plugins,
39+
};
40+
};

packages/solidstart/src/vite/types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type BundleSizeOptimizationOptions = {
8585
};
8686

8787
/**
88-
* Build options for the Sentry module. These options are used during build-time by the Sentry SDK.
88+
* Build options for the Sentry plugin. These options are used during build-time by the Sentry SDK.
8989
*/
9090
export type SentrySolidStartPluginOptions = {
9191
/**
@@ -133,4 +133,16 @@ export type SentrySolidStartPluginOptions = {
133133
* Defaults to: `./src/instrument.server.ts`
134134
*/
135135
instrumentation?: string;
136+
137+
/**
138+
* Enabling basic server tracing can be used for environments where modifying the node option `--import` is not possible.
139+
* However, enabling this option only supports limited tracing instrumentation. Only http traces will be collected (but no database-specific traces etc.).
140+
*
141+
* If this option is `true`, the Sentry SDK will import the instrumentation.server.ts|js file at the top of the server entry file to load the SDK on the server.
142+
*
143+
* **DO NOT** enable this option if you've already added the node option `--import` in your node start script. This would initialize Sentry twice on the server-side and leads to unexpected issues.
144+
*
145+
* @default false
146+
*/
147+
experimental_basicServerTracing?: boolean;
136148
};

packages/solidstart/test/config/withSentry.test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { beforeEach, describe, it, vi } from 'vitest';
2-
import type { Nitro } from '../../build/types/config/types';
1+
import type { Plugin } from 'vite';
2+
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import type { Nitro } from '../../src/config';
34
import { withSentry } from '../../src/config';
45

56
const userDefinedNitroRollupBeforeHookMock = vi.fn();
@@ -62,4 +63,84 @@ describe('withSentry()', () => {
6263
expect(experimental_addInstrumentationFileTopLevelImportToServerEntryMock).not.toHaveBeenCalled();
6364
expect(userDefinedNitroCloseHookMock).toHaveBeenCalled();
6465
});
66+
67+
it('adds the sentry solidstart vite plugin', () => {
68+
const config = withSentry(solidStartConfig, {
69+
project: 'project',
70+
org: 'org',
71+
authToken: 'token',
72+
});
73+
const names = config?.vite.plugins.flat().map((plugin: Plugin) => plugin.name);
74+
expect(names).toEqual([
75+
'sentry-solidstart-source-maps',
76+
'sentry-telemetry-plugin',
77+
'sentry-vite-release-injection-plugin',
78+
'sentry-debug-id-upload-plugin',
79+
'sentry-vite-debug-id-injection-plugin',
80+
'sentry-vite-debug-id-upload-plugin',
81+
'sentry-file-deletion-plugin',
82+
'sentry-solidstart-build-instrumentation-file',
83+
]);
84+
});
85+
86+
it('extends the passed in vite config object', () => {
87+
const config = withSentry(
88+
{
89+
...solidStartConfig,
90+
vite: {
91+
plugins: [{ name: 'my-test-plugin' }],
92+
},
93+
},
94+
{
95+
project: 'project',
96+
org: 'org',
97+
authToken: 'token',
98+
},
99+
);
100+
101+
const names = config?.vite.plugins.flat().map((plugin: Plugin) => plugin.name);
102+
expect(names).toEqual([
103+
'sentry-solidstart-source-maps',
104+
'sentry-telemetry-plugin',
105+
'sentry-vite-release-injection-plugin',
106+
'sentry-debug-id-upload-plugin',
107+
'sentry-vite-debug-id-injection-plugin',
108+
'sentry-vite-debug-id-upload-plugin',
109+
'sentry-file-deletion-plugin',
110+
'sentry-solidstart-build-instrumentation-file',
111+
'my-test-plugin',
112+
]);
113+
});
114+
115+
it('extends the passed in vite function config', () => {
116+
const config = withSentry(
117+
{
118+
...solidStartConfig,
119+
vite() {
120+
return { plugins: [{ name: 'my-test-plugin' }] };
121+
},
122+
},
123+
{
124+
project: 'project',
125+
org: 'org',
126+
authToken: 'token',
127+
},
128+
);
129+
130+
const names = config
131+
?.vite()
132+
.plugins.flat()
133+
.map((plugin: Plugin) => plugin.name);
134+
expect(names).toEqual([
135+
'sentry-solidstart-source-maps',
136+
'sentry-telemetry-plugin',
137+
'sentry-vite-release-injection-plugin',
138+
'sentry-debug-id-upload-plugin',
139+
'sentry-vite-debug-id-injection-plugin',
140+
'sentry-vite-debug-id-upload-plugin',
141+
'sentry-file-deletion-plugin',
142+
'sentry-solidstart-build-instrumentation-file',
143+
'my-test-plugin',
144+
]);
145+
});
65146
});

0 commit comments

Comments
 (0)