rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.63k stars 588 forks source link

Can't import '.ts' file from '.tsx' file #1465

Closed felixcatto closed 1 year ago

felixcatto commented 1 year ago

Expected Behavior

When i import '.ts' file from '.tsx' file, and specify '.js' extension, it should be imported.

Actual Behavior

I get error

Additional Information

ES modules want that we add file extensions to all files https://nodejs.org/api/esm.html#mandatory-file-extensions But Typescript doesn't like '.ts', '.tsx', it want '.js', '.jsx', '.mjs', ...

2023-03-26_11-38

But if i import utils.js, when actual files have different extension i got following error

index.tsx

import { x } from './utils.js';

utils.ts

export const x = '';

2023-03-26_11-41

index.tsx  'import'  utils.ts    # error
index.ts   'import'  utils.tsx   # error
index.js   'import'  utils.tsx   # error
index.ts   'import'  utils.ts    # works
index.tsx  'import'  utils.tsx   # works

In webpack i can define resolveextensionalias https://webpack.js.org/configuration/resolve/#resolveextensionalias

In rollup nodeResolve extensions option seems combines webpack extensionalias & extensions options. But only partially. It only works if all extensions matches, i.e. all files have '.tsx'

meyfa commented 1 year ago

I think this part may be to blame:

https://github.com/rollup/plugins/blob/e1bb4bdbee0203367c06baa1bcb9844d598d040d/packages/node-resolve/src/index.js#L149-L156

The loop will check the following:

The intended functionality according to the comment would be to add both .ts and .tsx in both cases.

meyfa commented 1 year ago

@felixcatto node-resolve v15.1.0 was just released with my fix (see #1498). Could you verify that this solves your issue?

felixcatto commented 1 year ago

@meyfa Yes, it solves first two cases from this list

index.tsx  'import'  utils.ts    # error => works now
index.ts   'import'  utils.tsx   # error => works now
index.js   'import'  utils.tsx   # error
index.ts   'import'  utils.ts    # works
index.tsx  'import'  utils.tsx   # works

But following scenario doesn't work (I also updated a repo at first post)

index.js

import { x } from './utils.js';
console.log(x);

utils.ts

export const x = 111

2023-05-31_07-28

Of course it is rare case, when someone want to import .ts file from .js file. So thanks for fix :wink:

meyfa commented 1 year ago

Oh sorry, I totally overlooked that case! And it seems that this works with ts-node out of the box as well, so perhaps Rollup should support it?

I personally think it's a bit unusual to import TS from a JS file.

felixcatto commented 1 year ago

Initially i wanted to say "yes", but decided to look at Vite behaviour in this situation. And Vite also throws error, but it is quite popular bundler and seems people doesn't have such problems if it doesn't fixed yet. So i would say that it can be ignored.

2023-05-31_17-42

stale[bot] commented 1 year ago

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.