Skip to content

Commit 3fcc908

Browse files
committed
switch to using loader for setting RewriteFrames global variable
1 parent 4f3386c commit 3fcc908

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

packages/nextjs/src/config/webpack.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { getSentryRelease } from '@sentry/node';
33
import { dropUndefinedKeys, logger } from '@sentry/utils';
44
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
55
import * as fs from 'fs';
6-
import * as os from 'os';
76
import * as path from 'path';
87

98
import {
@@ -51,6 +50,29 @@ export function constructWebpackConfigFunction(
5150
newConfig = userNextConfig.webpack(newConfig, buildContext);
5251
}
5352

53+
if (isServer) {
54+
newConfig.module = {
55+
...newConfig.module,
56+
rules: [
57+
...(newConfig.module?.rules || []),
58+
{
59+
test: /sentry\.server\.config\.(jsx?|tsx?)/,
60+
use: [
61+
{
62+
// Support non-default output directories by making the output path (easy to get here at build-time)
63+
// available to the server SDK's default `RewriteFrames` instance (which needs it at runtime), by
64+
// injecting code to attach it to `global`.
65+
loader: path.resolve(__dirname, 'prefixLoader.js'),
66+
options: {
67+
distDir: userNextConfig.distDir || '.next',
68+
},
69+
},
70+
],
71+
},
72+
],
73+
};
74+
}
75+
5476
// Tell webpack to inject user config files (containing the two `Sentry.init()` calls) into the appropriate output
5577
// bundles. Store a separate reference to the original `entry` value to avoid an infinite loop. (If we don't do
5678
// this, we'll have a statement of the form `x.y = () => f(x.y)`, where one of the things `f` does is call `x.y`.
@@ -121,7 +143,7 @@ async function addSentryToEntryProperty(
121143
// we know is that it won't have gotten *simpler* in form, so we only need to worry about the object and function
122144
// options. See https://webpack.js.org/configuration/entry-context/#entry.
123145

124-
const { isServer, dir: projectDir, dev: isDev, config: userNextConfig } = buildContext;
146+
const { isServer, dir: projectDir } = buildContext;
125147

126148
const newEntryProperty =
127149
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
@@ -132,21 +154,6 @@ async function addSentryToEntryProperty(
132154
// we need to turn the filename into a path so webpack can find it
133155
const filesToInject = [`./${userConfigFile}`];
134156

135-
// Support non-default output directories by making the output path (easy to get here at build-time) available to the
136-
// server SDK's default `RewriteFrames` instance (which needs it at runtime). Doesn't work when using the dev server
137-
// because it somehow tricks the file watcher into thinking that compilation itself is a file change, triggering an
138-
// infinite recompiling loop. (This should be fine because we don't upload sourcemaps in dev in any case.)
139-
if (isServer && !isDev) {
140-
const rewriteFramesHelper = path.resolve(
141-
fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
142-
'rewriteFramesHelper.js',
143-
);
144-
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${userNextConfig.distDir}';\n`);
145-
// stick our helper file ahead of the user's config file so the value is in the global namespace *before*
146-
// `Sentry.init()` is called
147-
filesToInject.unshift(rewriteFramesHelper);
148-
}
149-
150157
// inject into all entry points which might contain user's code
151158
for (const entryPointName in newEntryProperty) {
152159
if (shouldAddSentryToEntryPoint(entryPointName, isServer)) {

packages/nextjs/src/index.server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function sdkAlreadyInitialized(): boolean {
110110

111111
function addServerIntegrations(options: NextjsOptions): void {
112112
// This value is injected at build time, based on the output directory specified in the build config
113-
const distDirName = (global as GlobalWithDistDir).__rewriteFramesDistDir__ || '.next';
113+
const distDirName = (global as GlobalWithDistDir).__rewriteFramesDistDir__;
114114
// nextjs always puts the build directory at the project root level, which is also where you run `next start` from, so
115115
// we can read in the project directory from the currently running process
116116
const distDirAbsPath = path.resolve(process.cwd(), distDirName);

0 commit comments

Comments
 (0)