jonkwheeler / tsconfig-replace-paths

Replace absolute paths to relative paths for package compilation
MIT License
72 stars 11 forks source link

Ambiguity with folder modules and imports #27

Closed nicknotfun closed 1 year ago

nicknotfun commented 2 years ago

We just ran into this with our project, with the typescript (pre-compiled) state of:

# path mappings
    "baseUrl": "./src",
    "paths": {
      "@/*": [
        "./*"
      ],

# src/foo/bar.ts
import { x } from "@/foo"

# src/foo.ts
export const x = 42;

The rewritten output we get in bar.ts is:

require("./")

What I believe may be happening is it's mistakenly assuming a folder import (ala if index.ts existed); however in this circumstance there is no index.ts and it should instead generate "../foo.js"

Does this sound correct or might we have something more nuanced missing in our setup?

nicknotfun commented 2 years ago

Here is what I believe is needed: https://github.com/jonkwheeler/tsconfig-replace-paths/pull/28

jonkwheeler commented 2 years ago

I believe I have some free time coming up here soon. Maybe this week or next. Hopefully I can look at this and a few other things while on my paternity leave 🙃🤠

nicknotfun commented 2 years ago

Thanks! and congrats! For what it's worth I switched to using a fork with the PR and it certainly does fix the issue.

I'd phrase the problem as it's not correctly handling the ambiguity of package names when a directory and file have the same base name.

jonkwheeler commented 2 years ago

Yo when you get a sec can you send me a dummy repo with the issue?

jonkwheeler commented 2 years ago

Before I go digging further, I noticed this is how I have all my repos setup which use this package to publish... I have rootDir set.

"rootDir": "./src",
    "baseUrl": ".",
    "paths": {
      "@components": ["./src/components"],
      "@components/*": ["./src/components/*"],
      "@root": ["./"],
      "@root/*": ["./*"],
      "@utils": ["./src/utilities"],
      "@utils/*": ["./src/utilities/*"],
      "@vars": ["./src/variables"],
      "@vars/*": ["./src/variables/*"]
    },

So I'm wondering if the baseUrl is not being accounted for correctly, or simply switching your config to use rootDir would work.