fix(api): correct exports and resolve schema circular dependency (v0.2.1) - #7
Merged
Merged
Conversation
Two production bugs in @emisso/accounting-api@0.2.0 and @emisso/accounting@0.2.0:
1. exports pointed to .mjs files that tsup never emits. tsup produces
.js for ESM (since type=module) and .cjs for CJS. Changed exports
to reference the actual emitted filenames, and swapped main to
.cjs so CommonJS consumers resolve to CJS output. Engine package
already had the correct exports in the working tree but the fix
was never shipped to npm; ship it now in the 0.2.1 patch.
2. accountingSchema (pgSchema instance) was defined in index.ts,
which also re-exported all table files. Each table file imported
accountingSchema from ./index.js — a circular dependency that, in
ESM, left accountingSchema undefined when the tables were being
evaluated. Moved accountingSchema to a dedicated _schema.ts module
that is imported by both tables and index.ts.
Verified ESM and CJS loads:
node -e "import('@emisso/accounting-api').then(m => m.accounts)" // object
node -e "require('@emisso/accounting-api').accounts" // object
All 230 tests passing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Dos bugs de producción en los paquetes `@emisso/accounting@0.2.0` y `@emisso/accounting-api@0.2.0`, reportados durante integración.
Problema 1 — `exports` apunta a archivos `.mjs` inexistentes
tsup con `format: ["cjs", "esm"]` y `"type": "module"` en `package.json` emite:
Pero los `exports` del `api` publicado apuntaban a `./dist/index.mjs` y `./dist/adapters/next.mjs` → archivos que no existen en el tarball → `ERR_MODULE_NOT_FOUND` al `import`. Y `require` apuntaba a `./dist/index.js` (que es ESM por `type=module`) → `ERR_REQUIRE_ESM`.
Fix: reescribir `exports` para que apunten a los archivos reales:
```jsonc
{
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./next": {
"import": "./dist/adapters/next.js",
"require": "./dist/adapters/next.cjs"
}
}
}
```
El `engine` ya tenía los `exports` correctos en el working tree pero el fix nunca llegó a npm (el 0.2.0 publicado todavía apunta a `.mjs`). Aprovecho el 0.2.1 para shippearlo.
Problema 2 — Circular dependency en schema de Drizzle
`packages/api/src/db/schema/` tenía un ciclo:
En ESM, cuando `index.ts` empieza a ejecutar, intenta cargar `accounts.ts`, que a su vez pide `./index.js` antes de que `index.ts` termine su primera línea — resultado: `accountingSchema = undefined` al momento de llamar `accountingSchema.table(...)`, crasheando en carga.
Fix: mover `accountingSchema` a un archivo dedicado `_schema.ts` que no importa a `index.ts`. Todas las tablas importan desde `./_schema.js`. `index.ts` solo re-exporta.
Verificación
```bash
$ node -e "import('@emisso/accounting-api').then(m => console.log(typeof m.accounts))"
ESM OK — accounts: object accountingSchema: object
$ node -e "const m = require('@emisso/accounting-api'); console.log(typeof m.accounts)"
CJS OK — accounts: object accountingSchema: object
```
230 tests passing (178 engine + 3 api + 49 cli).
Changeset
`.changeset/fix-mjs-and-schema-ordering.md` declara `patch` para ambos paquetes → al mergear sale v0.2.1 de `@emisso/accounting` y `@emisso/accounting-api`.
🤖 Generated with Claude Code