justkey007 / tsc-alias

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

Adding `../` before external dependency from node_modules #193

Closed 2coo closed 9 months ago

2coo commented 1 year ago

Before compiling

image

After compiling

Screenshot 2023-08-24 at 23 58 47 image

To produce

graphql is external dependency I installed from npm registry and I have ~/graphql path alias which points to ./src/graphql so that I think it collides with ~/graphql image

tsconfig.json

image

kyle-belle commented 10 months ago

I get the same issue. In my case it happen when i try to import dayjs

relavant tsconfig.json

"outDir": "./build",
      "paths": {
        "@app/db": ["../db"],
        "@app/db/*": ["../db/*"],
        "@app/redis": ["../redis"],
        "@app/redis/*": ["../redis/*"],
        "@app/emitter": ["../emitter"],
        "@app/emitter/*": ["../emitter/*"],
        "@app/dayjs": ["../dayjs"],
        "@app/dayjs/*": ["../dayjs/*"],
        "@app/utils": ["../utils"],
        "@app/utils/*": ["../utils"],
        "@app/values": ["../values"],
        "@app/values/*": ["../values/*"]
      },

tsc input:

import dayjs from "dayjs";

tsc output/tsc-alias input:

const dayjs_1 = __importDefault(require("dayjs"));

tsc-alias output:

const dayjs_1 = __importDefault(require("../dayjs"));

whats strange is there are other dayjs imports that weren't touched

const dayjs_1 = __importDefault(require("../dayjs"));
const advancedFormat_1 = __importDefault(require("dayjs/plugin/advancedFormat"));
const arraySupport_1 = __importDefault(require("dayjs/plugin/arraySupport"));
const isLeapYear_1 = __importDefault(require("dayjs/plugin/isLeapYear"));
kyle-belle commented 10 months ago

its really unfortunate because this is the closest i got to having my project transpiled and running with just node. literally if i just remove the ../ from this line manually it works. The other alternative have even more issues. typescript-transform-paths doesn't correctly replace relative paths for me.

this package correctly replaces

require("@app/redis")

with

require("../redis")

it treated outDir as the new base/root which is what i actually want

typescript-transform-paths does this (still tries to pull original ts file from outside outDir):

require("../../../redis");
fdendorfer commented 10 months ago

This solved it for me: https://github.com/justkey007/tsc-alias/issues/185, but i'm using ES imports not CommonJS