Skip to content

Commit fc5bfe7

Browse files
committed
wip: vite config
1 parent dca9d06 commit fc5bfe7

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

packages/react-router/rollup.npm.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default [
55
makeBaseNPMConfig({
66
entrypoints: ['src/index.server.ts', 'src/index.client.ts'],
77
packageSpecificConfig: {
8-
external: ['react-router', 'react-router-dom', 'react', 'react/jsx-runtime'],
8+
external: ['react-router', 'react-router-dom', 'react', 'react/jsx-runtime', 'vite'],
99
output: {
1010
// make it so Rollup calms down about the fact that we're combining default and named exports
1111
exports: 'named',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './server';
2+
export * from './vite';

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { consoleSandbox } from '@sentry/core';
22
import { sentryVitePlugin } from '@sentry/vite-plugin';
3-
import type { Plugin, UserConfig } from 'vite';
3+
import { loadConfigFromFile, type Plugin, type UserConfig } from 'vite';
44
import type { SentryReactRouterPluginOptions } from './types';
55

66
/**
77
* Creates sentry's vite plugins
88
*/
9-
export function makeSentryVitePlugins(options: SentryReactRouterPluginOptions, viteConfig: UserConfig): Plugin[] {
9+
export async function makeSentryVitePlugins(options: SentryReactRouterPluginOptions): Promise<Plugin[]> {
1010
const {
1111
debug,
1212
sourceMapsUploadOptions,
@@ -19,11 +19,24 @@ export function makeSentryVitePlugins(options: SentryReactRouterPluginOptions, v
1919

2020
let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined;
2121

22+
let loadedConfig: { path: string; config: UserConfig; dependencies: string[] } | null = null;
23+
24+
try {
25+
loadedConfig = await loadConfigFromFile({ mode: 'production', command: 'build' });
26+
} catch (error) {
27+
if (debug) {
28+
consoleSandbox(() => {
29+
// eslint-disable-next-line no-console
30+
console.log('[Sentry] could not load Vite config during build');
31+
});
32+
}
33+
}
34+
2235
if (
2336
typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined' &&
2437
typeof unstable_sentryVitePluginOptions?.sourcemaps?.filesToDeleteAfterUpload === 'undefined' &&
2538
// Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
26-
typeof viteConfig.build?.sourcemap === 'undefined'
39+
typeof loadedConfig?.config.build?.sourcemap === 'undefined'
2740
) {
2841
// For .output, .vercel, .netlify etc.
2942
updatedFilesToDeleteAfterUpload = ['.*/**/*.map'];
@@ -58,6 +71,7 @@ export function makeSentryVitePlugins(options: SentryReactRouterPluginOptions, v
5871
metaFramework: 'react-router',
5972
},
6073
},
74+
6175
...unstable_sentryVitePluginOptions,
6276
}),
6377
];

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Plugin, UserConfig } from 'vite';
1+
import type { ConfigEnv } from 'vite';
2+
import { type Plugin } from 'vite';
23
import { makeSentryVitePlugins } from './makeSentryVitePlugin';
34
import { makeEnableSourceMapsVitePlugins } from './sourceMaps';
45
import type { SentryReactRouterPluginOptions } from './types';
@@ -10,13 +11,18 @@ import type { SentryReactRouterPluginOptions } from './types';
1011
* @param viteConfig - The Vite user config object
1112
* @returns An array of Vite plugins
1213
*/
13-
export function sentryReactRouter(options: SentryReactRouterPluginOptions = {}, viteConfig: UserConfig): Plugin[] {
14+
export async function sentryReactRouter(
15+
options: SentryReactRouterPluginOptions = {},
16+
config: ConfigEnv,
17+
): Promise<Plugin[]> {
1418
const plugins: Plugin[] = [];
1519

1620
if (process.env.NODE_ENV !== 'development') {
1721
if (options.sourceMapsUploadOptions?.enabled ?? true) {
18-
plugins.push(...makeSentryVitePlugins(options, viteConfig));
19-
plugins.push(...makeEnableSourceMapsVitePlugins(options));
22+
if (config.command === 'build' && config.isSsrBuild && config.mode === 'production') {
23+
plugins.push(...makeEnableSourceMapsVitePlugins(options));
24+
plugins.push(...(await makeSentryVitePlugins(options)));
25+
}
2026
}
2127
}
2228

0 commit comments

Comments
 (0)