Skip to content

ESM-first sources with an exports map, esbuild build, bundled types (v3) - #370

Merged
jordanbtucker merged 7 commits into
json5:mainfrom
bybraveHQ:esm-exports
Oct 15, 2026
Merged

jordanbtucker merged 7 commits into
json5:mainfrom
bybraveHQ:esm-exports

Conversation

@bybraveHQ

@bybraveHQ bybraveHQ commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

Brings the packaging work from @bybrave/json5 back upstream, as discussed in #348. Prettier switched to that fork in prettier/prettier#19843, so this code has been in production there since August.

Closes #240, closes #348. Supersedes #364, #243 and #241.

What changes

  • lib/ is now ES module sources; package.json gets "type": "module" and an exports map. The CommonJS entry (dist/index.cjs) is generated from the sources, so require('json5') and import {parse, stringify} from 'json5' resolve to the same shape.
  • Build: rollup 0.64 + buble + core-js 2 are replaced by a single esbuild step (build/build.js) producing the CJS entry, a minified ESM bundle and the minified browser global. build/unicode.js and build/package.js are kept and converted to ESM.
  • Tests: tap + sinon are replaced by node:test; the suite is ported 1:1 (178 tests). test/type-declarations.ts compiles the bundled types with tsc --strict.
  • Types: one lib/index.d.ts matching both entries, including the stringify(value, options) overload.
  • CI: GitHub Actions on Node.js 22, 24 and 26 (replaces the Travis config).
  • No runtime dependencies; dev dependencies drop from 19 to 5.

Breaking changes (this is a major)

  • Node.js ≥ 22. Browser bundles target ES2015 instead of ES5.
  • Deep imports are limited by the exports map to json5/register (plus the legacy json5/lib/register and json5/require aliases, kept so existing configs keep working).
  • The eslint 5 setup was removed together with the old toolchain. Happy to re-add linting on a current eslint in a follow-up if you want it.

Version is left at 2.2.3 for you to bump on release. Behavior changes from the fork (opt-in BigInt parsing, #292; lone-surrogate escaping, #192) are intentionally not part of this PR.

Verified

  • npm test, npm run test-types, npm run build-unicode (regenerates lib/unicode.js byte-identical), npm run build-package
  • npm pack installed into a clean project: require('json5'), import JSON5, {parse, stringify} from 'json5', require('json5/register'), require('json5/lib/register'), the CLI, dist/index.min.mjs and the window.JSON5 global

https://claude.ai/code/session_01SQ7pqqZUEYY57CkSL3wzHN

@bybraveHQ bybraveHQ mentioned this pull request Sep 17, 2026
Comment thread build/unicode.js Resolved
Comment thread build/package.js Resolved
Comment thread lib/index.js Resolved
Comment thread lib/util.js Resolved
Comment thread package.json Resolved
Comment thread build/package.js Resolved
Comment thread build/build.js Resolved
Comment thread lib/cli.js Resolved
Comment thread test/cli.test.js Resolved
@bybraveHQ
bybraveHQ force-pushed the esm-exports branch 2 times, most recently from e774622 to 18be0e6 Compare September 18, 2026 15:46
@bybraveHQ

Copy link
Copy Markdown
Collaborator Author

@aseemk Following up on this. fisker's review rounds are addressed (all threads answered), and CI hasn't run yet because workflow runs from first-time contributors need a maintainer's approval. When you have a moment, could you approve the workflow run and take a look? Happy to adjust anything.

@bybraveHQ

Copy link
Copy Markdown
Collaborator Author

@jordanbtucker Hi Jordan, reaching out since you're also a maintainer here. This PR is the ESM/exports part of what #348 asked for: ES module sources, an exports map with a CommonJS entry, bundled types and CI. fisker from the Prettier core team has reviewed it, and Prettier already ships with these changes through my fork.

Aseem invited me to help maintain json5 in #348, but he hasn't been active since September. CI here still needs a maintainer to approve the first workflow run. Could you take a look, or approve the run? I know you had a TypeScript v3 in the works on the v3 branch. I'm happy to adapt this PR to whatever direction you prefer.

@jordanbtucker jordanbtucker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for your patience. I'd like to use this as the basis for v3. The library code is essentially untouched, and it fixes a lot of long-standing packaging problems.

Before we merge this, let's address the following items (details in the comments):

  • CommonJS type declarations
  • Test coverage
  • Exports map backwards compatibility
  • Escapes in test cases
  • README and CHANGELOG accuracy

This PR also drops ESLint. But let's handle linting and formatting in a separate PR for the v3 milestone.

Once this is merged, let's also plan to port lib/ to TypeScript in a separate PR, so the type declarations are generated from the source and can't drift again.

Comment thread package.json Resolved
Comment thread package.json Resolved
Comment thread test/type-declarations.ts Resolved
Comment thread package.json Resolved
Comment thread package.json Resolved
Comment thread test/stringify.test.js Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
@bybraveHQ

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough review, Jordan. Everything is addressed in c1db19e.

CommonJS now has its own lib/index.d.cts with export =, and each exports condition points at its own types. The type tests import from 'json5' and compile with --module node16. The new .cts test failed with TS1471 before the fix, as you said. npm test now enforces 100% coverage, ./lib/require replaces ./require, and the escapes, eslint comments, README and CHANGELOG follow your suggestions.

I also tried the packed tarball in consumer projects. With node16 and nodenext, both .cts and .mts files compile and run. A default import under "module": "commonjs" without esModuleInterop now fails at compile time with TS1259 instead of crashing at runtime.

Agreed on handling linting in a separate PR and on porting lib/ to TypeScript after this lands. I'm happy to take either one.

Brings the packaging work from @bybrave/json5 upstream (json5#348): ES module
sources with an exports map so require() and import resolve to the same
shape (json5#240), a single esbuild build step, bundled type declarations,
node:test instead of tap, and GitHub Actions CI. Requires Node.js >= 18.
On Node.js 22.12+ both import and require() now load lib/index.js
directly; older 22.x and bundlers keep using the import/require
conditions. The string export name needs es2022, so the browser
bundles target that (esbuild 0.28).
lib/ only has named exports. The build adds the default export and the
'module.exports' interop export to dist/index.mjs (module-sync/import),
keeps the default export in dist/index.cjs for transpiled default imports,
and the browser global no longer needs a footer.
…ge.json

dist/index.cjs is built from a CommonJS entry, so require('json5') is a
plain {parse, stringify} object without an __esModule marker. The build
removes dist/ first. The CLI and its test import package.json with
import attributes instead of createRequire.
@jordanbtucker
jordanbtucker merged commit 73fdf86 into json5:main Oct 15, 2026
3 checks passed
@jordanbtucker jordanbtucker self-assigned this Oct 15, 2026
@jordanbtucker

jordanbtucker commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

@bybraveHQ This is merged, so main is now v3. Thanks for the work and for turning the review around so quickly.

On your offer: another contributor is taking linting and formatting, and the TypeScript port is on hold until some parser performance work lands, so it doesn't conflict.

I'd welcome the follow-ups you mentioned in #348 as separate PRs against main: lone-surrogate escaping (#192) and opt-in BigInt parsing (#292). For #292, I'm planning a v3 change that makes stringify throw a TypeError on BigInts to match JSON.stringify, but we can chat about that there (#372).

I also plan on dropping json5/register (and its aliases) in v3.

@fisker Thanks for your input and reviews as well.

@bybraveHQ

Copy link
Copy Markdown
Collaborator Author

Thanks for the merge and for driving v3 forward. I'll open the two follow-ups as separate PRs against main: lone-surrogate escaping for #192 first, then opt-in BigInt parsing for #292 once #372 lands, and I'll keep the stringify discussion there. Dropping json5/register in v3 sounds right to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

migrate to ESModules ESM module export differs from main export

3 participants