From 4c2721f612282a66262e7ff6e6e9b5beeef9a4ce Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Thu, 7 May 2026 21:47:02 -0700 Subject: [PATCH 1/2] fix(better-auth): keep schema-generator import lazy in CJS output (#2646) When the index entry and schema-generator entry were built in the same tsdown invocation, Rolldown injected a top-level `require("./schema-generator.cjs")` into `index.cjs`. This eagerly loaded `@zenstackhq/language` (Langium) at adapter construction time, which fails on Node CJS hosts because Langium's package.json has no `exports` main, producing ERR_PACKAGE_PATH_NOT_EXPORTED. Split the build into two tsdown invocations and import schema-generator via the package self-subpath (marked `deps.neverBundle`) so the dynamic import stays truly lazy in both CJS and ESM output. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../auth-adapters/better-auth/src/adapter.ts | 4 +++- .../auth-adapters/better-auth/tsdown.config.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/auth-adapters/better-auth/src/adapter.ts b/packages/auth-adapters/better-auth/src/adapter.ts index 27b2d4f59..3221f6952 100644 --- a/packages/auth-adapters/better-auth/src/adapter.ts +++ b/packages/auth-adapters/better-auth/src/adapter.ts @@ -187,7 +187,9 @@ export const zenstackAdapter = (db: ClientContract { - const generateSchema = (await import('./schema-generator')).generateSchema; + // Self-import via package subpath (not a relative './schema-generator') so the + // bundler treats it as external and keeps it lazy in the CJS output — see tsdown.config.ts. + const generateSchema = (await import('@zenstackhq/better-auth/schema-generator')).generateSchema; return generateSchema(file, tables, config, options); }, }; diff --git a/packages/auth-adapters/better-auth/tsdown.config.ts b/packages/auth-adapters/better-auth/tsdown.config.ts index 28af6aa31..075a684a2 100644 --- a/packages/auth-adapters/better-auth/tsdown.config.ts +++ b/packages/auth-adapters/better-auth/tsdown.config.ts @@ -1,3 +1,18 @@ import { createConfig } from '@zenstackhq/tsdown-config'; -export default createConfig({ entry: { index: 'src/index.ts', 'schema-generator': 'src/schema-generator.ts' } }); +// `index` and `schema-generator` are built as two separate tsdown invocations so that +// the lazy `await import('@zenstackhq/better-auth/schema-generator')` in the adapter +// stays lazy in the CJS output. When both entries live in a single build, Rolldown +// treats them as siblings and injects a top-level `require('./schema-generator.cjs')` +// into `index.cjs`, which eagerly pulls in `@zenstackhq/language` (Langium) at adapter +// load time. Splitting the builds hides that relationship; `neverBundle` then keeps +// the dynamic import as a package-name reference that Node resolves at first call. +export default [ + createConfig({ + entry: { index: 'src/index.ts' }, + deps: { neverBundle: ['@zenstackhq/better-auth/schema-generator'] }, + }), + createConfig({ + entry: { 'schema-generator': 'src/schema-generator.ts' }, + }), +]; From fc36d3ed2e0704545a076269d3d4d722e380c43d Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Thu, 7 May 2026 22:16:27 -0700 Subject: [PATCH 2/2] fix(better-auth): map self-import to source in tsconfig paths The package self-import `@zenstackhq/better-auth/schema-generator` resolves through the published `exports` map to `./dist/schema-generator.d.mts`, which doesn't exist on a clean CI build (tsc --noEmit runs before tsdown). Add a tsconfig `paths` mapping so TypeScript resolves the type lookup against the local source file at typecheck time. Runtime resolution remains unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/auth-adapters/better-auth/tsconfig.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/auth-adapters/better-auth/tsconfig.json b/packages/auth-adapters/better-auth/tsconfig.json index 3c7378e93..0cecd4cdc 100644 --- a/packages/auth-adapters/better-auth/tsconfig.json +++ b/packages/auth-adapters/better-auth/tsconfig.json @@ -3,7 +3,10 @@ "compilerOptions": { "rootDir": ".", "noPropertyAccessFromIndexSignature": false, - "types": ["node"] + "types": ["node"], + "paths": { + "@zenstackhq/better-auth/schema-generator": ["./src/schema-generator.ts"] + } }, "include": ["src/**/*"] }