Skip to content

Commit cdec363

Browse files
fix: switches moduleResolution to Bundler and build .d.ts files with tsup (#1437)
Co-authored-by: Shinigami <chrissi92@hotmail.de>
1 parent b9f74c2 commit cdec363

15 files changed

Lines changed: 84 additions & 80 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,8 @@ jobs:
9393
- name: Install deps
9494
run: pnpm install
9595

96-
- name: Build types
97-
run: pnpm run build:types
98-
99-
# Needed to ensure that the types after build:types are referenceable
100-
- name: Run a second install
101-
run: pnpm install
96+
- name: Build code (that includes types)
97+
run: pnpm run build
10298

10399
- name: Check scripts
104100
run: pnpm run ts-check

bin/node-pg-migrate.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import { cwd } from 'node:process';
1616
import { pathToFileURL } from 'node:url';
1717
import { format } from 'node:util';
1818
import type { ClientConfig } from 'pg';
19-
// This needs to be imported with .js extension, otherwise it will fail in esm
19+
import type ConnectionParametersType from 'pg/lib/connection-parameters';
20+
// TODO causes tests to fail when `.js` is removed
21+
// @ts-expect-error type exports from @types/pg doesn't match importing
2022
import ConnectionParameters from 'pg/lib/connection-parameters.js';
2123
import yargs from 'yargs/yargs';
2224
import type { RunnerOption } from '../src';
@@ -274,8 +276,11 @@ if (dotenv) {
274276

275277
let MIGRATIONS_DIR = argv[migrationsDirArg];
276278
let USE_GLOB = argv[useGlobArg];
277-
let DB_CONNECTION: string | ConnectionParameters | ClientConfig | undefined =
278-
process.env[argv[databaseUrlVarArg]];
279+
let DB_CONNECTION:
280+
| string
281+
| ConnectionParametersType
282+
| ClientConfig
283+
| undefined = process.env[argv[databaseUrlVarArg]];
279284
let IGNORE_PATTERN = argv[ignorePatternArg];
280285
let SCHEMA: string | string[] | undefined = argv[schemaArg];
281286
let CREATE_SCHEMA = argv[createSchemaArg];
@@ -436,7 +441,7 @@ function readJson(json: unknown): void {
436441
DB_CONNECTION,
437442
databaseUrlVarArg,
438443
json,
439-
(val): val is string | ConnectionParameters | ClientConfig =>
444+
(val): val is string | ConnectionParametersType | ClientConfig =>
440445
typeof val === 'string' || typeof val === 'object'
441446
);
442447
tsconfigPath = applyIf(tsconfigPath, tsconfigArg, json, isString);
@@ -456,7 +461,7 @@ function readJson(json: unknown): void {
456461
};
457462
}
458463
} else {
459-
DB_CONNECTION ??= json as string | ConnectionParameters | ClientConfig;
464+
DB_CONNECTION ??= json as string | ConnectionParametersType | ClientConfig;
460465
}
461466
}
462467

@@ -484,8 +489,7 @@ if (configFileName) {
484489
await readTsconfig();
485490

486491
if (useTsx) {
487-
const tsx =
488-
await tryImport<typeof import('tsx/dist/esm/api/index.mjs')>('tsx/esm');
492+
const tsx = await tryImport<typeof import('tsx/esm/api')>('tsx/esm');
489493
if (!tsx) {
490494
console.error("For TSX support, please install 'tsx' module");
491495
}

package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +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 --config tsup-bin.config.ts",
8-
"build:clean": "rimraf dist",
9-
"build:code": "tsup-node",
10-
"build:types": "tsc --project tsconfig.build.json",
11-
"build": "run-s build:clean build:code build:types build:bin",
7+
"build": "tsup-node",
128
"format": "prettier --cache --write .",
139
"lint": "eslint --cache --cache-strategy content .",
1410
"ts-check": "tsc",
@@ -18,7 +14,7 @@
1814
"docs:dev": "vitepress dev docs",
1915
"docs:build": "vitepress build docs",
2016
"docs:preview": "vitepress preview docs",
21-
"premigrate": "run-s build:bin",
17+
"premigrate": "pnpm run build",
2218
"migrate": "node bin/node-pg-migrate.js",
2319
"prepublishOnly": "pnpm run clean && pnpm install && pnpm run build",
2420
"preflight": "pnpm install && run-s format build lint test:update-snapshots ts-check"
@@ -64,12 +60,12 @@
6460
"default": "./bin/*.js"
6561
},
6662
".": {
67-
"types": "./dist/index.d.ts",
68-
"default": "./dist/index.js"
63+
"types": "./dist/bundle/index.d.ts",
64+
"default": "./dist/bundle/index.js"
6965
},
7066
"./*": {
71-
"types": "./dist/*.d.ts",
72-
"default": "./dist/*.js"
67+
"types": "./dist/legacy/*.d.ts",
68+
"default": "./dist/legacy/*.js"
7369
},
7470
"./package.json": "./package.json"
7571
},
@@ -104,6 +100,7 @@
104100
"cross-env": "7.0.3",
105101
"dotenv": "16.5.0",
106102
"dotenv-expand": "12.0.2",
103+
"esbuild-fix-imports-plugin": "1.0.21",
107104
"eslint": "9.28.0",
108105
"eslint-config-prettier": "10.1.5",
109106
"eslint-gitignore": "0.1.0",

pnpm-lock.yaml

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

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,5 @@ export type {
265265
export { PgType } from './pgType';
266266
export { PG_MIGRATE_LOCK_ID, runner } from './runner';
267267
export type { RunnerOption } from './runner';
268-
export { PgLiteral, isPgLiteral } from './utils';
268+
export { PgLiteral, escapeValue, isPgLiteral } from './utils';
269269
export type { PgLiteralValue } from './utils';

src/migration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ export class Migration implements RunMigration {
255255
: join(
256256
import.meta.dirname,
257257
'..',
258+
'..',
258259
'templates',
259260
`migration-template.${await resolveSuffix(directory, options)}`
260261
);

src/migrationBuilder.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ import * as tables from './operations/tables';
2020
import * as triggers from './operations/triggers';
2121
import * as types from './operations/types';
2222
import * as views from './operations/views';
23-
import { createSchemalize, PgLiteral } from './utils';
23+
import { createSchemalize } from './utils';
24+
/**
25+
* Export "PgLiteral" of module "src/utils/PgLiteral.ts" was reexported through module "src/utils/index.ts"
26+
* while both modules are dependencies of each other and will end up in different chunks by current Rollup settings.
27+
* This scenario is not well supported at the moment as it will produce a circular dependency between chunks
28+
* and will likely lead to broken execution order.
29+
* Either change the import in "src/migrationBuilder.ts" to point directly to the exporting module
30+
* or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
31+
*/
32+
import { PgLiteral } from './utils/PgLiteral';
2433

2534
/*
2635
* The migration builder is used to actually create a migration from instructions

test/ts/customRunner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve } from 'node:path';
22
import type { Client } from 'pg';
3-
import type { RunnerOption } from '../../dist';
4-
import { runner } from '../../dist';
3+
import type { RunnerOption } from '../../dist/bundle';
4+
import { runner } from '../../dist/bundle';
55

66
type TestOptions = {
77
count?: number;

test/ts/migrations/1-test-migration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ColumnDefinitions, MigrationBuilder } from '../../../dist';
1+
import type { ColumnDefinitions, MigrationBuilder } from '../../../dist/bundle';
22

33
export const shorthands: ColumnDefinitions | undefined = undefined;
44

test/ts/migrations/2-test-migration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ColumnDefinitions, MigrationBuilder } from '../../../dist';
1+
import type { ColumnDefinitions, MigrationBuilder } from '../../../dist/bundle';
22

33
export const shorthands: ColumnDefinitions | undefined = undefined;
44

0 commit comments

Comments
 (0)