Skip to content

Commit cefd523

Browse files
authored
ref(sveltekit): Inject init plugin only if sentry config files exist (#7663)
To make our Sentry SDK setup easier, we'll instruct users to init the SDK in the hooks files rather than in the dedicated sentry.client|server.config.js files (#7661). To support this change, this PR makes the injection of our `injectSentryInitPlugin` into the users' build config depend on the existence of said sentry config files. If they don't exist, there's no need to add the plugin or adjust the server config.
1 parent bb11a2e commit cefd523

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

packages/sveltekit/src/config/vitePlugins.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ function addSentryConfigFileImport(
6060
return { code: ms.toString(), map: ms.generateMap() };
6161
}
6262

63-
function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): string | undefined {
63+
/**
64+
* Looks up the sentry.{@param platform}.config.(ts|js) file
65+
* @returns the file path to the file or undefined if it doesn't exist
66+
*/
67+
export function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): string | undefined {
6468
const possibilities = [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`];
6569

6670
for (const filename of possibilities) {
@@ -69,5 +73,5 @@ function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): s
6973
}
7074
}
7175

72-
throw new Error(`Cannot find '${possibilities[0]}' or '${possibilities[1]}' in '${projectDir}'.`);
76+
return undefined;
7377
}

packages/sveltekit/src/config/withSentryViteConfig.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UserConfig, UserConfigExport } from 'vite';
22

3-
import { injectSentryInitPlugin } from './vitePlugins';
3+
import { getUserConfigFile, injectSentryInitPlugin } from './vitePlugins';
44

55
/**
66
* This function adds Sentry-specific configuration to your Vite config.
@@ -31,17 +31,27 @@ export function withSentryViteConfig(originalConfig: UserConfigExport): UserConf
3131
}
3232

3333
function addSentryConfig(originalConfig: UserConfig): UserConfig {
34+
const sentryPlugins = [];
35+
36+
const shouldAddInjectInitPlugin = hasSentryInitFiles();
37+
38+
if (shouldAddInjectInitPlugin) {
39+
sentryPlugins.push(injectSentryInitPlugin);
40+
}
41+
3442
const config = {
3543
...originalConfig,
36-
plugins: originalConfig.plugins ? [injectSentryInitPlugin, ...originalConfig.plugins] : [injectSentryInitPlugin],
44+
plugins: originalConfig.plugins ? [...sentryPlugins, ...originalConfig.plugins] : [...sentryPlugins],
3745
};
3846

39-
const mergedDevServerFileSystemConfig: UserConfig['server'] = {
40-
fs: {
41-
...(config.server && config.server.fs),
42-
allow: [...((config.server && config.server.fs && config.server.fs.allow) || []), '.'],
43-
},
44-
};
47+
const mergedDevServerFileSystemConfig: UserConfig['server'] = shouldAddInjectInitPlugin
48+
? {
49+
fs: {
50+
...(config.server && config.server.fs),
51+
allow: [...((config.server && config.server.fs && config.server.fs.allow) || []), '.'],
52+
},
53+
}
54+
: {};
4555

4656
config.server = {
4757
...config.server,
@@ -50,3 +60,9 @@ function addSentryConfig(originalConfig: UserConfig): UserConfig {
5060

5161
return config;
5262
}
63+
64+
function hasSentryInitFiles(): boolean {
65+
const hasSentryServerInit = !!getUserConfigFile(process.cwd(), 'server');
66+
const hasSentryClientInit = !!getUserConfigFile(process.cwd(), 'client');
67+
return hasSentryServerInit || hasSentryClientInit;
68+
}

packages/sveltekit/test/config/withSentryViteConfig.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import type fs from 'fs';
12
import type { Plugin, UserConfig } from 'vite';
3+
import { vi } from 'vitest';
24

35
import { withSentryViteConfig } from '../../src/config/withSentryViteConfig';
46

7+
let existsFile = true;
8+
vi.mock('fs', async () => {
9+
const original = await vi.importActual<typeof fs>('fs');
10+
return {
11+
...original,
12+
existsSync: vi.fn().mockImplementation(() => existsFile),
13+
};
14+
});
515
describe('withSentryViteConfig', () => {
616
const originalConfig = {
717
plugins: [{ name: 'foo' }],
@@ -115,4 +125,28 @@ describe('withSentryViteConfig', () => {
115125

116126
expect((sentrifiedConfig as UserConfig).server?.fs?.allow).toStrictEqual(['.']);
117127
});
128+
129+
it("doesn't add the inject init plugin or the server config if sentry config files don't exist", () => {
130+
existsFile = false;
131+
132+
const sentrifiedConfig = withSentryViteConfig({
133+
plugins: [{ name: 'some plugin' }],
134+
test: {
135+
include: ['src/**/*.{test,spec}.{js,ts}'],
136+
},
137+
server: {
138+
fs: {
139+
allow: ['./bar'],
140+
},
141+
},
142+
} as UserConfig);
143+
144+
expect(typeof sentrifiedConfig).toBe('object');
145+
const plugins = (sentrifiedConfig as UserConfig).plugins as Plugin[];
146+
expect(plugins).toHaveLength(1);
147+
expect(plugins[0].name).toBe('some plugin');
148+
expect((sentrifiedConfig as UserConfig).server?.fs?.allow).toStrictEqual(['./bar']);
149+
150+
existsFile = true;
151+
});
118152
});

0 commit comments

Comments
 (0)