@@ -87,26 +87,8 @@ export function constructWebpackConfigFunction(
8787 // `newConfig.module.rules` is required, so we don't have to keep asserting its existence
8888 const newConfig = setUpModuleRules ( rawNewConfig ) ;
8989
90- // Add a loader which will inject code that sets global values for use by `RewriteFrames`
91- addRewriteFramesLoader ( newConfig , isServer ? 'server' : 'client' , userNextConfig ) ;
92-
93- newConfig . module . rules . push ( {
94- test : / s e n t r y \. ( s e r v e r | c l i e n t ) \. c o n f i g \. ( j s x ? | t s x ? ) / ,
95- use : [
96- {
97- // Inject the release value the same way the webpack plugin does.
98- loader : path . resolve ( __dirname , 'loaders/prefixLoader.js' ) ,
99- options : {
100- templatePrefix : 'release' ,
101- replacements : [
102- [ '__RELEASE__' , webpackPluginOptions . release || process . env . SENTRY_RELEASE ] ,
103- [ '__ORG__' , webpackPluginOptions . org || process . env . SENTRY_ORG ] ,
104- [ '__PROJECT__' , webpackPluginOptions . project || process . env . SENTRY_PROJECT || '' ] ,
105- ] ,
106- } ,
107- } ,
108- ] ,
109- } ) ;
90+ // Add a loader which will inject code that sets global values
91+ addValueInjectionLoader ( newConfig , userNextConfig , webpackPluginOptions ) ;
11092
11193 if ( isServer ) {
11294 if ( userSentryOptions . autoInstrumentServerFunctions !== false ) {
@@ -667,49 +649,76 @@ function setUpModuleRules(newConfig: WebpackConfigObject): WebpackConfigObjectWi
667649}
668650
669651/**
670- * Support the `distDir` and `assetPrefix` options by making their values (easy to get here at build-time) available at
671- * runtime (for use by `RewriteFrames`), by injecting code to attach their values to `global` or `window`.
672- *
673- * @param newConfig The webpack config object being constructed
674- * @param target Either 'server' or 'client'
675- * @param userNextConfig The user's nextjs config options
652+ * Adds loaders to inject values on the global object based on user configuration.
676653 */
677- function addRewriteFramesLoader (
654+ function addValueInjectionLoader (
678655 newConfig : WebpackConfigObjectWithModuleRules ,
679- target : 'server' | 'client' ,
680656 userNextConfig : NextConfigObject ,
657+ webpackPluginOptions : SentryWebpackPlugin . SentryCliPluginOptions ,
681658) : void {
682- // Nextjs will use `basePath` in place of `assetPrefix` if it's defined but `assetPrefix` is not
683659 const assetPrefix = userNextConfig . assetPrefix || userNextConfig . basePath || '' ;
684- const replacements = {
685- server : [
686- [
687- '__DIST_DIR__' ,
688- // Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape
689- // characters)
690- userNextConfig . distDir ?. replace ( / \\ / g, '\\\\' ) || '.next' ,
691- ] ,
692- ] ,
693- client : [
694- [
695- '__ASSET_PREFIX_PATH__' ,
696- // Get the path part of `assetPrefix`, minus any trailing slash. (We use a placeholder for the origin if
697- // `assetPreix` doesn't include one. Since we only care about the path, it doesn't matter what it is.)
698- assetPrefix ? new URL ( assetPrefix , 'http://dogs.are.great' ) . pathname . replace ( / \/ $ / , '' ) : '' ,
699- ] ,
700- ] ,
660+ const releaseValue = webpackPluginOptions . release || process . env . SENTRY_RELEASE ;
661+ const orgValue = webpackPluginOptions . org || process . env . SENTRY_ORG ;
662+ const projectValue = webpackPluginOptions . project || process . env . SENTRY_PROJECT ;
663+
664+ const isomorphicValues = {
665+ // Inject release into SDK
666+ ...( releaseValue
667+ ? {
668+ SENTRY_RELEASE : {
669+ id : releaseValue ,
670+ } ,
671+ }
672+ : undefined ) ,
673+
674+ // Enable module federation support (see https://github.com/getsentry/sentry-webpack-plugin/pull/307)
675+ ...( projectValue && releaseValue
676+ ? {
677+ SENTRY_RELEASES : {
678+ [ orgValue ? `${ projectValue } @${ orgValue } ` : projectValue ] : { id : releaseValue } ,
679+ } ,
680+ }
681+ : undefined ) ,
701682 } ;
702683
703- newConfig . module . rules . push ( {
704- test : new RegExp ( `sentry\\.${ target } \\.config\\.(jsx?|tsx?)` ) ,
705- use : [
706- {
707- loader : path . resolve ( __dirname , 'loaders/prefixLoader.js' ) ,
708- options : {
709- templatePrefix : `${ target } RewriteFrames` ,
710- replacements : replacements [ target ] ,
684+ const serverValues = {
685+ ...isomorphicValues ,
686+ // Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape
687+ // characters)
688+ __rewriteFramesDistDir__ : userNextConfig . distDir ?. replace ( / \\ / g, '\\\\' ) || '.next' ,
689+ } ;
690+
691+ const clientValues = {
692+ ...isomorphicValues ,
693+ // Get the path part of `assetPrefix`, minus any trailing slash. (We use a placeholder for the origin if
694+ // `assetPreix` doesn't include one. Since we only care about the path, it doesn't matter what it is.)
695+ __rewriteFramesAssetPrefixPath__ : assetPrefix
696+ ? new URL ( assetPrefix , 'http://dogs.are.great' ) . pathname . replace ( / \/ $ / , '' )
697+ : '' ,
698+ } ;
699+
700+ newConfig . module . rules . push (
701+ {
702+ test : / s e n t r y \. s e r v e r \. c o n f i g \. ( j s x ? | t s x ? ) / ,
703+ use : [
704+ {
705+ loader : path . resolve ( __dirname , 'loaders/valueInjectionLoader.js' ) ,
706+ options : {
707+ values : serverValues ,
708+ } ,
711709 } ,
712- } ,
713- ] ,
714- } ) ;
710+ ] ,
711+ } ,
712+ {
713+ test : / s e n t r y \. c l i e n t \. c o n f i g \. ( j s x ? | t s x ? ) / ,
714+ use : [
715+ {
716+ loader : path . resolve ( __dirname , 'loaders/valueInjectionLoader.js' ) ,
717+ options : {
718+ values : clientValues ,
719+ } ,
720+ } ,
721+ ] ,
722+ } ,
723+ ) ;
715724}
0 commit comments