Replies: 2 comments 2 replies
-
A slightly different approach that could solve the local dev-ex issue would be if esbuild had a new output format specifically for CJS+ESM compatibility. i.e. the idea is to output a CJS build, but rather than having it This would really just be a stopgap until Node has a mechanism for re- |
Beta Was this translation helpful? Give feedback.
-
#4841 is now merged, which will provide new extra config options ( |
Beta Was this translation helpful? Give feedback.
-
As libraries increasingly become ESM-only and other ones do a better job of supporting ESM (e.g. React@18, see here), it would be nice to have an official ESM build option for node-based deployments, e.g. something like
serverBuildTarget: "node"
(to eventually supplant the"node-cjs"
default). This will unlock faster, smaller builds, as the only thing bundled will be the application code; all dependencies (CJS or ESM) will be external andimport
ed natively.Currently you can use the deprecated
serverModuleFormat: "esm"
setting (e.g. see here) to get a pure ESM build with no bundled dependencies, but it would be nice to have an official/un-deprecated option.I've got a branch where I've been playing with this, and it works just fine, e.g. here are some integration tests where we bundle for ESM and it works great despite many dependencies being implemented in CJS (i.e. Remix, React, cjs-only-pkg, etc.).
As far as I can tell, the biggest hurdle is actually around the local dev experience for stacks using
remix serve
, so we'd need to solve that before making it official.remix serve
usesrequire
and resets therequire.cache
in order to reload the server bundle on each request. If we switch it toimport
(which works for both ESM and CJS), there is no mechanism to re-import, so currently you'd have to restart the server to see your changes.Some ideas:
await import("build/index.mjs?yolo="+Math.random())
. Works, but the big downside is this leaks, as modules never get GCd.vm.SourceTextModule
. This works, but it also leaks, albeit at about half the rate of the query string approach.getLoadContext()
) gets problematic.What do y'all think of the above? And are there other any ESM gotchas I'm missing? Happy to polish up my branch and create a PR once we address any open questions.
Beta Was this translation helpful? Give feedback.
All reactions