News for hackers. Stuff that matters.
Re:Bun v1.0.0 by dfabulich on Friday September 08, 2023 @01:49PM (#37436923)
How does Bun support CJS and ESM in the same file? Is it transpiling CJS to ESM? Or is it transpiling ESM to CJS?

ESM can use top-level await and CJS can't. So this can't possibly work, right?

    // x.mjs
    export const x = await 'x';

    // y.cjs
    console.log(require('./x.mjs').x);
And, indeed, it doesn't work in Bun. When I `bun y.cjs` it gives me this error message:

   TypeError: require() async module "/tmp/x.mjs" is unsupported. use "await import()" instead.
… but using "await import()" instead is exactly what I was trying to avoid! I can use "await import()" in Node, too.

But it looks like it does work in Bun if I drop the `await` from y.mjs. So, is it just transpiling ESM to CJS, and failing out if the ESM uses top-level await?

EDIT: I see the documentation page https://bun.sh/docs/runtime/modules has a "low-level details" section, explaining how import(CJS) works (by transpiling CJS to ESM), but it doesn't say anything there about how require(ESM) works, which is unfortunate, because that's by far the most interesting part! I've filed a doc bug on this. https://github.com/oven-sh/bun/issues/4601

Re:Bun v1.0.0 by ayewo on Friday September 08, 2023 @03:36PM (#37438370)
I believe Jarred has answered this upthread: https://news.ycombinator.com/item?id=37437848
Re:Bun v1.0.0 by flagged24 on Saturday September 09, 2023 @07:39AM (#37444237)
By dropping a major feature of ESM: top-level await.