qmhc / vite-plugin-dts

A Vite plugin for generating `.d.ts` files.
MIT License
1.3k stars 90 forks source link

Is it possible to inline external dependencies? #305

Closed Blankeos closed 7 months ago

Blankeos commented 8 months ago

Description

Context

My goal currently is to generate the App type for my ElysiaJS server (basically the same as tRPC). So I can use it for my client on the frontend.

So far it works great. Here's my Config:

import { resolve } from "path";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";

export default defineConfig({
  build: {
    lib: {
      entry: resolve(__dirname, "src/appType.ts"),
      name: "MyLib",
      formats: ["es"],
    },
    rollupOptions: {
      output: {
        dir: "codegen-vite",
      },
    },
  },
  plugins: [dts({ rollupTypes: true, outDir: "codegen-vite" })],
});

It then generates a file like this: image

I can basically serve this on an endpoint and I can consume it on my frontend for my client by saving it somewhere in src after a curl script. It's super cool since I wouldn't need to share these types via an intermediary npm package.

The Feature Request

Is there any way we can also "inline" the types from external dependencies? 'elysia', 'geojson', 'lucia' for example. So that I wouldn't need to bun add -D @types/elysia @types/geojson @types/lucia on the client.

Suggested solution

Not too sure...

A great API addition would probably be dts({ rollupTypes: true, inlineExternalDeps: true }) though.

Alternative

No response

Additional context

No response

Validations

qmhc commented 8 months ago

Why you want this? (first of all, it's hard to implement ...) I think this is a fake requirement, refer to #302.

Blankeos commented 7 months ago

No worries! Just wondering.

Why I want this? I wrote it above. But for more context: I have separate client and backend repos for ElysiaJS. It's a typesafe backend like tRPC and similarly, it only works in monorepos, or if the type definitions are shared via an npm package. I have a polyrepo client and backend separate repos.

In any case, vite-plugin-dts already takes care of the transpilation for me. The type definitions from external libraries are also pretty much at the top anyway. So my work-around would be just to reinstall the types via the client repo (bun install -D @types/*)

Will be closing this now! Thanks!