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

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

Open
NinjaInShade opened this issue Apr 30, 2023 · 1 comment
Open

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

NinjaInShade opened this issue Apr 30, 2023 · 1 comment

Comments

@NinjaInShade
Copy link

NinjaInShade commented Apr 30, 2023

Doesn't transform alias if:

  • the file you are aliasing is in the same folder
  • you only compile that one folder

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
Copy link

saga-dash commented May 8, 2023

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用に新たに作成する事で解決しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants