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,17 +131,47 @@ 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' ) ) {
139
- addFileToExistingEntryPoint ( newEntryProperty , entryPointName , userConfigFile ) ;
140
+ // we need to turn the filename into a path so webpack can find it
141
+ addFileToExistingEntryPoint ( newEntryProperty , entryPointName , `./${ userConfigFile } ` ) ;
140
142
}
141
143
}
142
144
143
145
return newEntryProperty ;
144
146
}
145
147
148
+ /**
149
+ * Search the project directory for a valid user config file for the given platform, allowing for it to be either a
150
+ * TypeScript or JavaScript file.
151
+ *
152
+ * @param projectDir The root directory of the project, where the file should be located
153
+ * @param platform Either "server" or "client", so that we know which file to look for
154
+ * @returns The name of the relevant file. If no file is found, this method throws an error.
155
+ */
156
+ function getUserConfigFile ( projectDir : string , platform : 'server' | 'client' ) : string {
157
+ let configFile ;
158
+
159
+ const possibilities = [ `sentry.${ platform } .config.ts` , `sentry.${ platform } .config.js` ] ;
160
+
161
+ for ( const filename of possibilities ) {
162
+ if ( fs . existsSync ( path . resolve ( projectDir , filename ) ) ) {
163
+ configFile = filename ;
164
+ break ;
165
+ }
166
+ }
167
+
168
+ if ( ! configFile ) {
169
+ throw new Error ( `Cannot find '${ possibilities [ 0 ] } ' or '${ possibilities [ 1 ] } ' in '${ projectDir } '.` ) ;
170
+ }
171
+
172
+ return configFile ;
173
+ }
174
+
146
175
/**
147
176
* Add a file to a specific element of the given `entry` webpack config property.
148
177
*
0 commit comments