Skip to content

Commit acbe928

Browse files
committed
read options from vite config
1 parent 974f0cb commit acbe928

File tree

8 files changed

+83
-38
lines changed

8 files changed

+83
-38
lines changed

packages/react-router/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"@sentry/core": "9.1.0",
3939
"@sentry/node": "9.1.0",
4040
"@sentry/vite-plugin": "^3.2.0",
41-
"@sentry/cli": "^2.42.1"
41+
"@sentry/cli": "^2.42.1",
42+
"glob": "11.0.1"
4243
},
4344
"devDependencies": {
4445
"@react-router/node": "^7.1.5",

packages/react-router/src/vite/buildEnd/handleOnBuildEnd.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
import type { Config } from '@react-router/dev/dist/config';
22
import SentryCli from '@sentry/cli';
3-
import glob from 'glob';
4-
import * as fs from 'fs';
3+
import { glob } from 'glob';
4+
import { rm } from 'node:fs/promises';
5+
import type { SentryReactRouterBuildOptions } from '../types';
56

6-
type ExtendedBuildEndArgs = Parameters<NonNullable<Config['buildEnd']>>[0] & {
7-
sentryConfig: {
8-
authToken?: string;
9-
org?: string;
10-
project?: string;
11-
sourceMapsUploadOptions?: {
12-
enabled?: boolean;
13-
filesToDeleteAfterUpload?: string | string[] | false;
14-
};
15-
release?: {
16-
name?: string;
17-
};
18-
debug?: boolean;
19-
};
20-
};
7+
type BuildEndHook = NonNullable<Config['buildEnd']>;
8+
9+
function getSentryConfig(viteConfig: unknown): SentryReactRouterBuildOptions {
10+
if (!viteConfig || typeof viteConfig !== 'object' || !('sentryConfig' in viteConfig)) {
11+
// eslint-disable-next-line no-console
12+
console.error('[Sentry] sentryConfig not found - it needs to be passed to vite.config.ts');
13+
}
2114

22-
type ExtendedBuildEndHook = (args: ExtendedBuildEndArgs) => void | Promise<void>;
15+
return (viteConfig as { sentryConfig: SentryReactRouterBuildOptions }).sentryConfig;
16+
}
2317

2418
/**
2519
* A build end hook that handles Sentry release creation and source map uploads.
2620
* It creates a new Sentry release if configured, uploads source maps to Sentry,
2721
* and optionally deletes the source map files after upload.
2822
*/
29-
export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig, viteConfig, sentryConfig }) => {
30-
const { authToken, org, project, release, sourceMapsUploadOptions, debug } = sentryConfig;
23+
export const sentryOnBuildEnd: BuildEndHook = async ({ reactRouterConfig, viteConfig }) => {
24+
const {
25+
authToken = 'test-token',
26+
org = 'test-org',
27+
project = 'test-project',
28+
release = { name: 'test-release' },
29+
sourceMapsUploadOptions = { enabled: true },
30+
debug = false,
31+
} = getSentryConfig(viteConfig);
32+
3133
const cliInstance = new SentryCli(null, {
3234
authToken,
3335
org,
3436
project,
3537
});
36-
3738
// check if release should be created
3839
if (release?.name) {
3940
try {
@@ -43,7 +44,6 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
4344
console.error('[Sentry] Could not create release', error);
4445
}
4546
}
46-
4747
// upload sourcemaps
4848
if (sourceMapsUploadOptions?.enabled ?? (true && viteConfig.build.sourcemap !== false)) {
4949
try {
@@ -59,14 +59,11 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
5959
console.error('[Sentry] Could not upload sourcemaps', error);
6060
}
6161
}
62-
6362
// delete sourcemaps after upload
6463
let updatedFilesToDeleteAfterUpload = sourceMapsUploadOptions?.filesToDeleteAfterUpload;
65-
6664
// set a default value no option was set
6765
if (typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined') {
6866
updatedFilesToDeleteAfterUpload = [`${reactRouterConfig.buildDirectory}/**/*.map`];
69-
7067
if (debug) {
7168
// eslint-disable-next-line no-console
7269
console.info(
@@ -76,24 +73,21 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
7673
);
7774
}
7875
}
79-
8076
if (updatedFilesToDeleteAfterUpload) {
8177
try {
8278
const filePathsToDelete = await glob(updatedFilesToDeleteAfterUpload, {
8379
absolute: true,
8480
nodir: true,
8581
});
86-
8782
if (debug) {
8883
filePathsToDelete.forEach(filePathToDelete => {
8984
// eslint-disable-next-line no-console
9085
console.info(`Deleting asset after upload: ${filePathToDelete}`);
9186
});
9287
}
93-
9488
await Promise.all(
9589
filePathsToDelete.map(filePathToDelete =>
96-
fs.promises.rm(filePathToDelete, { force: true }).catch((e: unknown) => {
90+
rm(filePathToDelete, { force: true }).catch((e: unknown) => {
9791
if (debug) {
9892
// This is allowed to fail - we just don't do anything
9993
// eslint-disable-next-line no-console
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { sentryReactRouter } from './plugin';
22
export { sentryOnBuildEnd } from './buildEnd/handleOnBuildEnd';
3+
export type { SentryReactRouterBuildOptions } from './types';

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

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

55
/**
66
* Create a custom subset of sentry's vite plugins
77
*/
8-
export async function makeCustomSentryVitePlugins(options: SentryReactRouterPluginOptions): Promise<Plugin[]> {
8+
export async function makeCustomSentryVitePlugins(options: SentryReactRouterBuildOptions): Promise<Plugin[]> {
99
const {
1010
debug,
1111
sourceMapsUploadOptions,

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

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

55
/**
66
* A Sentry plugin for React Router to enable "hidden" source maps if they are unset.
77
*/
8-
export function makeEnableSourceMapsPlugin(options: SentryReactRouterPluginOptions): Plugin {
8+
export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOptions): Plugin {
99
return {
1010
name: 'sentry-react-router-update-source-map-setting',
1111
apply: 'build',
@@ -39,7 +39,7 @@ export function makeEnableSourceMapsPlugin(options: SentryReactRouterPluginOptio
3939
*/
4040
export function getUpdatedSourceMapSettings(
4141
viteConfig: UserConfig,
42-
sentryPluginOptions?: SentryReactRouterPluginOptions,
42+
sentryPluginOptions?: SentryReactRouterBuildOptions,
4343
): boolean | 'inline' | 'hidden' {
4444
viteConfig.build = viteConfig.build || {};
4545

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ConfigEnv } from 'vite';
22
import { type Plugin } from 'vite';
33
import { makeCustomSentryVitePlugins } from './makeCustomSentryVitePlugins';
44
import { makeEnableSourceMapsPlugin } from './makeEnableSourceMapsPlugin';
5-
import type { SentryReactRouterPluginOptions } from './types';
5+
import type { SentryReactRouterBuildOptions } from './types';
66

77
/**
88
* A Vite plugin for Sentry that handles source map uploads and bundle size optimizations.
@@ -12,7 +12,7 @@ import type { SentryReactRouterPluginOptions } from './types';
1212
* @returns An array of Vite plugins
1313
*/
1414
export async function sentryReactRouter(
15-
options: SentryReactRouterPluginOptions = {},
15+
options: SentryReactRouterBuildOptions = {},
1616
config: ConfigEnv,
1717
): Promise<Plugin[]> {
1818
const plugins: Plugin[] = [];

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ type BundleSizeOptimizationOptions = {
8989
excludeReplayWorker?: boolean;
9090
};
9191

92-
export type SentryReactRouterPluginOptions = {
92+
export type SentryReactRouterBuildOptions = {
93+
/**
94+
* Options for configuring the Sentry release.
95+
*/
96+
release?: {
97+
/**
98+
* The name of the release to create in Sentry
99+
*/
100+
name?: string;
101+
};
102+
93103
/**
94104
* The auth token to use when uploading source maps to Sentry.
95105
*

yarn.lock

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17404,6 +17404,18 @@ glob-to-regexp@^0.4.1:
1740417404
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
1740517405
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
1740617406

17407+
17408+
version "11.0.1"
17409+
resolved "https://registry.yarnpkg.com/glob/-/glob-11.0.1.tgz#1c3aef9a59d680e611b53dcd24bb8639cef064d9"
17410+
integrity sha512-zrQDm8XPnYEKawJScsnM0QzobJxlT/kHOOlRTio8IH/GrmxRE5fjllkzdaHclIuNjUQTJYH2xHNIGfdpJkDJUw==
17411+
dependencies:
17412+
foreground-child "^3.1.0"
17413+
jackspeak "^4.0.1"
17414+
minimatch "^10.0.0"
17415+
minipass "^7.1.2"
17416+
package-json-from-dist "^1.0.0"
17417+
path-scurry "^2.0.0"
17418+
1740717419
1740817420
version "7.1.4"
1740917421
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255"
@@ -19558,6 +19570,13 @@ jackspeak@^3.1.2:
1955819570
optionalDependencies:
1955919571
"@pkgjs/parseargs" "^0.11.0"
1956019572

19573+
jackspeak@^4.0.1:
19574+
version "4.1.0"
19575+
resolved "https://registry.yarnpkg.com/jackspeak/-/jackspeak-4.1.0.tgz#c489c079f2b636dc4cbe9b0312a13ff1282e561b"
19576+
integrity sha512-9DDdhb5j6cpeitCbvLO7n7J4IxnbM6hoF6O1g4HQ5TfhvvKN8ywDM7668ZhMHRqVmxqhps/F6syWK2KcPxYlkw==
19577+
dependencies:
19578+
"@isaacs/cliui" "^8.0.2"
19579+
1956119580
jake@^10.8.5:
1956219581
version "10.8.5"
1956319582
resolved "https://registry.yarnpkg.com/jake/-/jake-10.8.5.tgz#f2183d2c59382cb274226034543b9c03b8164c46"
@@ -21192,6 +21211,11 @@ lru-cache@^10.2.0, lru-cache@^10.4.3:
2119221211
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.4.3.tgz#410fc8a17b70e598013df257c2446b7f3383f119"
2119321212
integrity sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==
2119421213

21214+
lru-cache@^11.0.0:
21215+
version "11.0.2"
21216+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-11.0.2.tgz#fbd8e7cf8211f5e7e5d91905c415a3f55755ca39"
21217+
integrity sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA==
21218+
2119521219
lru-cache@^5.1.1:
2119621220
version "5.1.1"
2119721221
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
@@ -22198,6 +22222,13 @@ [email protected], minimatch@^5.0.1, minimatch@^5.1.0:
2219822222
dependencies:
2219922223
brace-expansion "^2.0.1"
2220022224

22225+
minimatch@^10.0.0:
22226+
version "10.0.1"
22227+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b"
22228+
integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==
22229+
dependencies:
22230+
brace-expansion "^2.0.1"
22231+
2220122232
minimatch@^7.4.1:
2220222233
version "7.4.6"
2220322234
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-7.4.6.tgz#845d6f254d8f4a5e4fd6baf44d5f10c8448365fb"
@@ -24422,6 +24453,14 @@ path-scurry@^1.11.1, path-scurry@^1.6.1:
2442224453
lru-cache "^10.2.0"
2442324454
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"
2442424455

24456+
path-scurry@^2.0.0:
24457+
version "2.0.0"
24458+
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-2.0.0.tgz#9f052289f23ad8bf9397a2a0425e7b8615c58580"
24459+
integrity sha512-ypGJsmGtdXUOeM5u93TyeIEfEhM6s+ljAhrk5vAvSx8uyY/02OvrZnA0YNGUrPXfpJMgI1ODd3nwz8Npx4O4cg==
24460+
dependencies:
24461+
lru-cache "^11.0.0"
24462+
minipass "^7.1.2"
24463+
2442524464
2442624465
version "0.1.10"
2442724466
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.10.tgz#67e9108c5c0551b9e5326064387de4763c4d5f8b"

0 commit comments

Comments
 (0)