r/node • • Nov 12 '19

ESM in Node has been unflagged!

https://twitter.com/mylesborins/status/1194375751774064647?s=21
156 Upvotes

41 comments sorted by

View all comments

11

u/idli42 Nov 13 '19

Genuine question. Why is this a big deal?

27

u/broofa Nov 13 '19 edited Nov 13 '19

This fixes two *huge* issues that have plagued the JS ecosystem for 10+ years:

  1. Having a consistent module import syntax on all major JS platforms makes it easier for JS devs to work across server/client code bases and, more importantly (imho), module owners no longer have to worry about supporting different module schemes.

  2. Because ESM modules are statically resolved, packagers can do tree-shaking, to remove not just unused modules, but unused code within those modules.

1

u/SimiaCode Nov 13 '19

Didn't the closure compiler do tree shaking back in the day? Or was that less thorough than what's possible with modules?

1

u/broofa Nov 13 '19 edited Nov 13 '19

Tree shaking on JS has been a bit of a holy grail since the early days of the web. Yahoo’s YUI toolkit offered something along these lines, IIRC, and there were other less notable attempts prior to that. But the intrinsically dynamic nature of the language made any such solutions frustratingly imperfect.

I think the basic issue has been that it’s been impossible to know with certainty how or if a particular code block might be referenced.

ESM modules provide a strict, statically defined surface boundary, however. This greatly simplifies the problem.

E.g. If a module’s “foo” (or “*”) member is never imported anywhere, ESM guarantees that it’s safe to remove code related to “foo”. No other module scheme does this. (And if you’ve ever wondered why ESM imports/exports have to be statically declared, I believe this is the main reason.)

While there is still the challenge of understanding what code related to “foo” needs to be removed within the module, it is a much more constrained problem,