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

Aliases import in types definition isn't resolved on the build result #137

Open
bryanltobing opened this issue Feb 24, 2022 · 7 comments
Open

Comments

@bryanltobing
Copy link

bryanltobing commented Feb 24, 2022

Current Behavior

I added absolute import aliases configuration in tsconfig.json

{
  "extends": "@tsconfig/create-react-app/tsconfig.json",
  "include": ["src", "types", "stories"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@Components": ["src/Components"],
      "@Components/*": ["src/Components/*"]
    }
  } 
}

and using that aliases import in one of my interfaces file
image

but when running dts build which will bundle the component. the import still not updated to relative import and cause the types not detected and the component will lose the ability of the types since it's refer to the incorrect path.
image

Expected behavior

Expected behavior is that when build the app it will resolve all aliases import into relative import so that it refers to the correct path

Suggested solution(s)

None at the moment,

My temporary workaround is just to update all import to relative import. but it will be a mess if in the future i have deeper nested folder with ../../../ import

Additional context

Since this lib based on tsdx. I have tried many things from the github issue on tsdx library but nothing seems to work
jaredpalmer#91

What i have tried

Your environment

 System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
    Memory: 6.51 GB / 13.86 GB
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (98.0.1108.56)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    dts-cli: ^1.4.0 => 1.4.0
    typescript: ^4.5.5 => 4.5.5
@aladdin-add
Copy link

you'll need to config aliases in the rollup config.

@bryanltobing
Copy link
Author

my 3rd try in my description was customizing rollup config. not sure what's wrong since i don't have experience with rollup. do you have your own configuration that support this aliases import ?

@aladdin-add
Copy link

aladdin-add commented Feb 25, 2022

I've tested on my local(https://github.com/weiran-zsd/dts-cli/tree/issue137), it seems working(you'll still need to config jest aliases).

it's less ideal, but a good improvement is to read tsconfig.paths, and apply the aliases to rollup & jest. So, users do not have to add multi configs.:)

@bryanltobing
Copy link
Author

bryanltobing commented Feb 25, 2022

I have cloned and https://github.com/weiran-zsd/dts-cli/tree/issue137 but still not working with my case.
This is my case https://github.com/bryantobing12/dts-cli-aliases. when I created an interface in single file and export it, then import it in my component, i ran yarn build the build result doesn't resolve the import properly.

src/Components/foo.interface.ts

export interface FooInterface {
  name: string;
}

src/index.ts

import * as React from 'react';
import { foo } from '@Components/foo';
import { FooInterface } from '@Components/foo.interface';

console.log(foo);

// Delete me
export const Thing = () => {
  return <div>Welcome to your first test package.</div>;
};

export const FooComponent = (props: FooInterface) => {
  return <div>FooComponent</div>;
};

build result

image

@bryanltobing bryanltobing changed the title Aliases import in type definition isn't resolved on build result Aliases import in types definition isn't resolved on the build result Feb 25, 2022
@aladdin-add
Copy link

I see. it's expected, as it won't transform the paths to relative. maybe bundling types with --rollupTypes could help. can you give it a try?

@bryanltobing
Copy link
Author

not really sure what that means. but i try to add --rollupTypes flag on build command

"scripts": {
  "analyze": "size-limit --why",
  "build": "dts build --rollupTypes",
  "lint": "dts lint",
  "prepare": "dts build",
  "size": "size-limit",
  "start": "dts watch",
  "test": "dts test --passWithNoTests"
},

but the import issue still there and the difference is just the build result remove the Components folder. which is not expected, since we still need that folder.

image

@aladdin-add
Copy link

well, it sounds a bug to me!

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

No branches or pull requests

2 participants