3 ms·
How a top level require statement with a string (which is the most common) not the same then? If we're ignoring edge cases, then why even bother with a new synt
by msoad 3y ago
How a top level require statement with a string (which is the most common) not the same then? If we're ignoring edge cases, then why even bother with a new syntax?
- afavour 3y ago> If we're ignoring edge cases ESM specifically doesn't ignore the edge case, it makes a special syntax for it! The point is that require() is surrounded by footguns. It's a magic function with untold side effects. That's bad. If an inexperienced developer sees this: const mod = require('module'); they might think it's fine to change it to: const moduleName = 'module'; const mod = require(moduleName); because why not? Or why not move it from the top level? There's nothing obvious about require() that would inform you you're about to cause a giant side effect. Indeed, on Node you wouldn't be. But you would be on a client-side build. This is the problem with non-standard implementations. Switching from a top-level import to using import() has a drastic effect on your code: import() is async, requires you to wait on a promise to resolve, etc. etc. It's obvious that making the switch alters the structure of your code and the way it loads.
- msoad 3y agoI am responding to your comment above: I've worked in this space and I've heard all of the argument > [require] is a magic function with untold side effects Sure, how about making it standard with a new directive? "use require" or something like that. Now require is special. You can't redefine it etc
- afavour 3y agoWell that would be a breaking change. Code is going to have to be changed. And if you’re introducing a breaking change why not try to fix all the problems? Even if require is standardised it’s still fundamentally weird. Let’s say we standardise around require only being used with static strings. Now we’re in a situation where every JavaScript function can be called with a variable as an argument except this special function called “require”, which can only receive strings. There’s no concept of that in the JS language. What we’re doing is creating a new concept. It makes sense, looking to the future, to create new syntax for it.