Repository navigation
ESM-first sources with an exports map, esbuild build, bundled types (v3) - #370
Conversation
e774622 to
18be0e6
Compare
|
@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. |
|
@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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the thorough review, Jordan. Everything is addressed in c1db19e. CommonJS now has its own I also tried the packed tarball in consumer projects. With node16 and nodenext, both Agreed on handling linting in a separate PR and on porting |
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.
c1db19e to
9c92599
Compare
|
@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 I also plan on dropping @fisker Thanks for your input and reviews as well. |
Brings the packaging work from
@bybrave/json5back 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.jsongets"type": "module"and anexportsmap. The CommonJS entry (dist/index.cjs) is generated from the sources, sorequire('json5')andimport {parse, stringify} from 'json5'resolve to the same shape.build/build.js) producing the CJS entry, a minified ESM bundle and the minified browser global.build/unicode.jsandbuild/package.jsare kept and converted to ESM.node:test; the suite is ported 1:1 (178 tests).test/type-declarations.tscompiles the bundled types withtsc --strict.lib/index.d.tsmatching both entries, including thestringify(value, options)overload.Breaking changes (this is a major)
exportsmap tojson5/register(plus the legacyjson5/lib/registerandjson5/requirealiases, kept so existing configs keep working).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(regenerateslib/unicode.jsbyte-identical),npm run build-packagenpm packinstalled into a clean project:require('json5'),import JSON5, {parse, stringify} from 'json5',require('json5/register'),require('json5/lib/register'), the CLI,dist/index.min.mjsand thewindow.JSON5globalhttps://claude.ai/code/session_01SQ7pqqZUEYY57CkSL3wzHN