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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/bundle-generator.ts
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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.

return !getModuleInfo(moduleFileName, criteria).isExternal;
},
},
Expand Down Expand Up @@ -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)) {
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)

return fixPath(filePath);
}
}

return `node_modules/${moduleName}/`;
}

function addTypesReference(library: string, typesReferences: Set<string>): void {
Expand Down
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { returnMyType } from "field/type";

export function test() {
return returnMyType()
}
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 {};
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": "./src"
}
}