11import { makeBaseNPMConfig , makeNPMConfigVariants , makeOtelLoaders } from '@sentry-internal/rollup-utils' ;
2- import { replacePlugin } from 'rolldown/plugins' ;
32import { createWorkerCodeBuilder } from './rollup.anr-worker.config.mjs' ;
43
54const [ anrWorkerConfig , getAnrBase64Code ] = createWorkerCodeBuilder (
@@ -12,6 +11,36 @@ const [localVariablesWorkerConfig, getLocalVariablesBase64Code] = createWorkerCo
1211 'build/esm/integrations/local-variables' ,
1312) ;
1413
14+ /**
15+ * Custom replace plugin that lazily evaluates replacement values.
16+ * This is needed because the worker scripts are built in earlier configs,
17+ * and we need to wait for their renderChunk hooks to complete before
18+ * we can get the base64-encoded worker code.
19+ */
20+ function makeLazyReplacePlugin ( replacements , options = { } ) {
21+ const { delimiters = [ '###' , '###' ] } = options ;
22+ const [ delimiterStart , delimiterEnd ] = delimiters ;
23+
24+ return {
25+ name : 'lazy-replace-plugin' ,
26+ renderChunk ( code ) {
27+ let result = code ;
28+
29+ for ( const [ key , valueFn ] of Object . entries ( replacements ) ) {
30+ const value = typeof valueFn === 'function' ? valueFn ( ) : valueFn ;
31+ const searchPattern = `${ delimiterStart } ${ key } ${ delimiterEnd } ` ;
32+ // Don't add quotes - the source already has quotes around the placeholder
33+ const replacement = value ;
34+
35+ // Replace all occurrences
36+ result = result . split ( searchPattern ) . join ( replacement ) ;
37+ }
38+
39+ return { code : result } ;
40+ } ,
41+ } ;
42+ }
43+
1544export default [
1645 ...makeOtelLoaders ( './build' , 'otel' ) ,
1746 // The workers needs to be built first since it's their output is copied in the main bundle.
@@ -27,15 +56,13 @@ export default [
2756 preserveModules : true ,
2857 } ,
2958 plugins : [
30- replacePlugin (
59+ makeLazyReplacePlugin (
3160 {
32- AnrWorkerScript : JSON . stringify ( getAnrBase64Code ( ) ) ,
33- LocalVariablesWorkerScript : JSON . stringify ( getLocalVariablesBase64Code ( ) ) ,
61+ AnrWorkerScript : getAnrBase64Code ,
62+ LocalVariablesWorkerScript : getLocalVariablesBase64Code ,
3463 } ,
3564 {
3665 delimiters : [ '###' , '###' ] ,
37- // removes some rollup warnings
38- preventAssignment : true ,
3966 } ,
4067 ) ,
4168 ] ,
0 commit comments