Skip to content

Commit cdcc4fd

Browse files
committed
use definePlugin to fix rewriteFrames base path
1 parent f96d47b commit cdcc4fd

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

packages/nextjs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
},
3636
"peerDependencies": {
3737
"next": "^10.0.8 || ^11.0",
38-
"react": "15.x || 16.x || 17.x"
38+
"react": "15.x || 16.x || 17.x",
39+
"webpack": ">= 4.0.0"
3940
},
4041
"scripts": {
4142
"build": "run-p build:esm build:es5",

packages/nextjs/src/config/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type NextConfigObject = {
1414
webpack?: WebpackConfigFunction;
1515
// whether to build serverless functions for all pages, not just API routes
1616
target?: 'server' | 'experimental-serverless-trace';
17+
// the output directory for the built app (defaults to ".next")
18+
distDir?: string;
1719
sentry?: {
1820
disableServerWebpackPlugin?: boolean;
1921
disableClientWebpackPlugin?: boolean;

packages/nextjs/src/config/webpack.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dropUndefinedKeys, logger } from '@sentry/utils';
33
import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
44
import * as fs from 'fs';
55
import * as path from 'path';
6+
import { DefinePlugin } from 'webpack';
67

78
import {
89
BuildContext,
@@ -78,6 +79,21 @@ export function constructWebpackConfigFunction(
7879
};
7980
}
8081

82+
// Support non-default output directories by making the output path (easy to get here at build-time) available to
83+
// the SDK's default `RewriteFrames` instance (which needs it at runtime).
84+
const distDir = buildContext.config.distDir;
85+
newConfig.plugins = newConfig.plugins || [];
86+
87+
const definePluginInstance = newConfig.plugins.find(
88+
plugin => plugin.constructor.name === 'DefinePlugin',
89+
) as DefinePlugin;
90+
91+
if (definePluginInstance) {
92+
definePluginInstance.definitions['__rewriteFramesDistDir__'] = distDir;
93+
} else {
94+
newConfig.plugins.push(new DefinePlugin({ __rewriteFramesDistDir__: distDir }));
95+
}
96+
8197
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
8298
const enableWebpackPlugin = buildContext.isServer
8399
? !userNextConfig.sentry?.disableServerWebpackPlugin
@@ -94,7 +110,6 @@ export function constructWebpackConfigFunction(
94110
newConfig.devtool = 'source-map';
95111
}
96112

97-
newConfig.plugins = newConfig.plugins || [];
98113
newConfig.plugins.push(
99114
// @ts-ignore Our types for the plugin are messed up somehow - TS wants this to be `SentryWebpackPlugin.default`,
100115
// but that's not actually a thing

packages/nextjs/src/index.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export function init(options: NextjsOptions): void {
2929
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']);
3030
metadataBuilder.addSdkMetadata();
3131
options.environment = options.environment || process.env.NODE_ENV;
32-
// TODO capture project root and store in an env var for RewriteFrames?
3332
addServerIntegrations(options);
3433
// Right now we only capture frontend sessions for Next.js
3534
options.autoSessionTracking = false;
@@ -47,7 +46,8 @@ function sdkAlreadyInitialized(): boolean {
4746
return !!hub.getClient();
4847
}
4948

50-
const SOURCEMAP_FILENAME_REGEX = /^.*\/\.next\//;
49+
// webpack will replace this placeholder at build time
50+
const SOURCEMAP_FILENAME_REGEX = new RegExp('__rewriteFramesDistDir__');
5151

5252
const defaultRewriteFramesIntegration = new RewriteFrames({
5353
iteratee: frame => {

0 commit comments

Comments
 (0)