Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] add .mjs extension to esm output #1058

Open
tmkx opened this issue Dec 13, 2023 · 3 comments
Open

[Feature Request] add .mjs extension to esm output #1058

tmkx opened this issue Dec 13, 2023 · 3 comments

Comments

@tmkx
Copy link
Sponsor Contributor

tmkx commented Dec 13, 2023

files:

// entry.ts
import { a } from './file'

// file.ts
export const a = 123

config:

export default defineConfig({
  entry: ['./entry.ts', './file.ts'],
  format: 'esm',
  bundle: false,
})
current output expected output
// entry.mjs
import { a } from "./file";
console.log(a);


// file.mjs
const a = 123;
export { a };
// entry.mjs
import { a } from "./file.mjs";
console.log(a);


// file.mjs
const a = 123;
export { a };

according to the esm spec, file extensions are mandatory

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@klippx
Copy link

klippx commented Jan 9, 2024

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.

@klippx
Copy link

klippx commented Jan 9, 2024

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.

@belgattitude
Copy link
Contributor

belgattitude commented Jan 10, 2024

It might be helpful:

  1. Use the tsup outExtension config.

An example here: https://github.com/belgattitude/httpx/blob/69af14514d89b7660a6f3e5e0e4494b7f4809532/packages/exception/tsup.config.mjs#L19

  1. If bundling into separate files

i tend to use the same esbuild plugin mentioned by @klippx in conjunction with outExtension.

An example here:

https://github.com/belgattitude/httpx/blob/69af14514d89b7660a6f3e5e0e4494b7f4809532/packages/assert/tsup.config.mjs#L28

Hope it helps, till mjs becomes default.

btw thx for tsup it’s really great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants