Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by Macha on Thursday August 06, 2020 @07:58AM
(#24070155)
It's not possible to write a nice automated transparent one for arbitrary libraries because of top level await, but it's totally possible for the 95%.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by jkrems on Thursday August 06, 2020 @01:15PM
(#24073075)
You cannot write a CJS shim for any library because the CJS shim cannot expose any ESM file with a synchronous API because importing ESM always returns a Promise.
You can - and people widely do - compile a CJS version of your library from ESM sources. You'll accept that there may be multiple copies of your library in the program but it does work. But in any case there's no reason to actually author in CJS, everything including the ESM shim can be generated from ESM.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by WorldMaker on Thursday August 06, 2020 @04:09PM
(#24074922)
It is possible, but you have to hack the Node require() loader to do it, teach it a lot about ESM loading, and then hope the downstream CJS don't have timing bugs around "very long" synchronous require() loads.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by jkrems on Thursday August 06, 2020 @08:31PM
(#24076823)
I'm not sure that's really relevant here because esm is just one of the transpilers for module syntax, it's not running native modules at all. The same can be achieved with the babel CLI or TSC etc. and the right configuration. But it's not really "ESM loading". It's compiling ESM to CJS and then running CJS. It's true that transpiling to CJS will continue to work, no matter what.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by WorldMaker on Friday August 07, 2020 @12:06PM
(#24083414)
To my understanding this ESM module uses native ESM loading on Node versions that support it, which is why others point out its behavior with top-level await is deficient in the same ways that Node's own behavior is, though it tries.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by desmap on Thursday August 06, 2020 @10:56AM
(#24071459)
Re your point OP also commented/quoted someone with “I don’t think designing a system with the blanket assumption that some feature just won’t get used is a viable path.”
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by Macha on Thursday August 06, 2020 @12:25PM
(#24072482)
Yes, someone cannot design a system to handle arbitrary libraries with the assumption.
A library author can provide cjs bindings to their library for as long as cjs remains relevant with the assumption that they won't use top level await.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by nightski on Thursday August 06, 2020 @11:43AM
(#24071956)
Yea they are looking at the ecosystem as a whole. But if you are a library author you have the power to control these things.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by inquisitiveio on Thursday August 06, 2020 @10:59AM
(#24071484)
I'm pretty sure services like pika.dev/skypack.dev/jspm.io are trying to solve the CJS to ESM problem so you can use it directly in the browser (or deno).
Last time I checked they seemed to lean into using rollup to take care of the translation under the covers.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by desmap on Thursday August 06, 2020 @11:02AM
(#24071524)
That's not the point here. It's about that a thin CJS wrapper to an ESM module isn't possible but the way around is.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by inquisitiveio on Thursday August 06, 2020 @12:06PM
(#24072256)
Sorry I misread your comment. But is it not easier to write an CJS wrapper around ESM given that its format is a lot looser?
From what I have seen seems pretty typical to simply export the named properties using a fresh module.exports = {...} and then use default as the main export?
OP wrote, note that it’s easy to write an ESM wrapper for CJS libraries, but it’s not possible to write a CJS wrapper for ESM libraries.