dts-bundle-generator icon indicating copy to clipboard operation
dts-bundle-generator copied to clipboard

Improve support of non-relative imports in projects with a baseUrl

Open josh- opened this issue 3 years ago • 2 comments

Thanks for all your work on this great project @timocov.

I noticed a small issue when using non-relative imports in TypeScript projects with a baseUrl configured. It appears that resolveModuleFileName treats all modules that isn't prefixed with a . as an external node_module, which causes needStripImportFromImportTypeNode to not strip these generated import() statements.

For example, prior to this change, the result of the import-from-non-relative-path-inferred-type test would be:

export interface MyType {
	field: string;
}
export declare function test(): import("field/type").MyType;

export {};

The import() here is unnecessary since MyType is already defined in the declaration file, and should be stripped.

This PR currently resolves the issue described above by, when a baseUrl is defined, checking whether a module exists relative to that baseUrl, before falling back to node_modules/${moduleName}/ if the module does not exist at that path.

josh- avatar Aug 29 '22 13:08 josh-

@josh- It seems that it is turning into something bigger than just one simple change (in my case it is more than 300 lines already and I still need to make some changes). I'll take it over and finish the fix. Thanks for you contribution! Let's keep this PR open for now (I'll use your commits and make changes on top of).

timocov avatar Sep 04 '22 19:09 timocov

@josh- It seems that it is turning into something bigger than just one simple change (in my case it is more than 300 lines already and I still need to make some changes). I'll take it over and finish the fix. Thanks for you contribution! Let's keep this PR open for now (I'll use your commits and make changes on top of).

No worries at all, that sounds great!

josh- avatar Sep 05 '22 00:09 josh-

The issue will be fixed in #224

timocov avatar Oct 02 '22 21:10 timocov

The fix has been released in v7.0.0.

timocov avatar Oct 07 '22 22:10 timocov