qmhc / vite-plugin-dts

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

Option to flatten "*.d.ts" files #255

Closed bgrand-ch closed 1 year ago

bgrand-ch commented 1 year ago

Description

Hello,

Thanks for this plugin.

Could you add an option to flatten "*.d.ts" files please?

Suggested solution

See rollup-plugin-flat-dts

Alternative

No response

Additional context

No response

Validations

qmhc commented 1 year ago

By default, the generated decalration files are flatten (without rollupTypes: true).

bgrand-ch commented 1 year ago

By default, the generated decalration files are flatten (without rollupTypes: true).

Thanks for the answer.

With the following Vite.js + dts configuration:

https://github.com/bgrand-ch/grapesjs-icons/blob/main/vite.config.js

I obtain this result (see into dist/types):

https://www.npmjs.com/package/grapesjs-icons?activeTab=code

It's not flatten, not merged into a single dts file.

Could you suggest me a better configuration to have one index.d.ts at the end please?

qmhc commented 1 year ago

Sry, maybe I misunderstand the meaning of 'flatten'. Would you show me what a result (structure of the out directory and declaration files) is flatten?

carpacciao commented 1 year ago

If I'm correct, he wants something like this:

(INIT) Let's say we have a library like this:

src/index.ts  // entry point
src/Form/index.ts
src/Form/Form.vue

(THIS) default build will be (from my memory, hopefully accurate):

dist/index.js
dist/index.d.ts
dist/types/index.d.ts
dist/Form/index.d.ts
dist/Form/Form.d.ts

(TO THIS) with "flatten" / "merged" types:

dist/index.js
dist/index.d.ts

He's talking about merging all types into one file in the dist folder.

You can achieve this by first building your lib with vite-plugin-dts and then use api-extractor with the dtsRollup option activated (and bunch of other options).

I saw that vite-plugin-dts is using api-extractor under the hood, so maybe it's possible to add the "merging feature" from it.

qmhc commented 1 year ago

If you want to merge all types into one file, just specfiy rollupTypes: true.

carpacciao commented 1 year ago

Yep, we sure missed the option 👀

But still a small problem, it generates the right working file but the types folder is remaining in the dist folder. Even when you specify other path in tsconfig/dts(). When you specify a custom library name, the file in the types folder is exporting the wrong file.

Let's say:

const libraryName = "cool";
/// 
src/index.ts  // entry point
src/Form/index.ts
src/Form/Form.vue

it will generate:

dist/cool.js
dist/cool.d.ts
dist/types/cool.d.ts // this one is exporting `../index.d.ts` which doesn't exist.

Any idea?

I know that I can rm -rf dist/types.

qmhc commented 1 year ago

@carpacciao Thanks for your expression.

Would you make a reproduction in stackblitz? Because I don't konw what are the config files look like, it's hard to confirm what's going on.

carpacciao commented 1 year ago

It was remaining config inside package.json:

{
 "types": "./dist/types/main.d.ts", // this
 "types": "./dist/main.d.ts", // to this
}

No more help needed, thanks @qmhc :)

I think this issue can be closed with the following solution:

roman-petrov commented 1 year ago

I am just trying to accomplish exactly the same right now task using rollupTypes: true and getting build error like

error during build:
Error: Error parsing C:\Projects\reactor\packages\reactor-intl\api-extractor.json:
The "mainEntryPointFilePath" value is not a declaration file: C:\Projects\reactor\packages\reactor-intl\src\index.ts
    at ExtractorConfig.prepare (C:\Projects\reactor\node_modules\@microsoft\api-extractor\lib\api\ExtractorConfig.js:577:19)
    at rollupDeclarationFiles (file:///C:/Projects/reactor/node_modules/vite-plugin-dts/dist/index.mjs:194:43)
    at Object.writeBundle (file:///C:/Projects/reactor/node_modules/vite-plugin-dts/dist/index.mjs:861:13)
    at async Promise.all (index 0)
    at async PluginDriver.hookParallel (file:///C:/Projects/reactor/node_modules/rollup/dist/es/shared/node-entry.js:25264:9)
    at async file:///C:/Projects/reactor/node_modules/rollup/dist/es/shared/node-entry.js:26576:13
    at async catchUnfinishedHookActions (file:///C:/Projects/reactor/node_modules/rollup/dist/es/shared/node-entry.js:25702:16)
    at async build (file:///C:/Projects/reactor/node_modules/vite/dist/node/chunks/dep-75f53616.js:47962:22)
    at async CAC.<anonymous> (file:///C:/Projects/reactor/node_modules/vite/dist/node/cli.js:822:9)

My Vite config is:

export default defineConfig({
  build: {
    lib: {
      entry: "src/index.ts",
      formats: ["es"],
    },
  },
  plugins: [
    dts({
      rollupTypes: true,
    }),
  ],
});

Everything is fine without rollupTypes: true, but types are not being merged into single file. Does anyone have any idea why this can happen?...

qmhc commented 1 year ago

@roman-petrov Did you set a path not ending with .d.ts in the types section of package.json?

roman-petrov commented 1 year ago

@qmhc, this was exactly the source of this problem, it's gone! works like a charm, thank you for the help!