Repository navigation
feat: add native ESM entrypoint and exports map - #1044
Merged
Merged
Conversation
@apidevtools/json-schema-ref-parser is ESM-only, so the top-level require() in refparser.js crashed on runtimes with require(esm) disabled, such as AWS Lambda nodejs20.x-24.x (ERR_REQUIRE_ESM). Load it lazily with a cached dynamic import() instead. Compile with module node16 so tsc emits a real import() in the CommonJS output and flags any future require of an ESM package. Fixes #1042 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SmjJ8wtCmDwhFrLMSsHrH
Add index.mjs that re-exports the CommonJS build, so `import OpenAPIBackend from 'openapi-backend'` returns the class rather than the module namespace, without a dual package hazard (ESM and CJS share one class instance). Add an exports map with import/require conditions and types (index.d.mts for ESM consumers); deep imports like `openapi-backend/router` keep working. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SmjJ8wtCmDwhFrLMSsHrH
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SmjJ8wtCmDwhFrLMSsHrH
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SmjJ8wtCmDwhFrLMSsHrH
# Conflicts: # src/module-compat.test.ts
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.
Follow-up to #1043 (#1042).
Problem
From native ESM,
import OpenAPIBackend from 'openapi-backend'returns the CommonJSmodule.exportsobject, not the class, sonew OpenAPIBackend()throws. The TypeScript types for ESM consumers (node16/nodenext) describe the same wrong shape.Changes
src/index.mts: builds toindex.mjs+index.d.mts. It re-exports the CommonJS build and sets the default export toOpenAPIBackend. Because it wraps the CJS build instead of shipping a second copy, ESM and CJS share one class (instanceofkeeps working, no dual-package hazard).package.json: anexportsmap withimport/requireconditions, each with its owntypes. Deep imports still resolve (openapi-backend/routerandopenapi-backend/router.js), as doesopenapi-backend/package.json.filesincludes the new outputs..gitignore: ignores the new build outputs.src/module-compat.test.ts: adds an ESM test (default import, named imports, a deep import, and the same class as CJS). The CJS test now imports by package name.Verification
--no-experimental-require-module.node16,nodenext,bundlerandnode10module resolution. The default import is typed as the class.npm run lintis clean andnpm testpasses (371 tests).Compatibility
Adding
exportsblocks deep imports of paths the map doesn't cover. Top-level.jsfiles are still covered, with or without the extension; uncommon paths like*.mapfiles no longer resolve. Suggest releasing as a minor (5.23.0).🤖 Generated with Claude Code
https://claude.ai/code/session_014SmjJ8wtCmDwhFrLMSsHrH