CJS/ESM module exports #49
Replies: 2 comments 6 replies
-
Thanks for the kind words @matteosacchetto ! I'm stoked you found radash and your a fan 🍻 I like what you're suggesting! I know there are some libraries out there that used to be cjs and made the switch at a major version bump. Sometimes I want to use them but I can't (or I have to install an older version). I've hit this page A LOT. Any ideas on how to make a better experience if we do provide esm? Like, maybe publishing I should tell you, I've never even used an esm library. I should really do a deep dive myself. Feel free to correct me or teach me on the subject if I'm way off base. |
Beta Was this translation helpful? Give feedback.
-
Hi @rayepps, yeah, I totally understand the frustration of having to deal with library which are now only ESM and a codebase which is still using CJS code, it happened to me a lot, until I decided to go for implementing new projects using the ESM module syntax, and it has been great so far. One cool this is that in node you can Anyway, I believe that in order to do so you can follow two alternative paths Solution 1: create two different packages, one for the CJS export and one for the ESM export
Solution 2: define two entry points, one for CJS'
Regarding the second solution I leave here some references on how to do it:
Unfortunately I also have no experience in writing a library which provides both CJS and ESM exports, but I do know that threeshaking with ESM modules is something which you get out of the box, and especially for people developing frontend having smaller builds would be great No hurry on this, is still just an idea, would be awesome to see what others may think on this Thank again for you time and have a nice day! 😃 |
Beta Was this translation helpful? Give feedback.
-
I discovered this library quite recently, and I find it very interesting, since it provides a lot of useful functions while being rather lightweight. But I have a question/idea for you
What about providing two different export, one for CJS modules and one for ESM modules?
So, I am asking this more from a performance standpoint, and the reason is the following: by providing an ESM module export it could allow bundlers like Rollup, which take advantage of ES6 named exports, to threeshake the code and import only the functions which are actually used. It could provide smaller builds for those using Rollup or tools based on that, like Vite.
I personally believe that at that point it would get even better than it already is, allowing people to take advantage of this library while keeping the builds smaller.
I did a small test, using Vue with Vite and I added a small piece of code in the App.vue component:
I then tried cloning the repo and updating the tsconfig.json file to build for
esnext
, and copied the build directory within thesrc
directory and called itradash
. To use this build the only difference with respect to above was the import which became:In this test, the difference in build size of the Vue app between the two configurations was of about 20KiB
What do you think about this?
Thank you for your time and keep up the good work!! 💪🏼
Matteo
Beta Was this translation helpful? Give feedback.
All reactions