Open tmkx opened 6 months ago
When I use tsup to build ESM I find myself grabbing for workarounds to even load (let alone run) the built ESM code, such as https://github.com/barhun/extensionless
But as a lib maintainer I can't proxy this burden to my clients, and a better workaround for me would be to patch the esm output in a postprocess somehow. But ideally tsup esm code should "just work" and no workaround should be necessary, and this issue describes exactly the behaviour I am expecting from tsup to give me for free: Correct ESM output as per the spec.
In the meantime, take a look at this repo - it is a great workaround for this issue! 🎉
https://github.com/favware/esbuild-plugin-file-path-extensions?tab=readme-ov-file#description
It provides an esbuild plugin that adds .mjs
(or whatever you want) extension to relative imports in your built project. Note that you are supposed to use bundle: true
which is the only weird thing going on here, but like they mention it doesn't bundle it.
It might be helpful:
An example here: https://github.com/belgattitude/httpx/blob/69af14514d89b7660a6f3e5e0e4494b7f4809532/packages/exception/tsup.config.mjs#L19
i tend to use the same esbuild plugin mentioned by @klippx in conjunction with outExtension.
An example here:
Hope it helps, till mjs becomes default.
btw thx for tsup it’s really great
files:
config:
according to the esm spec, file extensions are mandatory
Upvote & Fund