justkey007 / tsc-alias

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

baseUrl value is not accurately recognized #208

Open RHIE-coder opened 7 months ago

RHIE-coder commented 7 months ago

There is an error in not parsing the path accurately, depending on the baseUrl value in tsconfig.json.

Error Case

{
    ...
    "compilerOptions": {
        "baseUrl": "./src/jslib",
        "paths": { "@/crypto/*": ["crypto/*"], "@/utils/*": ["utils/*"] }
        "outDir": "./build"
     }
    ...
}

Problem found with debug

Correctly find the file to replace and read the baseUrl and path values in tsconfig.json correctly.

However, the .replace('---', '') function does not seem to work.

tsc-alias debug: default replacer - absoluteAliasPath:  '---/Users/rhiemh/Workspace/@demo/test-sandbox/build'
tsc-alias debug: default replacer - Invalid path
tsc-alias debug: base-url replacer - requiredModule:  '@/utils/math'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/index.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/crypto/decrypt.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/crypto/encrypt.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/utils/math.js'

Success Case

{
    ...
    "compilerOptions": {
        "baseUrl": "./",
        "paths": { "@/crypto/*": ["./src/jslib/crypto/*"], "@/utils/*": ["./src/jslib/utils/*"] },
        "outDir": "./build"
     }
    ...
}

Debug result

tsc-alias debug: default replacer - absoluteAliasPath:  '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/utils'
tsc-alias debug: default replacer - relativeAliasPath:  './jslib/utils'
tsc-alias debug: default replacer - newImportScript:  'require("./jslib/utils/math")'
tsc-alias debug: default replacer - modulePath:  './jslib/utils/math'
tsc-alias debug: base-url replacer - requiredModule:  './jslib/utils/math'
tsc-alias debug: base-url replacer - already resolved
tsc-alias debug: replaced file with changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/index.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/utils/math.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/crypto/decrypt.js'
tsc-alias debug: replaced file without changes: '/Users/rhiemh/Workspace/@demo/test-sandbox/build/src/jslib/crypto/encrypt.js'

Issue Conclusion

As you can see, both 'Error Case' and 'Success Case' have the same path, but they behave differently depending on the combination of baseUrl and path.

Am I doing something wrong or is it a bug?

Thanks!

Best regards.