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

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

Conversation

josh-
Copy link
Contributor

@josh- josh- commented Aug 29, 2022

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.

Copy link
Owner

@timocov timocov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @josh-, sorry for the late reply. Thanks for contributing, please take a look the comments. It seems that it might be fixed in a more appropriate way

if (baseUrl !== undefined) {
const filePath = `${path.join(baseUrl, moduleName)}.ts`;

if (fs.existsSync(filePath)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is legit to be used here. Only the compiler might help/know whether a module exists or not and we have to rely on its API in this matter (see my another comment in needStripImportFromImportTypeNode function)

@@ -357,7 +360,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
}

// we don't need to specify exact file here since we need to figure out whether a file is external or internal one
const moduleFileName = resolveModuleFileName(rootSourceFile.fileName, node.argument.literal.text);
const moduleFileName = resolveModuleFileName(rootSourceFile.fileName, node.argument.literal.text, baseUrl);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use updateResultCommonParams.resolveReferencedModule instead, I think that function is designed to get the module file name correctly by using the compiler's API instead of dirty hacks with the path.

@timocov
Copy link
Owner

timocov commented Sep 4, 2022

@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 timocov added the Bug label Sep 4, 2022
@timocov timocov self-assigned this Sep 4, 2022
@josh-
Copy link
Contributor Author

josh- commented Sep 5, 2022

@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!

@timocov
Copy link
Owner

timocov commented Oct 2, 2022

The issue will be fixed in #224

@timocov timocov added this to the 7.0 milestone Oct 2, 2022
@timocov timocov closed this in #224 Oct 2, 2022
@timocov
Copy link
Owner

timocov commented Oct 7, 2022

The fix has been released in v7.0.0.

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

Successfully merging this pull request may close these issues.

None yet

2 participants