How to publish a module, maintain support for CommonJS, and test with jest
The state of JavaScript is gross. We have native support for modules in node.js 13.2, but it's painful to use it with our existing tools, and non-trivial to publish packages that have backward compatbility with CommonJS.
That said, if you can stand the pain, there are a myriad of ways to do it, all of which are disgusting. This is one such way.
Babel is required to compile the CommonJS version of the module that will be used in situations where module support is not present.
This indicates what file to use when esm is import
is used.
This indicates what file to use when CJS require()
is used.
This syntax configures the experimental conditional exports support in Node.js, effectively doing the same thing as "main": "..."
amd "module": "..."
.
This tells node that the code contained in this repo is a module, and should use the import
and export
syntax.