Skip to content

Commit

Permalink
fix: Reland ESM fix (#540)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
lucacasonato and soedirgo authored Jun 25, 2024
1 parent 24dcbde commit 541cd5d
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 29 deletions.
420 changes: 408 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@
"dist",
"src"
],
"main": "dist/main/index.js",
"module": "dist/module/index.js",
"types": "dist/module/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/wrapper.mjs",
"exports": {
"import": "./dist/esm/wrapper.mjs",
"require": "./dist/cjs/index.js"
},
"types": "dist/cjs/index.d.ts",
"repository": "supabase/postgrest-js",
"scripts": {
"clean": "rimraf dist docs/v2",
"format": "prettier --write \"{src,test}/**/*.ts\"",
"format": "prettier --write \"{src,test}/**/*.ts\" wrapper.mjs",
"format:check": "prettier --check \"{src,test}/**/*.ts\"",
"build": "run-s clean format build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"build:cjs": "tsc -p tsconfig.json",
"build:esm": "cpy wrapper.mjs dist/esm/",
"docs": "typedoc src/index.ts --out docs/v2",
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
"test": "run-s format:check test:types db:clean db:run test:run db:clean",
"test": "run-s format:check test:types db:clean db:run test:run db:clean && node test/smoke.cjs && node test/smoke.mjs",
"test:run": "jest --runInBand",
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean",
"test:types": "run-s build:module && tsd --files test/*.test-d.ts",
"test:types": "run-s build && tsd --files test/*.test-d.ts",
"db:clean": "cd test/db && docker-compose down --volumes",
"db:run": "cd test/db && docker-compose up --detach && wait-for-localhost 3000"
},
Expand All @@ -39,6 +43,7 @@
},
"devDependencies": {
"@types/jest": "^27.5.1",
"cpy-cli": "^5.0.0",
"jest": "^28.1.0",
"node-abort-controller": "^3.0.1",
"npm-run-all": "^4.1.5",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Always update wrapper.mjs when updating this file.
export { default as PostgrestClient } from './PostgrestClient'
export { default as PostgrestQueryBuilder } from './PostgrestQueryBuilder'
export { default as PostgrestFilterBuilder } from './PostgrestFilterBuilder'
export { default as PostgrestTransformBuilder } from './PostgrestTransformBuilder'
export { default as PostgrestBuilder } from './PostgrestBuilder'
export {
export type {
PostgrestResponse,
PostgrestResponseFailure,
PostgrestResponseSuccess,
Expand Down
10 changes: 10 additions & 0 deletions test/smoke.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Check that the ESM build works as expected (namely has the same exports as the CJS build when imported via ESM).
const assert = require("node:assert");
const postgrestjs = require("@supabase/postgrest-js");

assert(typeof postgrestjs.PostgrestClient === "function");
assert(typeof postgrestjs.PostgrestQueryBuilder === "function");
assert(typeof postgrestjs.PostgrestFilterBuilder === "function");
assert(typeof postgrestjs.PostgrestTransformBuilder === "function");
assert(typeof postgrestjs.PostgrestBuilder === "function");
assert(typeof postgrestjs.default === "undefined");
15 changes: 15 additions & 0 deletions test/smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Check that the ESM build works as expected (namely has the same exports as the CJS build when imported via ESM).
import assert from "node:assert";
import * as postgrestjs from "@supabase/postgrest-js";

assert(typeof postgrestjs.PostgrestClient === 'function');
assert(typeof postgrestjs.PostgrestQueryBuilder === 'function');
assert(typeof postgrestjs.PostgrestFilterBuilder === 'function');
assert(typeof postgrestjs.PostgrestTransformBuilder === 'function');
assert(typeof postgrestjs.PostgrestBuilder === 'function');
assert(typeof postgrestjs.default === 'object');
assert(typeof postgrestjs.default.PostgrestClient === 'function');
assert(typeof postgrestjs.default.PostgrestQueryBuilder === 'function');
assert(typeof postgrestjs.default.PostgrestFilterBuilder === 'function');
assert(typeof postgrestjs.default.PostgrestTransformBuilder === 'function');
assert(typeof postgrestjs.default.PostgrestBuilder === 'function')
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declaration": true,
"declarationMap": true,
"module": "CommonJS",
"outDir": "dist/main",
"outDir": "dist/cjs",
"sourceMap": true,
"target": "ES2017",

Expand Down
7 changes: 0 additions & 7 deletions tsconfig.module.json

This file was deleted.

25 changes: 25 additions & 0 deletions wrapper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import index from '../cjs/index.js'
const {
PostgrestClient,
PostgrestQueryBuilder,
PostgrestFilterBuilder,
PostgrestTransformBuilder,
PostgrestBuilder,
} = index

export {
PostgrestBuilder,
PostgrestClient,
PostgrestFilterBuilder,
PostgrestQueryBuilder,
PostgrestTransformBuilder,
}

// compatibility with CJS output
export default {
PostgrestClient,
PostgrestQueryBuilder,
PostgrestFilterBuilder,
PostgrestTransformBuilder,
PostgrestBuilder,
}

0 comments on commit 541cd5d

Please sign in to comment.