-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Closed
josh-
wants to merge
1
commit into
timocov:master
from
josh-:fix-module-resolution-with-non-relative-imports
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as ts from 'typescript'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
import { compileDts } from './compile-dts'; | ||
import { TypesUsageEvaluator } from './types-usage-evaluator'; | ||
|
@@ -138,7 +139,9 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options: | |
const { program, rootFilesRemapping } = compileDts(entries.map((entry: EntryPointConfig) => entry.filePath), options.preferredConfigPath, options.followSymlinks); | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
const typeRoots = ts.getEffectiveTypeRoots(program.getCompilerOptions(), {}); | ||
const compilerOptions = program.getCompilerOptions(); | ||
const typeRoots = ts.getEffectiveTypeRoots(compilerOptions, {}); | ||
const baseUrl = compilerOptions.baseUrl; | ||
|
||
const sourceFiles = program.getSourceFiles().filter((file: ts.SourceFile) => { | ||
return !program.isSourceFileDefaultLibrary(file); | ||
|
@@ -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); | ||
return !getModuleInfo(moduleFileName, criteria).isExternal; | ||
}, | ||
}, | ||
|
@@ -582,8 +585,21 @@ function updateResultForModuleDeclaration(moduleDecl: ts.ModuleDeclaration, para | |
); | ||
} | ||
|
||
function resolveModuleFileName(currentFileName: string, moduleName: string): string { | ||
return moduleName.startsWith('.') ? fixPath(path.join(currentFileName, '..', moduleName)) : `node_modules/${moduleName}/`; | ||
function resolveModuleFileName(currentFileName: string, moduleName: string, baseUrl?: string): string { | ||
if (moduleName.startsWith('.')) { | ||
return fixPath(path.join(currentFileName, '..', moduleName)); | ||
} | ||
|
||
// determine if the module is a non-relative import that can be resolved with the baseUrl | ||
if (baseUrl !== undefined) { | ||
const filePath = `${path.join(baseUrl, moduleName)}.ts`; | ||
timocov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (fs.existsSync(filePath)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return fixPath(filePath); | ||
} | ||
} | ||
|
||
return `node_modules/${moduleName}/`; | ||
} | ||
|
||
function addTypesReference(library: string, typesReferences: Set<string>): void { | ||
|
5 changes: 5 additions & 0 deletions
5
tests/e2e/test-cases/import-from-non-relative-path-inferred-type/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { TestCaseConfig } from '../../test-cases/test-case-config'; | ||
|
||
const config: TestCaseConfig = {}; | ||
|
||
export = config; |
5 changes: 5 additions & 0 deletions
5
tests/e2e/test-cases/import-from-non-relative-path-inferred-type/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { returnMyType } from "field/type"; | ||
|
||
export function test() { | ||
return returnMyType() | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/e2e/test-cases/import-from-non-relative-path-inferred-type/output.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface MyType { | ||
field: string; | ||
} | ||
export declare function test(): MyType; | ||
|
||
export {}; |
9 changes: 9 additions & 0 deletions
9
tests/e2e/test-cases/import-from-non-relative-path-inferred-type/src/field/type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface MyType { | ||
field: string; | ||
} | ||
|
||
export function returnMyType(): MyType { | ||
return { | ||
field: "test" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/e2e/test-cases/import-from-non-relative-path-inferred-type/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./src" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.