Skip to content

build(nextjs): ship a complete CommonJS build - #554

Open
DonOmalVindula wants to merge 1 commit into
asgardeo:mainfrom
DonOmalVindula:fix/nextjs-cjs-build
Open

build(nextjs): ship a complete CommonJS build#554
DonOmalVindula wants to merge 1 commit into
asgardeo:mainfrom
DonOmalVindula:fix/nextjs-cjs-build

Conversation

@DonOmalVindula

Copy link
Copy Markdown
Contributor

Problem

esbuild.config.mjs ran with bundle: false on three entry points, so dist/cjs contained only index.js, server/index.js and middleware.js, each requiring files that do not exist:

$ node -e "require('@asgardeo/nextjs')"
Error: Cannot find module './AsgardeoNextClient'

The ESM build only worked because the build script then ran tsc --outDir dist/esm, which re-emitted every file on top of esbuild's output. Anything resolving the require condition (Jest, CommonJS tooling) got the broken tree. On top of that the package is "type": "module", so Node parses dist/cjs/*.js as ESM, and the top-level types field pointed at dist/index.d.ts, which does not exist.

Fix

  • Every source file (tests excluded) is transpiled on its own for both formats, mirroring the src tree. This package cannot be bundled like its siblings: Next.js needs 'use client' / 'use server' on the module that defines each component or server action, and esbuild keeps those directives in unbundled output (verified: 19 use client and 23 use server files in src, dist/esm and dist/cjs).
  • dist/cjs/package.json with "type": "commonjs" is written by the build.
  • tsc now emits declarations only (--emitDeclarationOnly, into dist/types as before).
  • The type-only re-exports in src/client/index.ts are marked export type; esbuild cannot elide them per file, so they would otherwise become runtime re-exports of names that do not exist.
  • types, homepage and repository.directory in package.json point at the right paths.

Testing

  • pnpm build: 62 files in each of dist/esm and dist/cjs, no stray tsc JavaScript, declarations in dist/types.
  • require('@asgardeo/nextjs/middleware') loads and exposes asgardeoMiddleware and createRouteMatcher.
  • require('@asgardeo/nextjs') and require('@asgardeo/nextjs/server') now get past this package but stop in @asgardeo/browser's CommonJS bundle, which begins with import {Buffer} from 'buffer/index.js' (and the sibling packages' dist/cjs also lack a "type": "commonjs" package.json). Those are pre-existing issues in the other packages and are left for a separate PR.
  • pnpm lint, pnpm vitest run and tsc --noEmit for @asgardeo/nextjs pass.

Changeset included (@asgardeo/nextjs patch).

🤖 Generated with Claude Code

esbuild ran unbundled on three entry files only, so dist/cjs contained just
those three and `require('@asgardeo/nextjs')` failed with "Cannot find module
'./AsgardeoNextClient'". The ESM output only worked because `tsc` re-emitted
every file into dist/esm afterwards.

- Transpile every source file (tests excluded) for both formats; a bundle
  cannot keep the per-module 'use client' / 'use server' directives, which
  esbuild preserves in unbundled output.
- Write dist/cjs/package.json with "type": "commonjs" so Node does not parse
  the CommonJS files as ESM under the package's "type": "module".
- Let tsc emit declarations only.
- Mark the type-only re-exports of the client entry with `export type`, since
  esbuild cannot elide them per file.
- Fix the package's `types`, `homepage` and `repository` paths.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 41 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: b723d942-38ff-44fd-a119-a47bb654f2db

📥 Commits

Reviewing files that changed from the base of the PR and between 409ebae and e0d3a07.

📒 Files selected for processing (4)
  • .changeset/nextjs-cjs-build.md
  • packages/nextjs/esbuild.config.mjs
  • packages/nextjs/package.json
  • packages/nextjs/src/client/index.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@asgardeo-github-bot

Copy link
Copy Markdown

🦋 Changeset detected

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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.

2 participants