22 * Tests to verify build artifacts are correctly formatted for their module type.
33 *
44 * These tests verify that:
5- * - CJS builds don't contain import.meta (would cause syntax errors)
6- * - ESM builds DO contain import.meta. env checks
7- * - Empty strings are filtered by resolveSpotlightOptions
5+ * - Both CJS and ESM builds contain the getEnvValue function
6+ * - Both builds use process. env for environment variable access
7+ * - Neither build contains import.meta (we intentionally don't use it to avoid CJS errors)
88 */
99
1010import * as fs from 'fs' ;
@@ -14,7 +14,7 @@ import { describe, expect, it } from 'vitest';
1414const REPO_ROOT = path . resolve ( __dirname , '../../..' ) ;
1515const BROWSER_BUILD = path . join ( REPO_ROOT , 'packages/browser/build/npm' ) ;
1616
17- describe ( 'Build Artifacts - CJS vs ESM ' , ( ) => {
17+ describe ( 'Build Artifacts - env.ts ' , ( ) => {
1818 describe ( 'CJS builds' , ( ) => {
1919 it ( 'env.js CJS build does NOT contain import.meta syntax' , ( ) => {
2020 const cjsDevPath = path . join ( BROWSER_BUILD , 'cjs/dev/utils/env.js' ) ;
@@ -59,44 +59,45 @@ describe('Build Artifacts - CJS vs ESM', () => {
5959 } ) ;
6060
6161 describe ( 'ESM builds' , ( ) => {
62- it ( 'env.js ESM build DOES contain import.meta.env check' , ( ) => {
62+ it ( 'env.js ESM build also does NOT contain import.meta syntax' , ( ) => {
63+ // We intentionally don't use import.meta.env because it would require
64+ // a rollup plugin to strip it from CJS builds, and we want consistent
65+ // behavior across both module formats.
6366 const esmDevPath = path . join ( BROWSER_BUILD , 'esm/dev/utils/env.js' ) ;
6467 const esmProdPath = path . join ( BROWSER_BUILD , 'esm/prod/utils/env.js' ) ;
6568
6669 if ( fs . existsSync ( esmDevPath ) ) {
6770 const content = fs . readFileSync ( esmDevPath , 'utf-8' ) ;
6871
69- // SHOULD contain import.meta checks
70- expect ( content ) . toContain ( 'import.meta' ) ;
71- expect ( content ) . toContain ( 'import.meta.env' ) ;
72+ // Filter out comments
73+ const lines = content
74+ . split ( '\n' )
75+ . filter ( line => ! line . trim ( ) . startsWith ( '//' ) && ! line . trim ( ) . startsWith ( '*' ) ) ;
76+ const codeOnly = lines . join ( '\n' ) ;
77+
78+ // Should NOT contain import.meta in actual code
79+ expect ( codeOnly ) . not . toContain ( 'import.meta.env' ) ;
7280 }
7381
7482 if ( fs . existsSync ( esmProdPath ) ) {
7583 const content = fs . readFileSync ( esmProdPath , 'utf-8' ) ;
7684
77- expect ( content ) . toContain ( 'import.meta' ) ;
78- expect ( content ) . toContain ( 'import.meta.env' ) ;
79- }
80- } ) ;
81-
82- it ( 'env.js ESM build contains rollup markers for ESM-only code' , ( ) => {
83- const esmDevPath = path . join ( BROWSER_BUILD , 'esm/dev/utils/env.js' ) ;
84-
85- if ( fs . existsSync ( esmDevPath ) ) {
86- const content = fs . readFileSync ( esmDevPath , 'utf-8' ) ;
85+ const lines = content
86+ . split ( '\n' )
87+ . filter ( line => ! line . trim ( ) . startsWith ( '//' ) && ! line . trim ( ) . startsWith ( '*' ) ) ;
88+ const codeOnly = lines . join ( '\n' ) ;
8789
88- // SHOULD contain the rollup markers (they're kept in output as comments)
89- expect ( content ) . toContain ( 'rollup-esm-only' ) ;
90+ expect ( codeOnly ) . not . toContain ( 'import.meta.env' ) ;
9091 }
9192 } ) ;
9293
93- it ( 'env.js ESM build contains process.env check as well ' , ( ) => {
94+ it ( 'env.js ESM build contains process.env check' , ( ) => {
9495 const esmDevPath = path . join ( BROWSER_BUILD , 'esm/dev/utils/env.js' ) ;
9596
9697 if ( fs . existsSync ( esmDevPath ) ) {
9798 const content = fs . readFileSync ( esmDevPath , 'utf-8' ) ;
9899
99- // SHOULD contain process.env check too (for compatibility)
100+ // SHOULD contain process.env check
100101 expect ( content ) . toContain ( 'process.env' ) ;
101102 }
102103 } ) ;
@@ -117,5 +118,20 @@ describe('Build Artifacts - CJS vs ESM', () => {
117118 expect ( content ) . toContain ( 'getEnvValue' ) ;
118119 }
119120 } ) ;
121+
122+ it ( 'contain globalThis check for bundler-injected values' , ( ) => {
123+ const cjsPath = path . join ( BROWSER_BUILD , 'cjs/dev/utils/env.js' ) ;
124+ const esmPath = path . join ( BROWSER_BUILD , 'esm/dev/utils/env.js' ) ;
125+
126+ if ( fs . existsSync ( cjsPath ) ) {
127+ const content = fs . readFileSync ( cjsPath , 'utf-8' ) ;
128+ expect ( content ) . toContain ( 'globalThis' ) ;
129+ }
130+
131+ if ( fs . existsSync ( esmPath ) ) {
132+ const content = fs . readFileSync ( esmPath , 'utf-8' ) ;
133+ expect ( content ) . toContain ( 'globalThis' ) ;
134+ }
135+ } ) ;
120136 } ) ;
121137} ) ;
0 commit comments