Skip to content

Commit d2e56d2

Browse files
build!: switch to esm
1 parent 6400703 commit d2e56d2

18 files changed

+88
-57
lines changed

.devcontainer/Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
FROM mcr.microsoft.com/devcontainers/javascript-node:20
1+
FROM mcr.microsoft.com/devcontainers/javascript-node:22
2+
3+
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
24

35
RUN corepack enable

.github/workflows/test.yml

+17-14
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,31 @@ jobs:
1111
with:
1212
node-version: 22
1313
cache: "yarn"
14-
- name: Install deps, build, then clear deps
14+
- name: Install deps and build
1515
run: |
1616
yarn install --immutable
1717
yarn build
1818
rm -rf node_modules
19-
- name: Test on Node 22
19+
20+
- name: test built package on node@22
21+
working-directory: ./package-test/
2022
run: |
23+
# CI implies --immutable
24+
yarn install --no-immutable
25+
2126
node -v
22-
node bin/test.js
23-
# Not using a matrix here since it's simpler
24-
# to just duplicate it and not spawn new instances
27+
node test.cjs
28+
node test.mjs
29+
30+
# Not using a matrix here since it's simpler
31+
# to just duplicate it and not spawn new instances
32+
2533
- uses: actions/setup-node@v4
2634
with:
2735
node-version: 20
28-
- name: Test on Node 20
36+
- name: test build package on node@20
37+
working-directory: ./package-test/
2938
run: |
3039
node -v
31-
node bin/test.js
32-
- uses: actions/setup-node@v4
33-
with:
34-
node-version: 18
35-
- name: Test on Node 18
36-
run: |
37-
node -v
38-
node bin/test.js
40+
node test.cjs
41+
node test.mjs

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"typescript.tsdk": "node_modules/typescript/lib",
23
"editor.formatOnSave": true,
34
"editor.defaultFormatter": "esbenp.prettier-vscode",
45
"editor.codeActionsOnSave": {

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ yarn add pg-error-enum
1919

2020
### Usage
2121

22-
TypeScript or ES6 Modules
23-
2422
```ts
2523
import { PostgresError } from "pg-error-enum";
2624
```
2725

28-
JavaScript
26+
<details>
27+
<summary>Legacy CommonJS</summary>
2928

3029
```js
31-
const PostgresError = require("pg-error-enum").PostgresError;
30+
const { PostgresError } = require("pg-error-enum");
3231
```
3332

33+
</details>
34+
3435
Usage
3536

3637
```ts

bin/sync.mts bin/sync.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ const getEnum = async () => {
107107

108108
const writeEnum = (enumString: string) => {
109109
writeFileSync(
110-
new URL(
111-
"../src/PostgresError.ts",
112-
// @ts-expect-error requires package.json type: module
113-
import.meta.url,
114-
),
110+
new URL("../src/PostgresError.ts", import.meta.url),
115111
enumString,
116112
);
117113
};

eslint.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import tseslint from "typescript-eslint";
2+
3+
// eslint-disable-next-line import/extensions
4+
import sharedConfig from "@nihalgonsalves/esconfig/eslint.config.shared.js";
5+
6+
export default tseslint.config(
7+
{ ignores: ["package-test", "dist"] },
8+
...sharedConfig,
9+
{
10+
rules: {
11+
"@typescript-eslint/no-duplicate-enum-values": "off",
12+
"@typescript-eslint/restrict-template-expressions": [
13+
"error",
14+
{ allowNumber: true },
15+
],
16+
},
17+
},
18+
);

eslint.config.mjs

-14
This file was deleted.

knip.jsonc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"$schema": "https://unpkg.com/knip@5/schema.json",
3-
"entry": ["src/index.ts!", "bin/sync.mts"],
3+
"entry": ["src/index.ts!", "bin/sync.ts"],
44
"project": ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"],
55
}

package-test/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.yarn

package-test/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"pg-error-enum": "../"
4+
}
5+
}

bin/test.js package-test/test.cjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// eslint-disable-next-line @typescript-eslint/no-require-imports
2-
const { PostgresError } = require("../dist");
1+
const { PostgresError } = require("pg-error-enum");
32

43
const main = () => {
54
console.log("Got code for UNIQUE_VIOLATION", PostgresError.UNIQUE_VIOLATION);
65

7-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-unsafe-enum-comparison
86
if (PostgresError.UNIQUE_VIOLATION !== "23505") {
97
throw new Error("Failed");
108
}

package-test/test.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PostgresError } from "pg-error-enum";
2+
3+
const main = () => {
4+
console.log("Got code for UNIQUE_VIOLATION", PostgresError.UNIQUE_VIOLATION);
5+
6+
if (PostgresError.UNIQUE_VIOLATION !== "23505") {
7+
throw new Error("Failed");
8+
}
9+
10+
console.log("OK");
11+
};
12+
13+
try {
14+
main();
15+
} catch (e) {
16+
console.error(e);
17+
process.exit(1);
18+
}

package-test/yarn.lock

Whitespace-only changes.

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "pg-error-enum",
3+
"type": "module",
34
"version": "0.7.3",
45
"description": "TypeScript Enum for Postgres Errors with no runtime dependencies",
56
"author": "Nihal Gonsalves <[email protected]>",
@@ -12,11 +13,13 @@
1213
"url": "https://github.com/nihalgonsalves/pg-error-enum/issues"
1314
},
1415
"homepage": "https://github.com/nihalgonsalves/pg-error-enum#readme",
15-
"main": "dist/index.js",
16-
"types": "dist/index.d.ts",
16+
"exports": {
17+
"types": "./dist/index.d.ts",
18+
"default": "./dist/index.js"
19+
},
1720
"packageManager": "[email protected]+sha512.5a0afa1d4c1d844b3447ee3319633797bcd6385d9a44be07993ae52ff4facabccafb4af5dcd1c2f9a94ac113e5e9ff56f6130431905884414229e284e37bb7c9",
1821
"scripts": {
19-
"sync": "node --experimental-strip-types bin/sync.mts",
22+
"sync": "node --experimental-strip-types bin/sync.ts",
2023
"clean": "rm -rf ./dist/",
2124
"typecheck": "tsc --noEmit --project tsconfig.json && tsc --noEmit --project tsconfig.build.json",
2225
"build": "tsc --build tsconfig.build.json",
@@ -29,6 +32,9 @@
2932
"files": [
3033
"dist/**/*"
3134
],
35+
"engines": {
36+
"node": "^20.19.0 || ^22.12.0 || >23.0.0"
37+
},
3238
"devDependencies": {
3339
"@nihalgonsalves/esconfig": "^0.11.0",
3440
"@types/node": "^18",

release-please-config.json

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
3-
"bump-minor-pre-major": true,
4-
"bump-patch-for-minor-pre-major": true,
53
"bootstrap-sha": "0e75ce7c949dbb046b8e2bd3a4afeae6c4f09b60",
64
"packages": {
75
".": {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { PostgresError } from "./PostgresError";
1+
export { PostgresError } from "./PostgresError.js";

tsconfig.build.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"target": "ES5",
54
"declaration": true,
65
"rootDir": "./src"
76
},

tsconfig.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
{
22
"extends": "@nihalgonsalves/esconfig/tsconfig.shared.json",
33
"compilerOptions": {
4-
"target": "es2020",
5-
// TODO: Also publish as ES Module?
6-
"module": "commonjs",
7-
"moduleResolution": "node",
8-
"verbatimModuleSyntax": false,
4+
// https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
5+
"lib": ["ES2023"],
6+
"module": "NodeNext",
7+
"target": "ES2023",
8+
"moduleResolution": "NodeNext",
99
"erasableSyntaxOnly": false,
1010
"outDir": "./dist",
1111
"types": ["node"]
1212
},
1313
"files": [
1414
"src/index.ts",
1515
"src/PostgresError.ts",
16-
"bin/sync.mts",
17-
"bin/test.js",
18-
"eslint.config.mjs"
16+
"bin/sync.ts",
17+
"eslint.config.js"
1918
]
2019
}

0 commit comments

Comments
 (0)