justkey007 / tsc-alias

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

BUG :: tsc-alias making relative a package.json defined lib #214

Closed alanszp closed 6 months ago

alanszp commented 6 months ago

Context

package.json

"tsc-alias": "^1.8.8",

yarn.lock

tsc-alias@^1.8.8:
  version "1.8.8"
  resolved "https://registry.npmjs.org/tsc-alias/-/tsc-alias-1.8.8.tgz#48696af442b7656dd7905e37ae0bc332d80be3fe"
  integrity sha512-OYUOd2wl0H858NvABWr/BoSKNERw3N9GTi3rHPK8Iv4O1UyUXIrTTOAZNHsjlVpXFOhpJBVARI1s+rzwLivN3Q==
  dependencies:
    chokidar "^3.5.3"
    commander "^9.0.0"
    globby "^11.0.4"
    mylas "^2.1.9"
    normalize-path "^3.0.0"
    plimit-lit "^1.2.6"

Original TS file

// File: src/passport.ts

import GlobalPassport from "passport";

JS code (Compiled by TSC)

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("passport"));

tsc-alias output

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("./passport")); ----> CHECK HERE THAT MADE RELATIVE PASSPORT

Expectations

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("passport")); ----> CHECK HERE. Just do not make passport relative

tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 20",
  "_version": "20.1.0",
  "include": ["./src/**/*", "./src/**/.*", "./*.js", "./.*.js"],
  "exclude": ["./dist/**/*", "./node_modules/**/*"],
  "compilerOptions": {
    "lib": ["es2022"],
    "module": "node16",
    "target": "es2022",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node16",
    "resolveJsonModule": true,
    "outDir": "./dist",
    "baseUrl": "./",

    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,

    "paths": {
      "@/*": ["src/*"]
    }
  }
}

More context

It's strange since I have other libs and default imports but it's only happening in passport import!

alanszp commented 6 months ago

Debugging I found the problem!

I just renamed the file from src/passport.ts to src/passportInstance.ts and it stopped!

@izumiya @mgcrea @nicholas-ochoa @Jokero I'm closing this since I found the problem, but I would suggest trying to reproduce it, since it's a bug.

It seems that when you import a lib in a file with the same name as the lib (and maybe on the root of the project) it tries to add the ./

leo-fresha commented 1 month ago

I think this should be reopened, as it seems to be an actual bug in tsc-alias.

I have encountered this while working on a backend typescript project. My theory is that if a module is defined in a file with the same name as a node_modules dependency, and such module imports the node_modules dependency, the import is wrongly interpreted as to be relative (basically importing itself).

I have this code in a file scr/utils/zod.ts, which contains utility functions related to the Zod validation library.

// scr/utils/zod.ts
// defines extra helpers and utility functions for the zod library

import { z } from 'zod'  // import the library

export function arrayWithOnlyValidElements<T extends z.ZodTypeAny>(
  itemSchema: T,
) {
  const catchValue = {} as never;

  return z
    .array(itemSchema.catch(catchValue))
    .transform((arr) => arr.filter((el) => el !== catchValue))
    .catch([]);
}

After running tsc (with ES imports), the import is still correctly import { z } from 'zod.js'. After running tsc-alias, a .js is added to the import:

> head -n 1 dist/utils/zod.js
import { z } from 'zod.js'

Which is incorrect. This is with tsc-alias 1.8.7. @justkey007 is it possible to reopen this issue?

Having local modules with the same name as some dependency, in a subdir like utils or helpers, is quite a common pattern when writing code that is related to that dependency.