typescript-transform-paths icon indicating copy to clipboard operation
typescript-transform-paths copied to clipboard

[Bug?] Index file import not being transformed correctly

Open SammyWhamy opened this issue 3 years ago • 1 comments

I have this settings in my tsconfig:

"paths": {
  "@util": ["util/index.js"]
}

But when I import { } from "@util"; It emits this code: import { } from "../util"; If I instead rename the file to something else, e.g. barrel, it emits this: import { } from "../util/barrel";

The problem is, node can resolve neither of these. And as I specify that @util should be rewritten to util/index.js I expect it to keep the index.js part, without this my import simply doesn't function.

I can't really tell if this is intended behavior or not, but I think emitting the full path (including index.js) should always be a safe option if it is specified as such in the paths option.

I know I could instead do "@util/*": ["util/*"] And then import { } from "@util/index.js"; But it'd be even better if I can have it emit the index.js for me as well.

SammyWhamy avatar May 24 '22 22:05 SammyWhamy

Hi @SammyWhamy .

Thanks for the report! Sorry to hear you're facing issues with the library. This could simply be an issue with rootDir or something to that effect. Though it looks like it might have to do with a known issue in handling indexes. I have this mostly fixed in the new version, but I have to complete it.

If you can provide a reproduction, I will have a look!

nonara avatar Aug 10 '22 01:08 nonara

Closing for housekeeping. Feel free to reopen if you have a reproduction.

nonara avatar Oct 20 '22 21:10 nonara

Came back to this after a while, with a reproduction!

I have the following typescript file:

{
  "compilerOptions": {
    "lib": [
      "es2021",
    ],
    "module": "esnext",
    "target": "es2021",
    "moduleResolution": "Node",

    "sourceMap": true,
    "declaration": true,
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,

    "baseUrl": "./src",
    "rootDir": "./src",
    "outDir": "./dist",

    "plugins": [
      { "transform": "typescript-transform-paths" },
      { "transform": "typescript-transform-paths", "afterDeclarations": true}
    ],

    "paths": {
      "@client": ["client/index.js"],
      "@objects": ["objects/index.js"],
      "@cache": ["cache/index.js"],
      "@utils": ["utils/index.js"],
    }
  }
}

In my typescript files, I want to use imports in the following way:

import { Client, SomeOtherClass } from "@client";

Where src/client/index.ts is the following:

export * from './Client.js';

And src/client/Client.ts contains something like:

export class Client { }
export class SomeOtherClass { }

However, when building this source, the import gets wrongly transformed to

import { Client, SomeOtherClass } from "./client";

Which in my experience doesn't work with node, as you need to explicitly provide the index.js part I'm using the current latest version of this package (3.4.6), and TypeScript 4.9.5

SammyWhamy avatar Mar 20 '23 17:03 SammyWhamy

This is tied to a know issue with indexes #136. Hoping to have that sorted in the next major version.

For now, you can use the @transform-path tag to explicitly set the output to ./client/index.js

nonara avatar Mar 21 '23 00:03 nonara