ethereumjs-monorepo
ethereumjs-monorepo copied to clipboard
Monorepo: Distribute modules with esm as well
since the packages are authored as typescript esm it should be easy to distribute as esm in addition to cjs
eg @ethereumjs/tx only provides a "main" which points to the cjs build https://npmfs.com/package/@ethereumjs/tx/3.3.0/package.json#L16
Some context on this, here is the Node.js ESM documentation.
Some articles on possible approaches:
- How to Create a Hybrid NPM Module for ESM and CommonJS, Feb 12 2021 (I like that one on a very first read)
- Hybrid npm packages (ESM and CommonJS), 2019-10-22 (a bit older)
Related issue: #978
What do you guys think, might be a good time to prioritize a bit on this? (@kumavis thanks for the suggestion)
Some more resources, more specifically for TypeScript:
https://github.com/gfmio/typescript-esm-cjs-hybrid-example
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#ecmascript-module-support-in-nodejs
I might be able to take this on, depending on how my experiments with the portal network module go. I'm using Rollup to package for esm and commonjs there though it's not seamless yet.😬
This should be delayed until node.js drops support for v14, which is 2023+. The constraint list:
It is not possible to use dynamic requires same code to be used in node.js and browser (NPM etc). I'm talking about this pattern:
export const crypto: { node?: any; web?: any } = (() => {
const webCrypto = typeof self === 'object' && 'crypto' in self ? self.crypto : undefined;
const nodeRequire = typeof module !== 'undefined' && typeof require === 'function';
return {
node: nodeRequire && !webCrypto ? require('crypto') : undefined,
web: webCrypto,
};
})();
Will be included in v7.