@@ -2,6 +2,7 @@ import { getSentryRelease } from '@sentry/node';
22import { dropUndefinedKeys , logger } from '@sentry/utils' ;
33import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin' ;
44import * as fs from 'fs' ;
5+ import * as os from 'os' ;
56import * as path from 'path' ;
67
78import {
@@ -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