by alexghr · 3y ago · 0 comments
What we gained is a native module system that works the same everywhere (Nodejs, browsers, deno, Bun) vs CommonJS which was really only built for Nodejs (with compatibility layers for others).
We have a specification for this system and any changes done to it have to follow the same process as any other change done to the language (for better or worse).
We have top-level async support that works across import boundaries. This is less useful on the server, but in the browser? With just a `import thing from "https://example.com/foo.js"` I get a fully intialised library even if does async requests as part of its initialisation.
What we lost is just this
``` const someInitializedModule = require("module-name")(someOptions); ```
The `app.use()` example can be replicated with an async `import()`. Maybe it's not as elegant as before.
There's going to be a lot pain of transitioning a big ecosystem like Javascript's to ESM but is that really a reason to not evolve the language?
This is indeed convenient if you want to quickly try a lib. However, what happens in a larger project with dozens of dependencies? Those http requests will get inefficient soon, and you'll be back having to compile everything with Babel or similar.
Plenty of websites are just server-side rendered and just need to sprinkle some client side JS. For them it's perfect to be able to drop in a script and not have to worry about bundling or polluting the `window` object while loading external scripts.
You're also now fighting for response time and bandwidth from a public resource you don't control. You are beholden to their traffic spikes, their downtime and their security incidents.
Just send it from your servers, or your edge nodes. They already have the DNS hit cached, they are already getting everything else from there. Chances are high you're sending image data that far exceeds the JS library anyway. This is especially prudent if you serve users in your own country, and that country isn't the US. Chances are very high your site's largest response delays are US CDN resources if you use them.
No, it won't any more: https://www.stefanjudis.com/notes/say-goodbye-to-resource-ca...
Standardised functionality, in this case, is better than needing tooling.
>``` const someInitializedModule = require("module-name")(someOptions); ```
That can partially be done such as:
import {jason} from 'https://example.com/foo.js?name=jason
On the script site use: const url = new URL(import.meta.url); console.log(`${url.searchParams.get("name")`);
require("module-name")(someOptions)
is instead represented as import { someModule as someModuleFactory } from 'module-name';
const someInitializedModule = someModuleFactory(someOptions); (await import("module-name")).default(someOptions)
The biggest issue is import() is async and module proxies aren't allowed to be directly callable so you need to pick an export from the other module to call. But `default` is an appropriate export to call, even if there's no nice shorthand for default imports in the import() case (as there is syntax sugar in the import keyword case for default). Neither of those things seem like deal breakers to me, and have very good reasons for why they are the way they are.All of that said I'm super happy I moved away from mobile/frontend into backend. Way less annoyances.