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.
It doesn't take much to support CJS, and it really helps the community with large applications and projects that continue to use this package.
Explanation
I'm not going to put my opinions on ESM vs CJS here since I don't want to start a flame war, but this change should make both parties happy, by supporting both:
superjson
can still be written in ESM and ship ESM, buttsc
will transpile it to CJS for the CJS consumers.If the package is
require()
'd, then it will use thedist-cjs/*
files, and if it'simport
'd, then it will usedist/index.js
.Testing
superjson
locallyyarn link
insidesuperjson
's directory"type": "module"
, in this directory runyarn add superjson && yarn link superjson
index.js
withimport S from 'superjson'; console.log(S);
and run it"type": "commonjs"
, in this directory runyarn add superjson && yarn link superjson
index.js
withconsole.log(require('superjson'));
, and run itMaintenance burden
In terms of burden for
superjson
maintainers, really it's having an extra 2 files:tsconfig.cjs.json
andcommonjs.js
. Both of these files together add up to 33 lines of code - it's not a lot. Since thesuperjson
package uses ESM, we can confidently just patch therequire
calls in thetsc
output, since ESM imports are more strict than CJS ones, etc.Final notes
Ultimately this is up to the
superjson
maintainers to accept or not, but I've at least proposed it as a starting point. Up to the maintainers to decide if they want to do it this way to support all their consumers or just a subset...Fixes #268
Fixes #299