-
Notifications
You must be signed in to change notification settings - Fork 26.7k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
TypeScript imports with .js file extensions don't work #9358
Comments
This comment has been minimized.
This comment has been minimized.
Hey @justinfagnani! Thanks for letting us know about this issue. I wasn't aware TypeScript allowed for this behavior. What happens when a Are there any other toolkits that support this behavior out-of-the-box? We'd love to see a reference implementation. We'll explore the above questions if they're more nuanced when we get around to implementing this. Feel free to send a PR as well! |
@Timer ts prefers ts and will ignore the js file when both are present, you can confirm with tsc -w without an outDir specified and changing the contents of the .js file - it won't recompile. to get this working in next, i think https://github.com/tleunen/babel-plugin-module-resolver/blob/master/src/resolvePath.js has everything you need |
seems that next.js prefers .ts files in node_modules folder over .js files as well, in a library I'm writing in TS which I build all my typescript files to js in the same folder, next.js starts crawling the node_modules directory for types and throws an error because I have a babel plugin that processes SVG and typescript doesn't know what it is. |
any news on this? |
👍 ing the importance of this issue. Not sure how strictly Next.js uses the
As an aside, some devs may have settings in their IDEs that auto-add |
If anyone is looking for copy-paste workaround here it is:
const path = require("path");
const fs = require("fs");
module.exports = {
presets: ["next/babel"],
plugins: [
[
"module-resolver",
{
extensions: [".js", ".jsx", ".es", ".es6", ".mjs", "ts", "tsx"],
resolvePath(sourcePath, currentFile, opts) {
if (!sourcePath.startsWith("./") && !sourcePath.startsWith("../")) {
return sourcePath;
}
if (sourcePath.endsWith(".js")) {
const relPath = path.resolve(path.dirname(currentFile), sourcePath);
const tsPath = relPath.replace(/\.js$/, ".ts");
const tsxPath = relPath.replace(/\.js$/, ".tsx");
if (fs.existsSync(tsPath)) {
return tsPath;
}
if (fs.existsSync(tsxPath)) {
return tsxPath;
}
}
return sourcePath;
},
},
],
],
}; I'm using that for few months already without any issues. I'd love to have this working without a workaround though. @samfortunato gave some nice arguments why. |
@dzek69 Awesome, that did work. Was looking for a solution like this before. Unfortunately, it means Next.js will not use the new Rust compiler. Would have to look at the SWC docs to see if a workaround could be configured in the same way... |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Bug report
Describe the bug
When importing a TypeScript module from another, TypeScript allows the use of the
.js
file extension:foo.ts:
bar.ts
This is important in plain TypeScript projects because browsers don't do any module resolution and require the correct extensions. The compiled output works as it.
If you copy the same files into a Next.js project you will get a compile error that
foo.js
cannot be found.To Reproduce
See above
Expected behavior
No compile errors
System information
The text was updated successfully, but these errors were encountered: