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

Compiler API to resolve path from ImportDeclaration #28276

Open
longlho opened this issue Nov 1, 2018 · 3 comments
Open

Compiler API to resolve path from ImportDeclaration #28276

longlho opened this issue Nov 1, 2018 · 3 comments
Labels
API Relates to the public API for TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@longlho
Copy link
Contributor

longlho commented Nov 1, 2018

Hi there, since TS supports paths in tsconfig.json, is there an API in the compiler (Program, CompilerHost or something) to programmatically resolve real path of an import?

I wrote this transformer https://github.com/longlho/ts-transform-css-modules and it's tripping up on paths that are aliased in tsconfig.json, e.g:

import * as css from 'alias/foo.css'

tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "alias/*": "../../../some_location/*"
    }
  }
}

I can try to manually reconstruct TS resolver but rather not to.

Thanks!

@ajafff
Copy link
Contributor

ajafff commented Nov 1, 2018

ts.resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference): ResolvedModuleWithFailedLookupLocations;

For your use case you can ignore the last parameter redirectedReference.
The cache if needed can be created using

ts.createModuleResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): ModuleResolutionCache;

@longlho
Copy link
Contributor Author

longlho commented Nov 1, 2018

Thanks a lot for the help! I gave that a try but unfortunately .css is not a valid TS module extension & looks like failedLookupLocations is internal so I can work off of the result :(

@HoldYourWaffle
Copy link
Contributor

Now that the original question has been answered, wouldn't this be a duplicate of #28770?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants