Skip to content

Commit a04f3cc

Browse files
authored
Merge pull request #16197 from getsentry/rola/config-injector
feat(react-router): Create a Vite plugin that injects sentryConfig into the global vite config
2 parents 0c983b3 + 5c01344 commit a04f3cc

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { sentryReactRouter } from './plugin';
22
export { sentryOnBuildEnd } from './buildEnd/handleOnBuildEnd';
33
export type { SentryReactRouterBuildOptions } from './types';
4+
export { makeConfigInjectorPlugin } from './makeConfigInjectorPlugin';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { type Plugin } from 'vite';
2+
import type { SentryReactRouterBuildOptions } from './types';
3+
4+
/**
5+
* Creates a Vite plugin that injects the Sentry options into the global Vite config.
6+
* This ensures the sentryConfig is available to other components that need access to it,
7+
* like the buildEnd hook.
8+
*
9+
* @param options - Configuration options for the Sentry Vite plugin
10+
* @returns A Vite plugin that injects sentryConfig into the global config
11+
*/
12+
export function makeConfigInjectorPlugin(options: SentryReactRouterBuildOptions): Plugin {
13+
return {
14+
name: 'sentry-react-router-config-injector',
15+
enforce: 'pre',
16+
config(config) {
17+
return {
18+
...config,
19+
sentryConfig: options,
20+
};
21+
},
22+
};
23+
}

packages/react-router/src/vite/plugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ConfigEnv } from 'vite';
22
import { type Plugin } from 'vite';
3+
import { makeConfigInjectorPlugin } from './makeConfigInjectorPlugin';
34
import { makeCustomSentryVitePlugins } from './makeCustomSentryVitePlugins';
45
import { makeEnableSourceMapsPlugin } from './makeEnableSourceMapsPlugin';
56
import type { SentryReactRouterBuildOptions } from './types';
@@ -17,6 +18,8 @@ export async function sentryReactRouter(
1718
): Promise<Plugin[]> {
1819
const plugins: Plugin[] = [];
1920

21+
plugins.push(makeConfigInjectorPlugin(options));
22+
2023
if (process.env.NODE_ENV !== 'development' && config.command === 'build' && config.mode !== 'development') {
2124
plugins.push(makeEnableSourceMapsPlugin(options));
2225
plugins.push(...(await makeCustomSentryVitePlugins(options)));

packages/react-router/test/vite/plugin.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { makeConfigInjectorPlugin } from '../../src/vite/makeConfigInjectorPlugin';
23
import { makeCustomSentryVitePlugins } from '../../src/vite/makeCustomSentryVitePlugins';
34
import { makeEnableSourceMapsPlugin } from '../../src/vite/makeEnableSourceMapsPlugin';
45
import { sentryReactRouter } from '../../src/vite/plugin';
@@ -12,57 +13,61 @@ vi.spyOn(console, 'warn').mockImplementation(() => {
1213

1314
vi.mock('../../src/vite/makeCustomSentryVitePlugins');
1415
vi.mock('../../src/vite/makeEnableSourceMapsPlugin');
16+
vi.mock('../../src/vite/makeConfigInjectorPlugin');
1517

1618
describe('sentryReactRouter', () => {
1719
const mockPlugins = [{ name: 'test-plugin' }];
1820
const mockSourceMapsPlugin = { name: 'source-maps-plugin' };
21+
const mockConfigInjectorPlugin = { name: 'sentry-config-injector' };
1922

2023
beforeEach(() => {
2124
vi.clearAllMocks();
2225
vi.mocked(makeCustomSentryVitePlugins).mockResolvedValue(mockPlugins);
2326
vi.mocked(makeEnableSourceMapsPlugin).mockReturnValue(mockSourceMapsPlugin);
27+
vi.mocked(makeConfigInjectorPlugin).mockReturnValue(mockConfigInjectorPlugin);
2428
});
2529

2630
afterEach(() => {
2731
vi.resetModules();
2832
});
2933

30-
it('should return an empty array in development mode', async () => {
34+
it('should return sentry config injector plugin in development mode', async () => {
3135
const originalNodeEnv = process.env.NODE_ENV;
3236
process.env.NODE_ENV = 'development';
3337

3438
const result = await sentryReactRouter({}, { command: 'build', mode: 'production' });
3539

36-
expect(result).toEqual([]);
40+
expect(result).toEqual([mockConfigInjectorPlugin]);
3741
expect(makeCustomSentryVitePlugins).not.toHaveBeenCalled();
3842
expect(makeEnableSourceMapsPlugin).not.toHaveBeenCalled();
3943

4044
process.env.NODE_ENV = originalNodeEnv;
4145
});
4246

43-
it('should return an empty array when not in build mode', async () => {
47+
it('should return config injector plugin when not in build mode', async () => {
4448
const result = await sentryReactRouter({}, { command: 'serve', mode: 'production' });
4549

46-
expect(result).toEqual([]);
50+
expect(result).toEqual([mockConfigInjectorPlugin]);
4751
expect(makeCustomSentryVitePlugins).not.toHaveBeenCalled();
4852
expect(makeEnableSourceMapsPlugin).not.toHaveBeenCalled();
4953
});
5054

51-
it('should return an empty array when in development mode', async () => {
55+
it('should return config injector plugin in development build mode', async () => {
5256
const result = await sentryReactRouter({}, { command: 'build', mode: 'development' });
5357

54-
expect(result).toEqual([]);
58+
expect(result).toEqual([mockConfigInjectorPlugin]);
5559
expect(makeCustomSentryVitePlugins).not.toHaveBeenCalled();
5660
expect(makeEnableSourceMapsPlugin).not.toHaveBeenCalled();
5761
});
5862

59-
it('should return plugins in production build mode', async () => {
63+
it('should return all plugins in production build mode', async () => {
6064
const originalNodeEnv = process.env.NODE_ENV;
6165
process.env.NODE_ENV = 'production';
6266

6367
const result = await sentryReactRouter({}, { command: 'build', mode: 'production' });
6468

65-
expect(result).toEqual([mockSourceMapsPlugin, ...mockPlugins]);
69+
expect(result).toEqual([mockConfigInjectorPlugin, mockSourceMapsPlugin, ...mockPlugins]);
70+
expect(makeConfigInjectorPlugin).toHaveBeenCalledWith({});
6671
expect(makeCustomSentryVitePlugins).toHaveBeenCalledWith({});
6772
expect(makeEnableSourceMapsPlugin).toHaveBeenCalledWith({});
6873

@@ -81,6 +86,7 @@ describe('sentryReactRouter', () => {
8186

8287
await sentryReactRouter(options, { command: 'build', mode: 'production' });
8388

89+
expect(makeConfigInjectorPlugin).toHaveBeenCalledWith(options);
8490
expect(makeCustomSentryVitePlugins).toHaveBeenCalledWith(options);
8591
expect(makeEnableSourceMapsPlugin).toHaveBeenCalledWith(options);
8692

0 commit comments

Comments
 (0)