Skip to content

Commit 541cd5d

Browse files
fix: Reland ESM fix (#540)
* fix: use proper dual-packaging Support CommonJS & ESM. Previously the ESM build (dist/module) is broken - it's recognized as CommonJS. For more on dual-packaging: https://nodejs.org/api/packages.html#dual-commonjses-module-packages * Fix and add smoke tests --------- Co-authored-by: Bobbie Soedirgo <[email protected]>
1 parent 24dcbde commit 541cd5d

File tree

8 files changed

+474
-29
lines changed

8 files changed

+474
-29
lines changed

package-lock.json

Lines changed: 408 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,27 @@
1414
"dist",
1515
"src"
1616
],
17-
"main": "dist/main/index.js",
18-
"module": "dist/module/index.js",
19-
"types": "dist/module/index.d.ts",
17+
"main": "dist/cjs/index.js",
18+
"module": "dist/esm/wrapper.mjs",
19+
"exports": {
20+
"import": "./dist/esm/wrapper.mjs",
21+
"require": "./dist/cjs/index.js"
22+
},
23+
"types": "dist/cjs/index.d.ts",
2024
"repository": "supabase/postgrest-js",
2125
"scripts": {
2226
"clean": "rimraf dist docs/v2",
23-
"format": "prettier --write \"{src,test}/**/*.ts\"",
27+
"format": "prettier --write \"{src,test}/**/*.ts\" wrapper.mjs",
2428
"format:check": "prettier --check \"{src,test}/**/*.ts\"",
2529
"build": "run-s clean format build:*",
26-
"build:main": "tsc -p tsconfig.json",
27-
"build:module": "tsc -p tsconfig.module.json",
30+
"build:cjs": "tsc -p tsconfig.json",
31+
"build:esm": "cpy wrapper.mjs dist/esm/",
2832
"docs": "typedoc src/index.ts --out docs/v2",
2933
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
30-
"test": "run-s format:check test:types db:clean db:run test:run db:clean",
34+
"test": "run-s format:check test:types db:clean db:run test:run db:clean && node test/smoke.cjs && node test/smoke.mjs",
3135
"test:run": "jest --runInBand",
3236
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean",
33-
"test:types": "run-s build:module && tsd --files test/*.test-d.ts",
37+
"test:types": "run-s build && tsd --files test/*.test-d.ts",
3438
"db:clean": "cd test/db && docker-compose down --volumes",
3539
"db:run": "cd test/db && docker-compose up --detach && wait-for-localhost 3000"
3640
},
@@ -39,6 +43,7 @@
3943
},
4044
"devDependencies": {
4145
"@types/jest": "^27.5.1",
46+
"cpy-cli": "^5.0.0",
4247
"jest": "^28.1.0",
4348
"node-abort-controller": "^3.0.1",
4449
"npm-run-all": "^4.1.5",

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// Always update wrapper.mjs when updating this file.
12
export { default as PostgrestClient } from './PostgrestClient'
23
export { default as PostgrestQueryBuilder } from './PostgrestQueryBuilder'
34
export { default as PostgrestFilterBuilder } from './PostgrestFilterBuilder'
45
export { default as PostgrestTransformBuilder } from './PostgrestTransformBuilder'
56
export { default as PostgrestBuilder } from './PostgrestBuilder'
6-
export {
7+
export type {
78
PostgrestResponse,
89
PostgrestResponseFailure,
910
PostgrestResponseSuccess,

test/smoke.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check that the ESM build works as expected (namely has the same exports as the CJS build when imported via ESM).
2+
const assert = require("node:assert");
3+
const postgrestjs = require("@supabase/postgrest-js");
4+
5+
assert(typeof postgrestjs.PostgrestClient === "function");
6+
assert(typeof postgrestjs.PostgrestQueryBuilder === "function");
7+
assert(typeof postgrestjs.PostgrestFilterBuilder === "function");
8+
assert(typeof postgrestjs.PostgrestTransformBuilder === "function");
9+
assert(typeof postgrestjs.PostgrestBuilder === "function");
10+
assert(typeof postgrestjs.default === "undefined");

test/smoke.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Check that the ESM build works as expected (namely has the same exports as the CJS build when imported via ESM).
2+
import assert from "node:assert";
3+
import * as postgrestjs from "@supabase/postgrest-js";
4+
5+
assert(typeof postgrestjs.PostgrestClient === 'function');
6+
assert(typeof postgrestjs.PostgrestQueryBuilder === 'function');
7+
assert(typeof postgrestjs.PostgrestFilterBuilder === 'function');
8+
assert(typeof postgrestjs.PostgrestTransformBuilder === 'function');
9+
assert(typeof postgrestjs.PostgrestBuilder === 'function');
10+
assert(typeof postgrestjs.default === 'object');
11+
assert(typeof postgrestjs.default.PostgrestClient === 'function');
12+
assert(typeof postgrestjs.default.PostgrestQueryBuilder === 'function');
13+
assert(typeof postgrestjs.default.PostgrestFilterBuilder === 'function');
14+
assert(typeof postgrestjs.default.PostgrestTransformBuilder === 'function');
15+
assert(typeof postgrestjs.default.PostgrestBuilder === 'function')

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"declaration": true,
55
"declarationMap": true,
66
"module": "CommonJS",
7-
"outDir": "dist/main",
7+
"outDir": "dist/cjs",
88
"sourceMap": true,
99
"target": "ES2017",
1010

tsconfig.module.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

wrapper.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import index from '../cjs/index.js'
2+
const {
3+
PostgrestClient,
4+
PostgrestQueryBuilder,
5+
PostgrestFilterBuilder,
6+
PostgrestTransformBuilder,
7+
PostgrestBuilder,
8+
} = index
9+
10+
export {
11+
PostgrestBuilder,
12+
PostgrestClient,
13+
PostgrestFilterBuilder,
14+
PostgrestQueryBuilder,
15+
PostgrestTransformBuilder,
16+
}
17+
18+
// compatibility with CJS output
19+
export default {
20+
PostgrestClient,
21+
PostgrestQueryBuilder,
22+
PostgrestFilterBuilder,
23+
PostgrestTransformBuilder,
24+
PostgrestBuilder,
25+
}

0 commit comments

Comments
 (0)