Skip to content

Commit f185d8b

Browse files
committed
allow for js or ts user config files
1 parent 74a0829 commit f185d8b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/nextjs/src/config/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type WebpackConfigObject = {
4343
};
4444

4545
// Information about the current build environment
46-
export type BuildContext = { dev: boolean; isServer: boolean; buildId: string };
46+
export type BuildContext = { dev: boolean; isServer: boolean; buildId: string; dir: string };
4747

4848
/**
4949
* Webpack `entry` config

packages/nextjs/src/config/webpack.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getSentryRelease } from '@sentry/node';
22
import { dropUndefinedKeys, logger } from '@sentry/utils';
33
import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
4+
import * as fs from 'fs';
5+
import * as path from 'path';
46

57
import {
68
BuildContext,
@@ -19,9 +21,6 @@ export { SentryWebpackPlugin };
1921
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
2022
// TODO: drop merged keys from override check? `includeDefaults` option?
2123

22-
export const CLIENT_SDK_CONFIG_FILE = './sentry.client.config.js';
23-
export const SERVER_SDK_CONFIG_FILE = './sentry.server.config.js';
24-
2524
const defaultSentryWebpackPluginOptions = dropUndefinedKeys({
2625
url: process.env.SENTRY_URL,
2726
org: process.env.SENTRY_ORG,
@@ -132,7 +131,9 @@ async function addSentryToEntryProperty(
132131
const newEntryProperty =
133132
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
134133

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');
136137

137138
for (const entryPointName in newEntryProperty) {
138139
if (entryPointName === 'pages/_app' || entryPointName.includes('pages/api')) {
@@ -143,6 +144,33 @@ async function addSentryToEntryProperty(
143144
return newEntryProperty;
144145
}
145146

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+
146174
/**
147175
* Add a file to a specific element of the given `entry` webpack config property.
148176
*

0 commit comments

Comments
 (0)