Skip to content

Commit 1e02837

Browse files
committed
fix(supabase): resolve jsDelivr CDN ESM import failure with .js extensions
1 parent a660d29 commit 1e02837

File tree

4 files changed

+127
-17
lines changed

4 files changed

+127
-17
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ jobs:
232232
contents: read
233233
id-token: write
234234
# Only run on master branch pushes, and only if all CI jobs succeeded
235+
# TEMPORARY: Also allow fix/jsdelivr-issue branch for testing (REVERT AFTER TESTING)
235236
if: |
236-
github.ref == 'refs/heads/master' &&
237+
(github.ref == 'refs/heads/master' || github.ref == 'refs/heads/fix/jsdelivr-issue') &&
237238
github.event_name == 'push' &&
238239
needs.ci-core.result == 'success' &&
239240
needs.ci-supabase-js.result == 'success'

packages/core/supabase-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"scripts": {
3737
"build": "npm run build:main && npm run build:module && npm run build:esm && npm run build:umd",
3838
"build:main": "tsc -p tsconfig.json",
39-
"build:module": "tsc -p tsconfig.module.json",
39+
"build:module": "tsc -p tsconfig.module.json && node scripts/fix-esm-extensions.cjs",
4040
"build:esm": "node scripts/copy-wrapper.cjs",
4141
"build:umd": "webpack --env mode=production",
4242
"test": "npm run test:types && npm run test:run",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Post-build script to add .js extensions to relative imports in ESM output.
3+
*
4+
* Node.js ESM requires explicit file extensions for relative imports.
5+
* TypeScript doesn't add these by default, so we add them after compilation.
6+
*
7+
* This fixes:
8+
* - Node.js direct ESM imports (import { createClient } from '@supabase/supabase-js')
9+
* - jsDelivr CDN ESM imports (when bundled with the module build)
10+
*/
11+
const fs = require('fs')
12+
const path = require('path')
13+
14+
function addJsExtensions(dir) {
15+
const files = fs.readdirSync(dir, { withFileTypes: true })
16+
for (const file of files) {
17+
const fullPath = path.join(dir, file.name)
18+
if (file.isDirectory()) {
19+
addJsExtensions(fullPath)
20+
} else if (file.name.endsWith('.js')) {
21+
let content = fs.readFileSync(fullPath, 'utf8')
22+
// Add .js to relative imports that don't already have an extension
23+
// Matches: from './foo' or from '../foo/bar' but not from './foo.js'
24+
content = content.replace(/(from\s+['"])(\.\.?\/[^'"]+)(?<!\.js)(['"])/g, '$1$2.js$3')
25+
fs.writeFileSync(fullPath, content)
26+
}
27+
}
28+
}
29+
30+
const targetDir = path.resolve(__dirname, '../dist/module')
31+
if (fs.existsSync(targetDir)) {
32+
addJsExtensions(targetDir)
33+
console.log('✓ Added .js extensions to ESM imports in dist/module/')
34+
} else {
35+
console.error('Error: dist/module directory not found')
36+
process.exit(1)
37+
}
Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,94 @@
1-
// Direct package re-exports - these work correctly in:
2-
// - Node.js (resolves via node_modules)
3-
// - jsDelivr (bundles with named exports)
4-
// - esm.sh (bundles correctly)
5-
// - Bundlers (webpack, vite, etc.)
6-
export * from '@supabase/auth-js'
7-
export { PostgrestError } from '@supabase/postgrest-js'
8-
export {
1+
import * as index from '../module/index.js'
2+
const {
3+
PostgrestError,
94
FunctionsHttpError,
105
FunctionsFetchError,
116
FunctionsRelayError,
127
FunctionsError,
138
FunctionRegion,
14-
} from '@supabase/functions-js'
15-
export * from '@supabase/realtime-js'
9+
SupabaseClient,
10+
createClient,
11+
GoTrueAdminApi,
12+
GoTrueClient,
13+
AuthAdminApi,
14+
AuthClient,
15+
navigatorLock,
16+
NavigatorLockAcquireTimeoutError,
17+
lockInternals,
18+
processLock,
19+
SIGN_OUT_SCOPES,
20+
AuthError,
21+
AuthApiError,
22+
AuthUnknownError,
23+
CustomAuthError,
24+
AuthSessionMissingError,
25+
AuthInvalidTokenResponseError,
26+
AuthInvalidCredentialsError,
27+
AuthImplicitGrantRedirectError,
28+
AuthPKCEGrantCodeExchangeError,
29+
AuthRetryableFetchError,
30+
AuthWeakPasswordError,
31+
AuthInvalidJwtError,
32+
isAuthError,
33+
isAuthApiError,
34+
isAuthSessionMissingError,
35+
isAuthImplicitGrantRedirectError,
36+
isAuthRetryableFetchError,
37+
isAuthWeakPasswordError,
38+
RealtimePresence,
39+
RealtimeChannel,
40+
RealtimeClient,
41+
REALTIME_LISTEN_TYPES,
42+
REALTIME_POSTGRES_CHANGES_LISTEN_EVENT,
43+
REALTIME_PRESENCE_LISTEN_EVENTS,
44+
REALTIME_SUBSCRIBE_STATES,
45+
REALTIME_CHANNEL_STATES,
46+
} = index.default || index
1647

17-
// Local exports from CJS build (Node.js can import CJS from ESM)
18-
import main from '../main/index.js'
19-
export const { SupabaseClient, createClient } = main
48+
export {
49+
PostgrestError,
50+
FunctionsHttpError,
51+
FunctionsFetchError,
52+
FunctionsRelayError,
53+
FunctionsError,
54+
FunctionRegion,
55+
SupabaseClient,
56+
createClient,
57+
GoTrueAdminApi,
58+
GoTrueClient,
59+
AuthAdminApi,
60+
AuthClient,
61+
navigatorLock,
62+
NavigatorLockAcquireTimeoutError,
63+
lockInternals,
64+
processLock,
65+
SIGN_OUT_SCOPES,
66+
AuthError,
67+
AuthApiError,
68+
AuthUnknownError,
69+
CustomAuthError,
70+
AuthSessionMissingError,
71+
AuthInvalidTokenResponseError,
72+
AuthInvalidCredentialsError,
73+
AuthImplicitGrantRedirectError,
74+
AuthPKCEGrantCodeExchangeError,
75+
AuthRetryableFetchError,
76+
AuthWeakPasswordError,
77+
AuthInvalidJwtError,
78+
isAuthError,
79+
isAuthApiError,
80+
isAuthSessionMissingError,
81+
isAuthImplicitGrantRedirectError,
82+
isAuthRetryableFetchError,
83+
isAuthWeakPasswordError,
84+
RealtimePresence,
85+
RealtimeChannel,
86+
RealtimeClient,
87+
REALTIME_LISTEN_TYPES,
88+
REALTIME_POSTGRES_CHANGES_LISTEN_EVENT,
89+
REALTIME_PRESENCE_LISTEN_EVENTS,
90+
REALTIME_SUBSCRIBE_STATES,
91+
REALTIME_CHANNEL_STATES,
92+
}
2093

21-
// Default export
22-
export default main
94+
export default index.default || index

0 commit comments

Comments
 (0)