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 extension to relative import id #1563

Closed
loynoir opened this issue Aug 30, 2021 · 6 comments
Closed

[Feature Request] Add extension to relative import id #1563

loynoir opened this issue Aug 30, 2021 · 6 comments

Comments

@loynoir
Copy link

loynoir commented Aug 30, 2021

Feature Request

Add extension .mjs to relative import id,

when specific --out-extension:.js=.mjs

Repeoduce

import deps from "./deps"
console.log(deps)
esbuild \
    index.ts \
    --out-extension:.js=.mjs \
    --outdir=dist

Actual

import deps from "./deps";
console.log(deps);
$ node dist/index.mjs
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'path/to/dist/deps' imported from path/to/dist/index.mjs

Expected

import deps from "./deps.mjs";
console.log(deps);
$ node dist/index.mjs
42
@hyrious
Copy link

hyrious commented Aug 31, 2021

tsc doesn't append .mjs too.
see #622

@evanw
Copy link
Owner

evanw commented Aug 31, 2021

Yes, TypeScript works this way by design, and therefore esbuild's TypeScript support also works this way. If you would like for Microsoft to change how TypeScript works you should ask them, not me. I don't work at Microsoft and don't have any control over their roadmap. I believe microsoft/TypeScript#27957 is a relevant issue to follow.

The workarounds for this situation are described here: #622 (comment). Either use .js and "type": "module" or write your own esbuild plugin to implement the behavior you want.

@loynoir
Copy link
Author

loynoir commented Aug 31, 2021

@evanw

As the bottleneck here is relative import , what do you think of #1564 ?

Allow CLI accessing some out-of-box external preset.

In this case, add an out-of-box preset mark all non-relative import id as external.

So, the output will has no relative import, both typescript and node will happy.

@evanw
Copy link
Owner

evanw commented Oct 13, 2021

TypeScript's upcoming version 4.5 now has a way to do this. If you have a TypeScript file that needs to import another TypeScript file that will have a .mjs extension when compiled, you should use the .mts file extension for the TypeScript file and you should use import "./deps.mjs" in your TypeScript code to import deps.mts. This will be the official way to do it going forward and doesn't need any special support from esbuild. You can read the announcement here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#new-file-extensions.

Closing this issue since this is being addressed outside of esbuild by the TypeScript team, and is possible to implement within esbuild via a plugin without needing to modify esbuild itself.

@evanw evanw closed this as completed Oct 13, 2021
@stefanvonderkrone
Copy link

if someone is interrested in my solution:
#622 (comment)

@shellscape
Copy link

shellscape commented Oct 14, 2023

I think it's completely reasonable for a compiler to add a file extension as a transformation. Compilers make all kinds of adjustments for other nuanced mechanics of intent. At the very least, a plugin should solve this. And for what it's worth, the TS team has been repeatedly chided by the community for the decision. Their decision making isn't exactly pure.

Edit: Boom. There's a plugin. https://github.com/favware/esbuild-plugin-file-path-extensions

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

5 participants