Skip to content

Add a helper function to resolve module import text to a file #1793

Description

currently resolving a context for the language service requires calling ts.preProcessFile on all files in the context, and following references. resolving triple-slash references is simple, resolving modules is a bit more complicated, as it involves searching in containing directories until a matching .ts file is found.

As a work around, here is the resolution logic:

function resolveImport(moduleName: string, souceFilename: string, fileExists: (filename: string) => boolean): string {
        var searchPath = ts.getDirectoryPath(souceFilename);

        while (true) {
            var filename = ts.normalizePath(ts.combinePaths(searchPath, moduleName));
            if (fileExists(filename + ".ts")) return filename + ".ts";
            if (fileExists(filename + ".d.ts")) return filename + ".d.ts";

            var parentPath = ts.getDirectoryPath(searchPath);
            if (parentPath === searchPath) break;

            searchPath = parentPath;
        }

        return undefined;
    }

Activity

  1. mhegazy commented on Feb 3, 2015

    @mhegazy
    ContributorAuthor

    This should be handled by #1735

  2. spadgos commented on Apr 25, 2017

    @spadgos

    The wiki links to this page.

    Something that is currently missing, and should be added in next release, is a built-in way to resolve imports. ts.preProcessFile returns import text, and the caller needs to resolve that to a .ts file path. Issue #1793 tracks fixing it.

    Should that be updated?

  3. mhegazy commented on Apr 27, 2017

    @mhegazy
    ContributorAuthor

    Should that be updated?

    i think so.

  4. vivin commented on Sep 19, 2017

    @vivin

    Mohamed Hegazy (@mhegazy) Is there updated documentation that shows how this can be done? I'm looking to implement some custom module-resolution logic. I have it working with tsc, but I'd like the IDE to be able to resolve these modules as well without them showing up as errors.

  5. mhegazy commented on Sep 19, 2017

    @mhegazy
    ContributorAuthor

    Mohamed Hegazy (@mhegazy) Is there updated documentation that shows how this can be done? I'm looking to implement some custom module-resolution logic. I have it working with tsc, but I'd like the IDE to be able to resolve these modules as well without them showing up as errors.

    IDE's use tsserver. the only way you can change the behavior there is if you create a language service plugin. you can find more docs about LS plugins in https://github.com/Microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin

  6. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptBy DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions