tsconfig-replace-paths icon indicating copy to clipboard operation
tsconfig-replace-paths copied to clipboard

Ambiguity with folder modules and imports

Open nicknotfun opened this issue 3 years ago • 5 comments

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 avatar Feb 16 '22 01:02 nicknotfun

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

nicknotfun avatar Feb 16 '22 01:02 nicknotfun

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 🙃🤠

jonkwheeler avatar Feb 16 '22 01:02 jonkwheeler

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.

nicknotfun avatar Feb 16 '22 02:02 nicknotfun

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

jonkwheeler avatar Feb 16 '22 03:02 jonkwheeler

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.

jonkwheeler avatar Feb 16 '22 04:02 jonkwheeler