Skip to content

Commit 334de16

Browse files
committed
fix(misc): another approach
1 parent 6fdf914 commit 334de16

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

packages/core/supabase-js/test/bundle-hermes-compat.test.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
* - No new Function() calls -> browsers with a strict Content-Security-Policy
88
* (no 'unsafe-eval') block new Function() identically to eval() at runtime.
99
*
10-
* Both constraints are satisfied by pointing the CJS bundle at the TypeScript
11-
* CommonJS-compiled output of @supabase/tracing (dist/main/), where import()
12-
* has already been downcompiled to require() by tsc.
10+
* Both constraints are satisfied by telling Rolldown to honor the `require`
11+
* export condition when building dist/index.cjs (see tsdown.config.ts).
12+
* @supabase/tracing's `require` condition points at its tsc-built CJS output
13+
* (dist/main/), where dynamic `import()` has already been lowered to require().
1314
*
1415
* Run with: node test/bundle-hermes-compat.test.cjs
1516
*/

packages/core/supabase-js/tsdown.config.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { defineConfig } from 'tsdown'
2-
import { createRequire } from 'module'
32
import { fileURLToPath } from 'url'
43
import { dirname, join } from 'path'
54

@@ -29,17 +28,21 @@ export default defineConfig([
2928
target: 'es2017',
3029
inputOptions: (_options, format) => {
3130
if (format === 'cjs') {
32-
// Rolldown always resolves @supabase/tracing via its 'import' export
33-
// condition (ESM source) even for CJS output — so import() ends up in
34-
// index.cjs. hermesc (Hermes bytecode compiler for React Native) rejects
35-
// import() at parse time; browser strict CSP blocks new Function() at
36-
// runtime. Aliasing to the package's 'main' field (dist/main/) gives us
37-
// TypeScript's CJS-compiled require() — safe for both constraints.
31+
// Rolldown defaults to the 'import' export condition even when emitting
32+
// CJS, so @supabase/tracing's ESM source (with native `import()`) ends
33+
// up inlined into dist/index.cjs. hermesc (Hermes bytecode compiler for
34+
// React Native release builds) rejects `import()` at parse time, before
35+
// dead-code elimination — so the syntax has to be physically absent
36+
// from the CJS bundle.
37+
//
38+
// Tell Rolldown to honor the 'require' export condition for this build.
39+
// For @supabase/tracing that resolves to dist/main/index.js — tsc's
40+
// CJS output, where dynamic `import()` has been lowered to require().
41+
// dist/index.mjs intentionally keeps native `import()` (valid ESM,
42+
// not blocked by browser CSP, and Hermes never sees the ESM bundle).
3843
return {
3944
resolve: {
40-
alias: {
41-
'@supabase/tracing': createRequire(import.meta.url).resolve('@supabase/tracing'),
42-
},
45+
conditionNames: ['require', 'node', 'default'],
4346
},
4447
}
4548
}

0 commit comments

Comments
 (0)