1
1
import { getSentryRelease } from '@sentry/node' ;
2
2
import { dropUndefinedKeys , logger } from '@sentry/utils' ;
3
3
import * as SentryWebpackPlugin from '@sentry/webpack-plugin' ;
4
+ import * as fs from 'fs' ;
5
+ import * as path from 'path' ;
4
6
5
7
import {
6
8
BuildContext ,
@@ -19,9 +21,6 @@ export { SentryWebpackPlugin };
19
21
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
20
22
// TODO: drop merged keys from override check? `includeDefaults` option?
21
23
22
- export const CLIENT_SDK_CONFIG_FILE = './sentry.client.config.js' ;
23
- export const SERVER_SDK_CONFIG_FILE = './sentry.server.config.js' ;
24
-
25
24
const defaultSentryWebpackPluginOptions = dropUndefinedKeys ( {
26
25
url : process . env . SENTRY_URL ,
27
26
org : process . env . SENTRY_ORG ,
@@ -132,7 +131,9 @@ async function addSentryToEntryProperty(
132
131
const newEntryProperty =
133
132
typeof currentEntryProperty === 'function' ? await currentEntryProperty ( ) : { ...currentEntryProperty } ;
134
133
135
- const userConfigFile = buildContext . isServer ? SERVER_SDK_CONFIG_FILE : CLIENT_SDK_CONFIG_FILE ;
134
+ const userConfigFile = buildContext . isServer
135
+ ? getUserConfigFile ( buildContext . dir , 'server' )
136
+ : getUserConfigFile ( buildContext . dir , 'client' ) ;
136
137
137
138
for ( const entryPointName in newEntryProperty ) {
138
139
if ( entryPointName === 'pages/_app' || entryPointName . includes ( 'pages/api' ) ) {
@@ -143,6 +144,33 @@ async function addSentryToEntryProperty(
143
144
return newEntryProperty ;
144
145
}
145
146
147
+ /**
148
+ * Search the project directory for a valid user config file for the given platform, allowing for it to be either a
149
+ * TypeScript or JavaScript file.
150
+ *
151
+ * @param projectDir The root directory of the project, where the file should be located
152
+ * @param platform Either "server" or "client", so that we know which file to look for
153
+ * @returns The name of the relevant file. If no file is found, this method throws an error.
154
+ */
155
+ function getUserConfigFile ( projectDir : string , platform : 'server' | 'client' ) : string {
156
+ let configFile ;
157
+
158
+ const possibilities = [ `sentry.${ platform } .config.ts` , `sentry.${ platform } .config.js` ] ;
159
+
160
+ for ( const filename of possibilities ) {
161
+ if ( fs . existsSync ( path . resolve ( projectDir , filename ) ) ) {
162
+ configFile = filename ;
163
+ break ;
164
+ }
165
+ }
166
+
167
+ if ( ! configFile ) {
168
+ throw new Error ( `Cannot find ${ possibilities [ 0 ] } or ${ possibilities [ 1 ] } in ${ projectDir } ` ) ;
169
+ }
170
+
171
+ return configFile ;
172
+ }
173
+
146
174
/**
147
175
* Add a file to a specific element of the given `entry` webpack config property.
148
176
*
0 commit comments