ES Modules version #1
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
2025 Update
I've added the initialization to
jswt
2024 Update
FULL ESM support has finally landed in Node in 2024 (and Browsers in 2023), but we won't switch this package back to ESM until it's been in LTS for a while.
Original
There's an
esmodule
branch which has this an as ES Module using import, but there is literally NO benefit to using this in that way.It works with other ES Modules as-is.
Transforming it to an ES Module itself just breaks backwards compatibility with 99.999% of the npm ecosystem that uses Common JS.
The Bottom Line
As of September 2024, now it's time.
--experimental-require-module
HOWEVER, that's for new packages. Existing packages (like
@root/walk
) should wait until Node support has hit LTS before making the switch - probably when Node v25 or v27 comes out - otherwise things downstream will just break.h(this also makes
basetag
obsolete for relative paths, see also https://github.com/janniks/basetag/issues/31)3 Module Maps
Slides: https://beyondcodebootcamp.github.io/presos/js-for-go-devs/#8
It's a little annoying, but you do have to specify your module maps 3 different times in 3 slightly different (but strictly particular) formats:
JavaScript / WHATWG
app.js
:Node / package.json
passkey.test.js
:tsc / jsconfig.json
jsconfig.json
:Note: this is the same format as
tsconfig.json
.Original
I'm revisiting the idea of switching to ESM
Now that ESM has finally achieved "baseline" browser support (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap), and there's been some time for people to update their browsers and phones, there could be real benefit to switching.
I came back here in hopes of having more of an explanation. I'm disappointed in my past self for being so angsty and vague.
Will go figure it out again now and report back...
Update
Browser Support (Baseline since March 2023)
Browsers have had partial support for years, but full support is finally "Baseline" (meaning "ubiquitous", "ALL Evergreen Browsers").
See:
Node Support (Experimental since ~March of 2024)
As of v22 it's still behind the
--experimental-require-module
feature flag, but I'm going to bet on it being included.It's really important to understand that this went from years of
wontfix
to nowexperimental
- this means that the hangup was more political than technical. With those winds having changed, it is extremely unlikely to be killed off at this stage.The big caveat is that modules MUST NOT use a
top-level
await - those must be couched in anasync function init() {}
orasync function main() {}
, or some caching mechanism that does whatever needs to be done on first use.See:
Tedium
The tedious part is that there are 3 similar-but-different package/dependency formats:
importmap
,package.json
, andtsconfig.json
.I'm not sure that the tooling to manage all 3 simultaneously exists - especially since this is sidestepped in most existing environments by bundlers with bespoke methods - but it's certainly doable by hand, and the tooling is fairly trivial (just get your trailing
/
s and*s
in the right place).Recommendation
It's time to switch to ESM for libraries with the constraint of a single default export and not using top-level
await
. Typically only apps need top-level await - not libraries - so this shouldn't be a big hangup.Rationalé
Having "Baseline" browser support for over 18 months means that almost everyone should have had a power outage, computer crash, or forced update to get support by now. No doubt there are some very old Androids and iPhones that will never be updated, but they limit to 0 as time goes on.
I see "Experimental" Node support for this as a signal that they're exploring edge cases, and too much of a "no-brainer" (much like fetch) to get dropped. Bun will follow Node, and I don't think Deno supports top-level require anyway.
By definition, early adopters typically aren't on a super old Android phone, a version an old version of node.
As whatever you're building today reaches the early majority, majority, and late majority, the unsupported devices, frameworks, etc, become vanishingly few.
Time to do it.