Skip to content

Commit b2c314a

Browse files
committed
use helper to add RewriteFrames prefix loader
1 parent edd18f3 commit b2c314a

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,8 @@ export function constructWebpackConfigFunction(
8181
const newConfig = setUpModuleRules(rawNewConfig);
8282

8383
if (isServer) {
84-
newConfig.module.rules.push({
85-
test: /sentry\.server\.config\.(jsx?|tsx?)/,
86-
use: [
87-
{
88-
// Support non-default output directories by making the output path (easy to get here at build-time)
89-
// available to the server SDK's default `RewriteFrames` instance (which needs it at runtime), by
90-
// injecting code to attach it to `global`.
91-
loader: path.resolve(__dirname, 'loaders/prefixLoader.js'),
92-
options: {
93-
distDir: userNextConfig.distDir || '.next',
94-
},
95-
},
96-
],
97-
});
84+
// This loader will inject code setting global values for use by `RewriteFrames`
85+
addRewriteFramesLoader(newConfig, 'server', userNextConfig);
9886

9987
if (userSentryOptions.autoInstrumentServerFunctions !== false) {
10088
const pagesDir = newConfig.resolve?.alias?.['private-next-pages'] as string;
@@ -644,3 +632,42 @@ function setUpModuleRules(newConfig: WebpackConfigObject): WebpackConfigObjectWi
644632
// `newConfig.module.rules` just above, but ¯\_(ツ)_/¯
645633
return newConfig as WebpackConfigObjectWithModuleRules;
646634
}
635+
636+
/**
637+
* Support the `distDir` option by making its value (easy to get here at build-time) available to the server SDK's
638+
* default `RewriteFrames` instance (which needs it at runtime), by injecting code to attach it to `global`.
639+
*
640+
* @param newConfig The webpack config object being constructed
641+
* @param target Either 'server' or 'client'
642+
* @param userNextConfig The user's nextjs config options
643+
*/
644+
function addRewriteFramesLoader(
645+
newConfig: WebpackConfigObjectWithModuleRules,
646+
target: 'server' | 'client',
647+
userNextConfig: NextConfigObject,
648+
): void {
649+
const replacements = {
650+
server: [
651+
[
652+
'__DIST_DIR__',
653+
// Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape
654+
// characters)
655+
userNextConfig.distDir?.replace(/\\/g, '\\\\') || '.next',
656+
],
657+
],
658+
};
659+
660+
newConfig.module.rules.push({
661+
test: new RegExp(`sentry\\.${target}\\.config\\.(jsx?|tsx?)`),
662+
use: [
663+
{
664+
loader: path.resolve(__dirname, 'loaders/prefixLoader.js'),
665+
options: {
666+
templatePrefix: `${target}RewriteFrames`,
667+
// This weird cast will go away as soon as we add the client half of this function in
668+
replacements: replacements[target as 'server'],
669+
},
670+
},
671+
],
672+
});
673+
}

0 commit comments

Comments
 (0)