Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split migrations into separte bundle #29

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ Pass the migration source to all migrate commands as follows:

```ts
import {join} from 'node:path'
import {KyselyFsMigrationSource, PGColdDialect} from 'kysely-knex'
import {PGColdDialect} from 'kysely-knex'
import {KyselyFsMigrationSource} from 'kysely-knex/migrations'
import {knex} from '../src/knex'
import {kysely} from '../src/kysely'

Expand All @@ -130,7 +131,8 @@ or pass it to Knex CLI globally:
`knexfile.ts`:

```ts
const {KyselyFsMigrationSource, PGColdDialect} = require('kysely-knex')
const {PGColdDialect} = require('kysely-knex')
const {KyselyFsMigrationSource} = require('kysely-knex/migrations')

module.exports = {
// ...
Expand Down
3 changes: 2 additions & 1 deletion knexfile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {KyselyFsMigrationSource, PGColdDialect} = require('./dist/cjs')
const {PGColdDialect} = require('./dist')
const {KyselyFsMigrationSource} = require('./dist/migrations')

module.exports = {
client: 'pg',
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
"homepage": "https://github.com/kysely-org/kysely-knex",
"author": "Igal Klebanov <[email protected]>",
"license": "MIT",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./migrations": {
"import": "./dist/migrations.mjs",
"require": "./dist/migrations.js"
}
},
"files": [
Expand Down
72 changes: 14 additions & 58 deletions scripts/dist-fix.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,26 @@
const {
mkdir,
readdir,
rename,
rm,
writeFile,
copyFile,
readFile,
unlink,
} = require('node:fs/promises')
// @ts-check
const {readdir, writeFile, readFile} = require('node:fs/promises')
const path = require('node:path')

;(async () => {
const distPath = path.join(__dirname, '../dist')
const distCjsPath = path.join(distPath, 'cjs')
const distEsmPath = path.join(distPath, 'esm')

const [dist, distEsm] = await Promise.all([
readdir(distPath),
readdir(distEsmPath),
rm(distCjsPath, {force: true, recursive: true}),
])
const dist = await readdir(distPath)

// Inject type declarations for deno.
// See https://docs.deno.com/runtime/manual/advanced/typescript/types
await Promise.all([
echocrow marked this conversation as resolved.
Show resolved Hide resolved
mkdir(distCjsPath),
writeFile(
path.join(distEsmPath, 'package.json'),
JSON.stringify({type: 'module', sideEffects: false}),
),
...dist
.filter((distFilePath) => distFilePath.match(/\.d\.ts$/))
.map((distFilePath) =>
copyFile(
path.join(distPath, distFilePath),
path.join(distEsmPath, distFilePath),
),
),
...distEsm
.filter((esmFilePath) => esmFilePath.match(/\.js$/))
.map(async (esmFilePath) => {
const distEsmFilePath = path.join(distEsmPath, esmFilePath)

const esmFile = await readFile(distEsmFilePath)
const esmFileContents = esmFile.toString()

const dtsFilePath = `./${esmFilePath.replace('.js', '.d.ts')}`

const denoFriendlyEsmFileContents = [
`/// <reference types="${dtsFilePath}" />`,
esmFileContents,
].join('\n')
.filter((filePath) => filePath.match(/\.m?js$/))
.filter((filePath) => !filePath.startsWith('chunk-'))
echocrow marked this conversation as resolved.
Show resolved Hide resolved
.map(async (filename) => {
const distFilePath = path.join(distPath, filename)

await unlink(distEsmFilePath)
const dtsFilePath = filename.replace(/\.(m?)js$/, '.d.$1ts')
const denoFriendlyJsFileContents =
`/// <reference types="${dtsFilePath}" />\n` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a minimal deno test @ tests/deno to verify we're building this stuff correctly..
see https://github.com/kysely-org/kysely/tree/master/test/deno for reference (just local is fine).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

disclaimer: never used deno, so very new to both it and this kind of deno testing. 😅 tried to roughly replicate the setup from the kysely repo with a new bare-bones test. not sure if this aligns with what you had in mind though(?)

I also don't think this setup actually tests our TS-only /// <reference ...> injection at all. If I alter the reference, the deno script still completes fine. So this might only somewhat tests if the bundled runtime code is deno-compatible.

(await readFile(distFilePath)).toString()

await writeFile(distEsmFilePath, denoFriendlyEsmFileContents)
await writeFile(distFilePath, denoFriendlyJsFileContents)
}),
])

await Promise.all([
writeFile(
path.join(distCjsPath, 'package.json'),
JSON.stringify({type: 'commonjs', sideEffects: false}),
),
...dist
.filter((filePath) => filePath.match(/\.[t|j]s(\.map)?$/))
.map((filePath) =>
rename(path.join(distPath, filePath), path.join(distCjsPath, filePath)),
),
])
})()
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ export * from './cold-dialect/pg/cold-dialect.js'
export * from './cold-dialect/pg/query-compiler.js'
export * from './cold-dialect/pg/results-parser.js'
export * from './cold-dialect/results-parser.js'
export * from './fs-migration-source.js'
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {Knex} from 'knex'
// @ts-ignore
import {FsMigrations} from 'knex/lib/migrations/migrate/sources/fs-migrations.js'
import {Kysely} from 'kysely'
import type {ColdDialect} from './cold-dialect/cold-dialect.js'
import {KyselyKnexDialect} from './dialect.js'
import type {ColdDialect} from '../cold-dialect/cold-dialect.js'
import {KyselyKnexDialect} from '../dialect.js'

export interface KyselyFsMigrationSourceProps {
kyselySubDialect: ColdDialect
Expand Down
1 change: 1 addition & 0 deletions src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fs-migration-source.js'
4 changes: 2 additions & 2 deletions tests/nodejs/fs-migration-source.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {join} from 'node:path'
import {KyselyFsMigrationSource} from '../..'
import {KyselyFsMigrationSource} from '../../dist/migrations'
import {
CONFIGS,
SUPPORTED_DIALECTS,
Expand All @@ -12,7 +12,7 @@ import {
for (const dialect of SUPPORTED_DIALECTS) {
describe(`KyselyFsMigrationSource: ${dialect}`, () => {
let ctx: TestContext
let migrationSource: KyselyFsMigrationSource
let migrationSource: InstanceType<typeof KyselyFsMigrationSource>

before(async function () {
ctx = await initTest(this, dialect)
Expand Down
6 changes: 4 additions & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {defineConfig} from 'tsup'
export default defineConfig({
clean: true,
dts: true,
entry: ['./src/index.ts'],
entry: {
index: 'src/index.ts',
migrations: 'src/migrations/index.ts',
},
format: ['cjs', 'esm'],
legacyOutput: true,
outDir: 'dist',
sourcemap: true,
})