Skip to content

fix(api): correct exports and resolve schema circular dependency (v0.2.1) - #7

Merged
cdotte merged 1 commit into
mainfrom
fix/mjs-output-and-schema-ordering
Apr 23, 2026
Merged

fix(api): correct exports and resolve schema circular dependency (v0.2.1)#7
cdotte merged 1 commit into
mainfrom
fix/mjs-output-and-schema-ordering

Conversation

@cdotte

@cdotte cdotte commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

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:

  • `dist/index.js` (ESM)
  • `dist/index.cjs` (CJS)

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:

  • `index.ts` definía `accountingSchema` y re-exportaba `accounts`, `journalEntries`, `entryLines`, `periods`
  • Cada archivo de tabla importaba `accountingSchema` desde `./index.js`

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

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>
@cdotte
cdotte merged commit b3fbd59 into main Apr 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant