justkey007 / tsc-alias

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

Package import replaced with relative path to folder name #185

Closed Getterac7 closed 1 year ago

Getterac7 commented 1 year ago

tsc-alias seems to be replacing "react" imports with "../react" when the files exist inside a folder called "react". I have to believe this is because there's a folder with the same name as a package import, but why it's trying to replace that I'm not user. Below are the most relevant files as an example of my setup.

tsc-alias version: 1.8.7

package.json:

{
  "scripts": {
    "build": "tsc --build && tsc-alias"
  },
  "dependencies": {
    "react": "^17.0.0"
  },
  "devDependencies": {
    "tsc-alias": "^1.8.7",
    "typescript": "^4.9.5"
  }
}

tsconfig.json:

{
  "references": [
    {
      "path": "../otherPackage/tsconfig.json"
    }
  ],
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "build",
    "paths": {
      "@otherPackage/*": ["../otherPackage/src/*"]
    },
    "baseUrl": ".",
    "sourceMap": false,
    "tsBuildInfoFile": "build/.tsbuildinfo",
  },
  "exclude": ["./build/**/*", "node_modules", "@company/otherPackage"],
  "tsc-alias": {
    "verbose": true,
    "replacers": {
      "default": {
        "enabled": true,
        "file": "replacer.js"
      }
    }
  }
}

replacer.js:

/** Replace our monorepo alias with the external package name when building */
exports.default  ({ orig }) => {
  return orig.replaceAll(/@otherPackage\//g, "@company/otherPackage/")
}

src/react/MyContext.tsx:

import { createContext, useContext } from "react"

export interface MyContextProps {
}

export const MyContext = createContext<MyContextProps>(
 {} as MyContextProps
)

export function useMyContext(): MyContextProps {
  return useContext(MyContext)
}

debug output when running tsc-alias:

...
tsc-alias debug: default replacer - requiredModule: '../react'
tsc-alias debug: default replacer - alias:
...
raouldeheer commented 1 year ago

@Getterac7 Have you tried disabling the baseUrl replacer? Please read the first question from the FAQ #110

Add this to tsconfig.json

{
  "compilerOptions": {
    ...
  },
  "tsc-alias": {
    "replacers": {
      "base-url": {
        "enabled": false
      }
    }
  }
}
Getterac7 commented 1 year ago

So simple, I should have looked for more docs! Thanks for the quick reply.