Skip to content

Commit 48395e8

Browse files
committed
inject distDir into global at build time
1 parent f79028b commit 48395e8

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getSentryRelease } from '@sentry/node';
22
import { dropUndefinedKeys, logger } from '@sentry/utils';
33
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
44
import * as fs from 'fs';
5+
import * as os from 'os';
56
import * as path from 'path';
67

78
import {
@@ -127,14 +128,31 @@ async function addSentryToEntryProperty(
127128
const newEntryProperty =
128129
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
129130

131+
// `sentry.server.config.js` or `sentry.client.config.js` (or their TS equivalents)
130132
const userConfigFile = buildContext.isServer
131133
? getUserConfigFile(buildContext.dir, 'server')
132134
: getUserConfigFile(buildContext.dir, 'client');
133135

136+
// we need to turn the filename into a path so webpack can find it
137+
const filesToInject = [`./${userConfigFile}`];
138+
139+
// Support non-default output directories by making the output path (easy to get here at build-time) available to the
140+
// server SDK's default `RewriteFrames` instance (which needs it at runtime).
141+
if (buildContext.isServer) {
142+
const rewriteFramesHelper = path.resolve(
143+
fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
144+
'rewriteFramesHelper.js',
145+
);
146+
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${buildContext.config.distDir}';\n`);
147+
// stick our helper file ahead of the user's config file so the value is in the global namespace *before*
148+
// `Sentry.init()` is called
149+
filesToInject.unshift(rewriteFramesHelper);
150+
}
151+
152+
// inject into all entry points which might contain user's code
134153
for (const entryPointName in newEntryProperty) {
135154
if (shouldAddSentryToEntryPoint(entryPointName)) {
136-
// we need to turn the filename into a path so webpack can find it
137-
addFileToExistingEntryPoint(newEntryProperty, entryPointName, `./${userConfigFile}`);
155+
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
138156
}
139157
}
140158

0 commit comments

Comments
 (0)