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

Couple of typescript related issues #3

Closed
axelthat opened this issue Jun 8, 2021 · 4 comments
Closed

Couple of typescript related issues #3

axelthat opened this issue Jun 8, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@axelthat
Copy link

axelthat commented Jun 8, 2021

I'm using vite for my vanilla typescript project along with this plugin. I found few bugs with this library,

  • When I build the project with this plugin, it leaves the converted javascript files in the disk as you can see in the project.

Screen Shot 2021-06-08 at 7 46 35 PM

  • If I use a non relative path like /@/, then the emitted declarations includes incorrect path. For example,

I have constants.ts,

export enum Color {
    Red,
    Blue,
    Green
}

export const colors = new Array<Color>(Color.Red, Color.Blue, Color.Green)

And in main.ts I have following,

import { Color } from "/@/constants"

export default function viteProject(): Color {
    console.log(Color.Red)
    return Color.Red
}

Then the emitted declaration file has src/constants which is an incorrect path,

import { Color } from 'src/constants';
export default function viteProject(): Color;

Here's the source code: https://github.com/sanjade/js-bug

@qmhc
Copy link
Owner

qmhc commented Jun 8, 2021

Thanks for using this plugin.

For the first point, is not a bug in this plugin, because you used tsc without --noEmit parameter.

The second point strictly is not a bug.

The reason is when all files are in one directory and without rootDir config in tsconfig.json, the root directory in ts will be inferred to that only one directory, so incorrect path calculation occurred.

Currently you can add { "rootDir": "." } to your tsconfig.json solve this problem succinctly.

@qmhc qmhc added the bug Something isn't working label Jun 8, 2021
@axelthat
Copy link
Author

axelthat commented Jun 9, 2021

@qmhc For the first point, do you mean in package.json I should do "build": "tsc --noEmit && vite build"? Because I did that and it gives me the following error,

tsconfig.json:9:9 - error TS5053: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'.

9         "emitDeclarationOnly": true,

Regarding the second point the dts file is generated as main.d.ts. Shouldn't it be index.d.ts? Because typescript complains that there are no types for the lib.

Screen Shot 2021-06-09 at 8 02 42 AM

@qmhc
Copy link
Owner

qmhc commented Jun 9, 2021

@sanjade If you only want to check your typescript you needn't add emitDeclarationOnly option.

The .d.ts files named by the source files name, for your situation I think you should add types option in your lib package.json to specify the entry of type analysis, just like main option.

{
  "main": "dist/viteproject.cjs.js",
  "types": "dist/main.d.ts",
}

I recommend you to try @microsoft/api-extractor if you want to bundle all declaration in one file.

The staticImport option is for using @microsoft/api-extractor.

@qmhc qmhc closed this as completed in a1d83d2 Jun 9, 2021
@axelthat
Copy link
Author

axelthat commented Jun 9, 2021

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants