Skip to content

Commit ea6adec

Browse files
authored
fix: fix esm (#1148)
1 parent c08f807 commit ea6adec

7 files changed

+93
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ versions.json
8282
/migrations
8383
/bin/*.js
8484
/bin/*.mjs
85+
/bin/esm
8586

8687
TAGS
8788
REVISION

bin/node-pg-migrate.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env node
22

33
import type { DotenvConfigOptions } from 'dotenv';
4+
// Import as node-pg-migrate, so tsup does not self-reference as '../dist'
5+
// otherwise this could not be imported by esm
6+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
7+
// @ts-ignore: when a clean was made, the types are not present in the first run
8+
import { default as migrationRunner, Migration } from 'node-pg-migrate';
49
import { readFileSync } from 'node:fs';
510
import { resolve } from 'node:path';
611
import { format } from 'node:util';
712
import type { ClientConfig } from 'pg';
8-
import ConnectionParameters from 'pg/lib/connection-parameters';
13+
// This needs to be imported with .js extension, otherwise it will fail in esm
14+
import ConnectionParameters from 'pg/lib/connection-parameters.js';
915
import yargs from 'yargs/yargs';
10-
import { default as migrationRunner, Migration } from '../dist';
1116
import type { RunnerOption } from '../src';
1217
import type { FilenameFormat } from '../src/migration';
1318

@@ -461,10 +466,16 @@ if (action === 'create') {
461466
ignorePattern: IGNORE_PATTERN,
462467
}),
463468
})
464-
.then((migrationPath) => {
465-
console.log(format('Created migration -- %s', migrationPath));
466-
process.exit(0);
467-
})
469+
.then(
470+
(
471+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
472+
// @ts-ignore: when a clean was made, the types are not present in the first run
473+
migrationPath
474+
) => {
475+
console.log(format('Created migration -- %s', migrationPath));
476+
process.exit(0);
477+
}
478+
)
468479
.catch((error: unknown) => {
469480
console.error(error);
470481
process.exit(1);

package.json

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "PostgreSQL database migration management tool for node.js",
55
"scripts": {
66
"clean": "rimraf .eslintcache dist pnpm-lock.yaml node_modules",
7-
"build:bin": "tsup-node ./bin/node-pg-migrate.ts --clean false --out-dir ./bin",
7+
"build:bin": "tsup-node --config tsup-bin.config.ts",
88
"build:clean": "rimraf dist",
99
"build:code": "tsup-node",
1010
"build:types": "tsc --project tsconfig.build.json",
@@ -28,31 +28,29 @@
2828
},
2929
"type": "commonjs",
3030
"main": "dist/index.js",
31-
"module": "dist/index.mjs",
31+
"module": "dist/esm/index.mjs",
3232
"types": "dist/index.d.ts",
3333
"exports": {
3434
"./dist/*": {
3535
"types": "./dist/*.d.ts",
3636
"require": "./dist/*.js",
37-
"import": "./dist/*.mjs",
37+
"import": "./dist/esm/*.mjs",
3838
"default": "./dist/*.js"
3939
},
40-
"./bin/node-pg-migrate": {
41-
"require": "./bin/node-pg-migrate.js",
42-
"import": "./bin/node-pg-migrate.mjs",
43-
"default": "./bin/node-pg-migrate.js"
40+
"./bin/*": {
41+
"require": "./bin/*.js",
42+
"import": "./bin/*.mjs",
43+
"default": "./bin/*.js"
4444
},
4545
".": {
4646
"types": "./dist/index.d.ts",
4747
"require": "./dist/index.js",
48-
"import": "./dist/index.mjs",
48+
"import": "./dist/esm/index.mjs",
4949
"default": "./dist/index.js"
5050
},
5151
"./*": {
5252
"types": "./dist/*.d.ts",
53-
"require": "./dist/*.js",
54-
"import": "./dist/*.mjs",
55-
"default": "./dist/*.js"
53+
"require": "./dist/*.js"
5654
},
5755
"./package.json": "./package.json"
5856
},
@@ -122,6 +120,7 @@
122120
"eslint-plugin-prettier": "5.1.3",
123121
"eslint-plugin-unicorn": "52.0.0",
124122
"json5": "2.2.3",
123+
"node-pg-migrate": "file:.",
125124
"npm-run-all2": "6.1.2",
126125
"pg": "8.11.5",
127126
"prettier": "3.2.5",

pnpm-lock.yaml

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/db.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
*/
44
import { inspect } from 'node:util';
55
import type {
6+
Client,
67
ClientBase,
78
ClientConfig,
89
QueryArrayConfig,
910
QueryArrayResult,
1011
QueryConfig,
1112
QueryResult,
1213
} from 'pg';
13-
import { Client } from 'pg';
14+
// This needs to be imported as `*`, otherwise it will fail in esm
15+
import * as pg from 'pg';
1416
import type { DB, Logger } from './types';
1517

1618
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -54,7 +56,7 @@ function db(
5456

5557
const client: Client = isExternalClient
5658
? (connection as Client)
57-
: new Client(connection as string | ClientConfig);
59+
: new pg.Client(connection as string | ClientConfig);
5860

5961
const beforeCloseListeners: any[] = [];
6062

tsup-bin.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig({
4+
entry: ['bin/node-pg-migrate.ts'],
5+
outDir: 'bin',
6+
clean: false,
7+
format: ['esm', 'cjs'],
8+
target: ['es2020', 'node16'],
9+
dts: false,
10+
minify: false,
11+
sourcemap: false,
12+
bundle: false,
13+
});

tsup.config.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { defineConfig } from 'tsup';
22

3-
export default defineConfig({
4-
entry: ['src/**/*.ts'],
5-
outDir: 'dist',
6-
clean: true,
7-
format: ['esm', 'cjs'],
8-
target: ['es2020', 'node16'],
9-
dts: false,
10-
minify: false,
11-
splitting: true,
12-
bundle: false,
13-
});
3+
export default defineConfig([
4+
// src esm
5+
{
6+
entry: ['src/index.ts'],
7+
outDir: 'dist/esm',
8+
clean: true,
9+
format: 'esm',
10+
target: ['es2020', 'node16'],
11+
dts: false,
12+
minify: false,
13+
sourcemap: false,
14+
bundle: true,
15+
outExtension: () => ({ js: '.mjs' }),
16+
},
17+
// src cjs
18+
{
19+
entry: ['src/**/*.ts'],
20+
outDir: 'dist',
21+
clean: false,
22+
format: 'cjs',
23+
target: ['es2020', 'node16'],
24+
dts: false,
25+
minify: false,
26+
sourcemap: false,
27+
bundle: false,
28+
},
29+
]);

0 commit comments

Comments
 (0)