If you closely follow any of my open-source work, you might find me as this rigid individual who always creates libraries for Node.js and ignores other JavaScript runtimes.
A recent example is VineJS. Everyone asked me to explain.
- Why can VineJS not run in the browser?
- Why can it not run with Deno or Bun?
- Why can it not be used with Cloudflare workers?
- And reduce the bundle size to make Lamba cold starts quicker.
Making a library work in all these environments has its own set of requirements.
- Universally, your code should not use platform-specific dependencies. If you want to run the same code in Node.js and Cloudflare workers, you should not have imports from Node.js core and vice-versa.
- To keep browser bundles small, the library should be tree-shakable.
- Bare imports from
node_modulesdo not work in Deno. They have experimental support for npm packages, but I have not used them much. - Recently discovered, you cannot use
new Functionin Cloudflare workers. You might not neednew Functionthat often, but you must be aware that runtimes can also reduce the language's feature set.
You can make it work
If you believe in the slogan "Run JavaScript everywhere", you might be keen to suggest that it is 100% possible to produce runtime-agnostic JavaScript with careful development.
One way is to create a slim runtime agnostic core and then have platform-specific implementations. Cloudflare users can import library/cloudflare, Node.js users can import from library/node, and so on.
While it is certainly possible to follow this advice, the amount of time and energy you will spend creating a platform-agnostic codebase can be spent elsewhere. Well, in improving "What JavaScript can do,".
Improving what JavaScript can do
JavaScript frameworks in their current state are like Icon sets, everyone is creating them, yet you cannot use an Icon set JavaScript framework alone to build a product. But still, every one of them has become popular.
While the mainstream ecosystem is busy making their frameworks small, agnostic, run everywhere, bring your tools (but deploy with us). I am trying to stick to a single platform and improve "What JavaScript can do".
Talking about frameworks, Rails, Laravel, and Django are miles ahead of what JavaScript frameworks can do. It does not have to do with talent or bright minds. It is all about the focus. They are focusing on improving what their frameworks can do, and we are busy fighting to build tools or adding support for a new platform.