justkey007 / tsc-alias

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

doesn't replace alias if importing from same directory #177

Open NinjaInShade opened 1 year ago

NinjaInShade commented 1 year ago

Doesn't transform alias if:

Let's say you have a directory structure like:

/src
-> /folder_A
     -> /file_A.ts
     -> /file_B.ts

You also have an alias defined so that ~/ is resolved to src/*. File B exports some constant File A imports that constant via ~/folder_A/file_b

If you then run tsc & tsc-alias and the include in your tsconfig only include/s a file/s from only that folder_A, it doesn't transform the path correctly. Here's my debug output:

tsc-alias debug: default replacer - requiredModule:  '~/server/test2'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '~/',
  paths: [
    {
      path: 'src/',
      basePath: 'C:\\Users\\leonm\\Desktop\\leon\\dev\\ninja\\docs\\dist\\node',
      isExtra: false
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '---C:\\Users\\leonm\\Desktop\\leon\\dev\\ninja\\docs\\dist\\node'
tsc-alias debug: default replacer - Invalid path
tsc-alias debug: base-url replacer - requiredModule:  '@ninjalib/core'
tsc-alias debug: base-url replacer - requiredModule:  '~/server/test2'
tsc-alias debug: replaced file without changes: 'C:/Users/leonm/Desktop/leon/dev/ninja/docs/dist/node/server.js'
tsc-alias debug: replaced file without changes: 'C:/Users/leonm/Desktop/leon/dev/ninja/docs/dist/node/test2.js'
tsc-alias debug: replaced file without changes: 'C:/Users/leonm/Desktop/leon/dev/ninja/docs/dist/node/test2.d.ts'
tsc-alias info: 0 files were affected!

server.ts in this case is importing a constant from test2.ts and I am only compiling those files. It seems to pick up on the import alias, but not actually transform it (see the last line).

Now, this obviously isn't a big deal since A) you should just use a relative import if the file is in the same directory and B) normally you will be compiling more than one directory, but it would be nice to see it get fixed

saga-dash commented 1 year ago

sorry. compilerOptions.outDir does not need to be ., but it needed to be executed with tsc && tsc-alias.

~If you define a paths with isExtra: false, it seems to cause an error if the compilerOptions.outDir in tsconfig.json is anything other than .~ ~In my case, I wanted the outDir in tsconfig.json to be . /dist, so I solved this problem by creating a new one for build.~

Before translation `isExtra: false` となるpathsを定義した場合、tsconfig.jsonのcompilerOptions.outDirが `.` 以外だと不正になるようです 私の場合tsconfig.jsonのoutDirは `./dist` にしたかったので、build用に新たに作成する事で解決しました。