justkey007 / tsc-alias

Replace alias paths with relative paths after typescript compilation
MIT License
876 stars 63 forks source link

fix: replacer got an invalid path if tsconfig.json is out of package dir #200

Closed MisterLuffy closed 11 months ago

MisterLuffy commented 11 months ago

My problem

I met a problem when use this tool. Here is my tsconfig.json:

{
  "compilerOptions": {
      "outDir": "es",
      "baseUrl": ".",
      "paths": {
        "my-list/utils/*": ["/some-absolute-path/example/src/utils/*"]
      }
    },
  "include": ["src/**/*.ts", "src/**/*.tsx"]
}

This tsconfig.json is out of my package folder, it is in the node_module/cache folder. When I use tsc-alias to transform the alias configured by tsconfig.json, I got this exception:

image

Solution

This line shows that you judge if it's an alias references a file outside the baseUrl via aliasPath.path.includes('..').However this line shows that aliasPath.path is a path relative to configDir (not baseUrl). So I changed it to a path relative baseUrl.

And I try to run jest after my change, but got an error no tsc-alias command founded. It seems to be a pnpm problem in my environment:

❯ pnpm link -g
 WARN  link:/Users/helium/Desktop/tsc-alias has no binaries

So if there is something wrong with Jest result, please notify me. Thanks for your review.