@@ -142,3 +142,49 @@ describe('spotlight', () => {
142142 } ) ;
143143 }
144144} ) ;
145+
146+ describe ( '__VITE_SPOTLIGHT_ENV__ rollup replacement' , ( ) => {
147+ // Test that our rollup build correctly replaces __VITE_SPOTLIGHT_ENV__
148+ // ESM bundles should have import.meta.env access, CJS should have undefined
149+
150+ function readSdkFile ( packageName : string , format : 'esm' | 'cjs' ) : string {
151+ const sdkPath = path . join (
152+ rootDir ( ) ,
153+ 'packages' ,
154+ packageName ,
155+ 'build' ,
156+ format ,
157+ 'sdk.js' ,
158+ ) ;
159+ if ( ! fs . existsSync ( sdkPath ) ) {
160+ throw new Error ( `SDK file not found: ${ sdkPath } . Make sure to run yarn build:dev first.` ) ;
161+ }
162+ return fs . readFileSync ( sdkPath , 'utf8' ) ;
163+ }
164+
165+ // Remove comments from code to test only actual code
166+ function stripComments ( code : string ) : string {
167+ // Remove single-line comments
168+ return code . replace ( / \/ \/ .* $ / gm, '' ) . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' ) ;
169+ }
170+
171+ test . each ( [ 'react' , 'vue' , 'svelte' , 'solid' ] as const ) (
172+ '%s ESM bundle contains import.meta.env?.VITE_SENTRY_SPOTLIGHT access' ,
173+ packageName => {
174+ const code = stripComments ( readSdkFile ( packageName , 'esm' ) ) ;
175+ // ESM bundles should have import.meta.env access for Vite support
176+ // The replacement is: import.meta.env?.VITE_SENTRY_SPOTLIGHT
177+ expect ( code ) . toMatch ( / i m p o r t \. m e t a \. e n v \? \. [ A - Z _ ] + S P O T L I G H T / ) ;
178+ } ,
179+ ) ;
180+
181+ test . each ( [ 'react' , 'vue' , 'svelte' , 'solid' ] as const ) (
182+ '%s CJS bundle does not contain import.meta.env (CJS incompatible)' ,
183+ packageName => {
184+ const code = stripComments ( readSdkFile ( packageName , 'cjs' ) ) ;
185+ // CJS bundles should NOT have import.meta.env as it's ESM-only syntax
186+ // The __VITE_SPOTLIGHT_ENV__ placeholder should be replaced with 'undefined'
187+ expect ( code ) . not . toMatch ( / i m p o r t \. m e t a \. e n v / ) ;
188+ } ,
189+ ) ;
190+ } ) ;
0 commit comments