Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?] Index file import not being transformed correctly #159

Closed
SammyWhamy opened this issue May 24, 2022 · 4 comments
Closed

[Bug?] Index file import not being transformed correctly #159

SammyWhamy opened this issue May 24, 2022 · 4 comments

Comments

@SammyWhamy
Copy link

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.

@nonara
Copy link
Collaborator

nonara commented Aug 10, 2022

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 nonara changed the title Index file import not being transformed correctly [Bug?] Index file import not being transformed correctly Aug 10, 2022
@nonara
Copy link
Collaborator

nonara commented Oct 20, 2022

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

@nonara nonara closed this as completed Oct 20, 2022
@SammyWhamy
Copy link
Author

SammyWhamy commented Mar 20, 2023

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

@nonara
Copy link
Collaborator

nonara commented Mar 21, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants