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
Improve support of non-relative imports in projects with a baseUrl #219
Conversation
…aseUrl configured
There was a problem hiding this 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)) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
@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! |
The issue will be fixed in #224 |
The fix has been released in v7.0.0. |
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 thatresolveModuleFileName
treats all modules that isn't prefixed with a.
as an external node_module, which causesneedStripImportFromImportTypeNode
to not strip these generatedimport()
statements.For example, prior to this change, the result of the
import-from-non-relative-path-inferred-type
test would be:The
import()
here is unnecessary sinceMyType
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 tonode_modules/${moduleName}/
if the module does not exist at that path.