[ts-transform-paths] Original relative path should be preferred over mapped
ark120202 opened this issue · 8 comments
Reproduction:
// tsconfig.json
{
"compilerOptions": {
"baseDir": ".",
"paths": {
"*": ["modules/*"]
}
}
}
// /utils.ts
export const a = 1
// /foo/utils.ts
export const b = 2
// /foo/bar/file.ts
import { b } from '../utils';
console.log(b);
The compiled /foo/bar/file.ts
contains an import to ../../utils
(resolves to /utils.ts
), while types are resolved to /foo/utils.ts
Please, provide demo, i can't reproduce, check https://github.com/zerkalica/zerollup-test-18/tree/issue-24.
Your reproduction looks fine except that source files shouldn't be in the modules
directory, but in the project root
Your config looks wrong. No such option in tsconfig:
"baseDir": ".",
I put modules into src and add "baseUr"l: "src", all works fine: https://github.com/zerkalica/zerollup-test-18/tree/issue-24
Oh, sorry, named it incorrectly in the example.
All of the files listed here should be outside of modules
directory, i.e. file structure might look like
project/
├── tsconfig.json
└── src/
├── modules/ - could have any files, but it's not required for the reproduction
├── utils.ts
└── foo/
├── utils.ts
└── bar/
└── file.ts
file.ts
contains an import to ../utils
, which gets caught by "*": ["modules/*"]
mapping, creating a modules/../utils.ts
path candidate and then is transformed into ../../utils
Actually, looks like TypeScript just never maps relative paths at all. A simpler reproduction would be:
{
"compilerOptions": {
"outDir": "lib",
"baseUrl": "src",
"paths": {
"../*": ["modules/*"]
},
"plugins": [{ "transform": "@zerollup/ts-transform-paths" }]
}
}
// src/test.ts
import { foo } from '../foo';
console.log(foo)
// src/modules/foo.ts
export const foo = 1;
TypeScript gives a Cannot find module '../foo'.
diagnostic, but the transformer resolves it to "./modules/foo"
.
Check 1.7.10
Thanks, it solved the issue for me!
Though would it maybe be better to do something like oldImport.startsWith('./') || oldImport.startsWith('../')
or /^\.\.?\//.test(oldImport)
? Just checking for the dot character also catches valid mappings starting with .
which TypeScript resolves fine
Thanks, startsWith added in 1.7.11