LinkedSoftwareDependencies / Components-Generator.js

⚙️ Generate Components.js component files from TypeScript
13 stars 7 forks source link

Export of directories with an index.d.ts not handled correctly #99

Closed Maximvdw closed 2 years ago

Maximvdw commented 2 years ago

It seems that the components-generator does not handle exports of directories with an index.d.ts

Directory structure:

Example index.d.ts (root)

export * from './directory_1';
export * from './directory_2';

Running the generator will throw the following error:

[Components.js] info: Generating components for 1 package
ENOENT: no such file or directory, open '...\directory_1.d.ts

There is indeed no directory_1.d.ts, but there is a '..\directory_1\index.d.ts'

If I change the index.d.ts in the root to:

export * from './directory_1/index';
export * from './directory_2/index';

it will correctly generate the components

I might have a PR that I am testing that could fix this issue by modifying: https://github.com/LinkedSoftwareDependencies/Components-Generator.js/blob/master/lib/resolution/ResolutionContext.ts#L67-L69 to include a fallback. But I am not entirely sure it it would be the best place to fix it here, as the context of the relative paths within that index are incorrect (i.e. if you have an export * from './SomeFile' in ./directory_1/index.d.ts it will try to load ./SomeFile instead of ./directory_1/SomeFile

rubensworks commented 2 years ago

Thanks for the issue!

I might have a PR that I am testing that could fix this issue by modifying: https://github.com/LinkedSoftwareDependencies/Components-Generator.js/blob/master/lib/resolution/ResolutionContext.ts#L67-L69

That should indeed be a good place to fix the issue.

One solution might be to first check if filePath exists, and is a directory. If so, /index.d.ts could be appended to it, and it could be loaded. If not, .d.ts is appended, and loaded directly.

Maximvdw commented 2 years ago

One solution might be to first check if filePath exists, and is a directory. If so, /index.d.ts could be appended to it, and it could be loaded. If not, .d.ts is appended, and loaded directly.

Hi, thanks for the fast reply. The reason why I was not certain is due to the relative exports in ./directory_1/index.d.ts I added an example at the end of my original post of why/when it should fail

rubensworks commented 2 years ago

The reason why I was not certain is due to the relative exports in ./directory_1/index.d.ts I added an example at the end of my original post of why/when it should fail

Right, that may indeed cause issues. I suspect that some changes for that case might be needed in https://github.com/LinkedSoftwareDependencies/Components-Generator.js/blob/master/lib/parse/ClassLoader.ts#L157 or https://github.com/LinkedSoftwareDependencies/Components-Generator.js/blob/master/lib/parse/ClassLoader.ts#L534