Example code how to publish ESM and CJS backward compatible package
ts-esm
loader for javascript es module and typescript
npm install ts-esm --dev
./packages/my-lib-example
- this is the hybrid backward compatibility, with conditional exports
yarn build.lib
to create package for hybrid package- output structure
./dist/my-lib-example/ cjs/ my-lib.js package.json my-lib.d.ts my-lib.js package.json
./dist/my-lib-example/cjs/package.json
- this will indicate that
.js
file iscommonjs
file - all the
.js
file with the same directory ofpackage.json
would treat ascommonjs
{ "type": "commonjs" }
- this will indicate that
./dist/my-lib-example/package.json
main
entry point for commonjs, this property can be overriden byexports
exports
property hasrequire
for commonjs entry pointimport
for es module entry pointmodule
is for bundler, ifmodule
is not present bundler getsmain
entry pointtype
property indicates that all.js
file treat ases
module{ "name": "my-package-example", "version": "0.0.1", "type": "module", "main": "./my-lib.js", "module": "./my-lib.js", "exports": { "require": "./cjs/my-lib.js", "import": "./my-lib.js" }, "typings": "./my-lib.d.ts" }
- output structure