Skip to content

Commit c37804c

Browse files
committed
allow for js or ts user config files
1 parent b89b086 commit c37804c

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 34 additions & 5 deletions
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,17 +131,47 @@ 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')) {
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}`);
140142
}
141143
}
142144

143145
return newEntryProperty;
144146
}
145147

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

0 commit comments

Comments
 (0)