Skip to content

ref(sveltekit): Inject init plugin only if sentry config files exist #7663

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/sveltekit/src/config/vitePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function addSentryConfigFileImport(
return { code: ms.toString(), map: ms.generateMap() };
}

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

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

throw new Error(`Cannot find '${possibilities[0]}' or '${possibilities[1]}' in '${projectDir}'.`);
return undefined;
}
32 changes: 24 additions & 8 deletions packages/sveltekit/src/config/withSentryViteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserConfig, UserConfigExport } from 'vite';

import { injectSentryInitPlugin } from './vitePlugins';
import { getUserConfigFile, injectSentryInitPlugin } from './vitePlugins';

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

function addSentryConfig(originalConfig: UserConfig): UserConfig {
const sentryPlugins = [];

const shouldAddInjectInitPlugin = hasSentryInitFiles();

if (shouldAddInjectInitPlugin) {
sentryPlugins.push(injectSentryInitPlugin);
}

const config = {
...originalConfig,
plugins: originalConfig.plugins ? [injectSentryInitPlugin, ...originalConfig.plugins] : [injectSentryInitPlugin],
plugins: originalConfig.plugins ? [...sentryPlugins, ...originalConfig.plugins] : [...sentryPlugins],
};

const mergedDevServerFileSystemConfig: UserConfig['server'] = {
fs: {
...(config.server && config.server.fs),
allow: [...((config.server && config.server.fs && config.server.fs.allow) || []), '.'],
},
};
const mergedDevServerFileSystemConfig: UserConfig['server'] = shouldAddInjectInitPlugin
? {
fs: {
...(config.server && config.server.fs),
allow: [...((config.server && config.server.fs && config.server.fs.allow) || []), '.'],
},
}
: {};

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

return config;
}

function hasSentryInitFiles(): boolean {
const hasSentryServerInit = !!getUserConfigFile(process.cwd(), 'server');
const hasSentryClientInit = !!getUserConfigFile(process.cwd(), 'client');
return hasSentryServerInit || hasSentryClientInit;
}
34 changes: 34 additions & 0 deletions packages/sveltekit/test/config/withSentryViteConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import type fs from 'fs';
import type { Plugin, UserConfig } from 'vite';
import { vi } from 'vitest';

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

let existsFile = true;
vi.mock('fs', async () => {
const original = await vi.importActual<typeof fs>('fs');
return {
...original,
existsSync: vi.fn().mockImplementation(() => existsFile),
};
});
describe('withSentryViteConfig', () => {
const originalConfig = {
plugins: [{ name: 'foo' }],
Expand Down Expand Up @@ -115,4 +125,28 @@ describe('withSentryViteConfig', () => {

expect((sentrifiedConfig as UserConfig).server?.fs?.allow).toStrictEqual(['.']);
});

it("doesn't add the inject init plugin or the server config if sentry config files don't exist", () => {
existsFile = false;

const sentrifiedConfig = withSentryViteConfig({
plugins: [{ name: 'some plugin' }],
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
},
server: {
fs: {
allow: ['./bar'],
},
},
} as UserConfig);

expect(typeof sentrifiedConfig).toBe('object');
const plugins = (sentrifiedConfig as UserConfig).plugins as Plugin[];
expect(plugins).toHaveLength(1);
expect(plugins[0].name).toBe('some plugin');
expect((sentrifiedConfig as UserConfig).server?.fs?.allow).toStrictEqual(['./bar']);

existsFile = true;
});
});