> Which then makes people believe that treeshaking is not possible with CommonJS modules. Well, it is - Rollup just chose not to support it.
It is possible with CommonJS but it’s enormously brittle and unintuitive. I can write:
module.exports = { blah: 1 }
module.exports.blah = 1
let name = “blah”;
module.exports[name] = 1
And countless other possibilities. Then on the other side: const { blah } = require(“module”)
const blah = require(“module”).blah;
const mod = require(“blah”);
mod.blah;
let key = value ? “blah” : “something else”;
mod[blah];
Footguns all over the place that could easily ruin your treeshaking and result in unnecessarily large bundles.> And then people go "well you can statically analyze it better!", apparently not realizing that ESM doesn't actually change any of the JS semantics
Also plainly untrue. Similar to the above, I can do:
let name = “module”;
const mod = require(name);
With even a slight bit of added complication in that logic static analysis becomes enormously difficult. ESM does not let you do any of that.> loading a full dependency tree over the network would be unreasonably and unavoidably slow
Allow me to introduce you to HTTP2. It’s also fast to load dependencies because ESM is statically analysable in a way CommonJS isn’t, so you don’t actually have to evaluate the JS. No, it won’t be as fast as one request but the ability to just write a file and immediately upload it is tremendously liberating, IMO. For small projects Webpack, Vite or whatever is tremendous overkill.
> There are no actual advantages to it. At all.
Igorance is bliss, I guess.