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

Re-export types from monorepo internal dependencies #302

Open
2 tasks done
gxxcastillo opened this issue Feb 22, 2024 · 10 comments
Open
2 tasks done

Re-export types from monorepo internal dependencies #302

gxxcastillo opened this issue Feb 22, 2024 · 10 comments

Comments

@gxxcastillo
Copy link

Description

I have a packageA in a monorepo and it depends on packageB and packageC, packageC depends on packageD.

When I run vite build on packageA using the dts plugin and { rollupTypes: true }, I don't see the types from packageB, packageC, and packageD being exported from packageA.

Suggested solution

Have a option that allows for types from dependencies to be included in the generated *.d.ts files

Alternative

No response

Additional context

No response

Validations

  • Read the FAQ.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@qmhc
Copy link
Owner

qmhc commented Mar 12, 2024

If the depended relationships are correct, TypeScript may always resolve the declarations map when you are using them.

Like the following:

// A.d.ts
import D from 'packageD'

interface A extends D {
  // ...
}

TypeScript will find the declaration files of packageD from your dev dependencies (deeply, you don't need to install packageD direactly).

@ohkimur
Copy link

ohkimur commented Apr 14, 2024

@qmhc We need a fix for this. It's making life hard when using monorepos. For example, if there is at least one private package that is imported into another public package, the private import is not resolved for types currently. At least not consistently.

It seems that if we have rollupTypes set on true and bundledPackages set to ["@company/*"] then for some reason it works. However, if we have only the bundledPackages param set, the imports are not resolved again. After looking deep through the types and the code of vite-plugin-dts I think this is not as intended.

It would be great if we could tell to vite-plugin-dts which packages are bundled via bundledPackages for it to bundle the types, but without being required to rollup the types via rollupTypes param. I personally don't want to be forced to rollup my types, and also this makes life hard when working with packages that have multiple entry points.

My proposal is to just decouple them the right way so that we make this plugin even more better. Keep it up!

@qmhc
Copy link
Owner

qmhc commented Apr 14, 2024

@ohkimur You can view this demo: https://stackblitz.com/edit/vitejs-vite-oaq33v?file=vite.config.ts&view=editor.

If you think this is not match your case, you can edit it to reproduce you issue.

@ohkimur
Copy link

ohkimur commented Apr 15, 2024

@qmhc I've updated the demo with a situation as close to the one I have. Here is a link: https://stackblitz.com/edit/vitejs-vite-k5aekx

If you go to packages/foo/dist and check foo.d.ts you'll see that it has the following content:

import { Baz } from '@test/baz';

export interface Foo {
    baz: Baz;
    c: boolean;
}
export declare const foo: {
    baz: number;
};

However, the @test/baz doesn't exist in the foo package. While this works inside the monorepo, because the package will be found in the workspace, after package publishing this will fail to resolve.

It'll be better, in my opinion, to include the types of @test/baz inside the build of @test/foo. Something more like this:

export interface Baz {
    a: number;
    b: string;
}
export declare const baz = 1;

export interface Foo {
    baz: Baz;
    c: boolean;
}
export declare const foo: {
    baz: number;
};

NOTE: As I already mentioned previously, when using rollupTypes and bundledPackages, this already happens. It'll be nice to happen also without bing required to set rollupTypes on true.

@avp1598
Copy link

avp1598 commented Apr 23, 2024

@ohkimur for me even adding rollupTypes and bundledPackages does not work for some reason, it generates this index.d.ts file

import { Something } from './common_package';

but there is no common_package file generated, I am on turborepo yarn monrepo setup and i am trying to use a common package as an internal dependency on another package i am building using vite

my vite config

export default defineConfig({
  build: {
    lib: {
      entry: resolve(__dirname, "src/index.ts"),
      fileName: "index",
      formats: ["es"],
    },
    target: "esnext",
    outDir: "dist",
  },
  plugins: [
    dts({
      bundledPackages: ["common_package"],
      rollupTypes: true,
      staticImport: true,
      beforeWriteFile: (filePath, content) => {
        if (filePath.endsWith("index.d.ts")) {
          return {
            filePath: filePath.replace(/\.d\.ts$/, ".d.mts"),
            content,
          };
        }
      },
    }),
    wasm(),
  ],
});

added my dependency like this in package.json

 "dependencies": {
    "viem": "^2.7.15",
    "common_package": "*"
  }

even tried like this

 "dependencies": {
    "viem": "^2.7.15",
    "common_package": "file:../common"
  }

nothing seems to work

@ohkimur
Copy link

ohkimur commented Apr 23, 2024

@avp1598 Your setup is very similar to mine. I also use turborepo and multiple packages that are powered by vite.

Here is how I setup the dts for my packages:

    dts({
      exclude: ["tests"],
      outDir: "dist/types",
      rollupTypes: true,
      bundledPackages: ["@acme/*"],
    }),

@pandeymangg
Copy link

@ohkimur I'm also running into similar problems. I have a monorepo setup with turborepo and pnpm and it has a couple of packages built with vite. I have a packageA which uses an internal package packageB with multiple exports like packageB/app and packageB/website.

For some reason, having bundledPackages set to bundledPackages: ["packageB"] in the vite config of packageA does not inline the declarations of packageB and its the same problem that you're talking about. It should work though, since the package.json for packageA does have packageB as a dependency but its not 😞

@ohkimur
Copy link

ohkimur commented May 4, 2024

@pandeymangg Yeah, this is also the case for my monorepo. You can rollupTypes as a workaround, but that is far from the correct behavior.

@qmhc Do you have any ideas on how to fix this? If you give at least some hints, I could make a PR to fix this one.

@qmhc
Copy link
Owner

qmhc commented May 5, 2024

@ohkimur Well, it's a difficult work. You need to analyze the relationship between packages, and manually find the modules and symbols in internal packages which are used in the public packages.

After some research I found that it even need to use internal APIs of TypeScript. 😓

@augusttty
Copy link

facing the same issue in monorepo, the dependencies may be a private module.it would be much better that vite-plugin-dts can extract some type declaration from dependencies into output...

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

6 participants