-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhandleOnBuildEnd.ts
More file actions
150 lines (138 loc) · 5.64 KB
/
handleOnBuildEnd.ts
File metadata and controls
150 lines (138 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { rm } from 'node:fs/promises';
import type { Config } from '@react-router/dev/config';
import { SentryCli } from '@sentry/cli';
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
import { glob } from 'glob';
import type { SentryReactRouterBuildOptions } from '../types';
type BuildEndHook = NonNullable<Config['buildEnd']>;
function getSentryConfig(viteConfig: unknown): SentryReactRouterBuildOptions {
if (!viteConfig || typeof viteConfig !== 'object' || !('sentryConfig' in viteConfig)) {
// eslint-disable-next-line no-console
console.error('[Sentry] sentryConfig not found - it needs to be passed to vite.config.ts');
}
return (viteConfig as { sentryConfig: SentryReactRouterBuildOptions }).sentryConfig;
}
/**
* A build end hook that handles Sentry release creation and source map uploads.
* It creates a new Sentry release if configured, uploads source maps to Sentry,
* and optionally deletes the source map files after upload.
*/
export const sentryOnBuildEnd: BuildEndHook = async ({ reactRouterConfig, viteConfig }) => {
const sentryConfig = getSentryConfig(viteConfig);
// todo(v11): Remove deprecated sourceMapsUploadOptions support (no need for spread/pick anymore)
const {
sourceMapsUploadOptions, // extract to exclude from rest config
...sentryConfigWithoutDeprecatedSourceMapOption
} = sentryConfig;
const unstableSentryVitePluginOptions = sentryConfig.unstable_sentryVitePluginOptions;
const {
authToken,
org,
project,
release,
sourcemaps = { disable: false },
debug = false,
}: Omit<SentryReactRouterBuildOptions, 'sourcemaps' | 'sourceMapsUploadOptions'> &
// Pick 'sourcemaps' from Vite plugin options as the types allow more (e.g. Promise values for `deleteFilesAfterUpload`)
Pick<SentryVitePluginOptions, 'sourcemaps'> = {
...unstableSentryVitePluginOptions,
...sentryConfigWithoutDeprecatedSourceMapOption, // spread in the config without the deprecated sourceMapsUploadOptions
sourcemaps: {
...unstableSentryVitePluginOptions?.sourcemaps,
...sentryConfig.sourcemaps,
...sourceMapsUploadOptions,
// eslint-disable-next-line deprecation/deprecation
disable: sourceMapsUploadOptions?.enabled === false ? true : sentryConfig.sourcemaps?.disable,
},
release: {
...unstableSentryVitePluginOptions?.release,
...sentryConfig.release,
},
project: unstableSentryVitePluginOptions?.project
? Array.isArray(unstableSentryVitePluginOptions?.project)
? unstableSentryVitePluginOptions?.project[0]
: unstableSentryVitePluginOptions?.project
: sentryConfigWithoutDeprecatedSourceMapOption.project,
};
const cliInstance = new SentryCli(null, {
authToken,
org,
...sentryConfig.unstable_sentryVitePluginOptions,
// same handling as in bundler plugins: https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/05084f214c763a05137d863ff5a05ef38254f68d/packages/bundler-plugin-core/src/build-plugin-manager.ts#L102-L103
project: Array.isArray(project) ? project[0] : project,
});
// check if release should be created
if (release?.name) {
try {
await cliInstance.releases.new(release.name, {});
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not create release', error);
}
}
if (!sourcemaps?.disable && viteConfig.build.sourcemap !== false) {
// inject debugIds
try {
await cliInstance.execute(
['sourcemaps', 'inject', reactRouterConfig.buildDirectory],
debug,
);
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not inject debug ids', error);
}
// upload sourcemaps
try {
await cliInstance.releases.uploadSourceMaps(release?.name || 'undefined', {
include: [
{
paths: [reactRouterConfig.buildDirectory],
},
],
});
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not upload sourcemaps', error);
}
}
// delete sourcemaps after upload
let updatedFilesToDeleteAfterUpload = await sourcemaps?.filesToDeleteAfterUpload;
// set a default value no option was set
if (typeof updatedFilesToDeleteAfterUpload === 'undefined') {
updatedFilesToDeleteAfterUpload = [`${reactRouterConfig.buildDirectory}/**/*.map`];
debug &&
// eslint-disable-next-line no-console
console.info(
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
updatedFilesToDeleteAfterUpload,
)}\` to delete generated source maps after they were uploaded to Sentry.`,
);
}
if (updatedFilesToDeleteAfterUpload) {
try {
const filePathsToDelete = await glob(updatedFilesToDeleteAfterUpload, {
absolute: true,
nodir: true,
});
if (debug) {
filePathsToDelete.forEach(filePathToDelete => {
// eslint-disable-next-line no-console
console.info(`Deleting asset after upload: ${filePathToDelete}`);
});
}
await Promise.all(
filePathsToDelete.map(filePathToDelete =>
rm(filePathToDelete, { force: true }).catch((e: unknown) => {
// This is allowed to fail - we just don't do anything
debug &&
// eslint-disable-next-line no-console
console.debug(`An error occurred while attempting to delete asset: ${filePathToDelete}`, e);
}),
),
);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error deleting files after sourcemap upload:', error);
}
}
};