Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
804 stars 68 forks source link

Generated type definition file is empty #147

Closed vegidio closed 3 years ago

vegidio commented 3 years ago

Checklist

Code Snipped

import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import dts from 'rollup-plugin-dts';

export default [
    {
        input: 'src/index.ts',
        plugins: [commonjs(), typescript({ useTsconfigDeclarationDir: true })],
        output: [
            {
                file: 'dist/bundle.js',
                format: 'esm',
                sourcemap: false,
            },
        ],
    },
    {
        input: 'dist/types/index.d.ts',
        output: [{ file: 'dist/types.d.ts' }],
        plugins: [dts()],
    },
];

Error Message

I created a simple project (it can be found here: https://github.com/vegidio-js/template-typescript) where I'm trying to put all the type definition files created by TypeScript into a single d.ts file, but the generated d.ts file is blank.

If you clone the project above and run yarn && yarn build then you will see the following message:

dist/types/index.d.ts → dist/types.d.ts...
(!) Generated an empty chunk
index.d
created dist/types.d.ts in 7ms

This seems to be happening because my d.ts entrypoint dist/types/index.d.ts is blank, but all the other d.ts files generated by TS have something in it, but isn't this package supposed to merge all files together under a single one or did I miss something in the configuration?

Swatinem commented 3 years ago

This seems to be happening because my d.ts entrypoint dist/types/index.d.ts is blank

I think thats because https://github.com/vegidio-js/template-typescript/blob/master/src/index.ts has no export declarations in it. Typescript will do its own dead code elimination. And obviously if your index file is empty, this plugin won’t find anything.

vegidio commented 3 years ago

Damn, such a silly mistake of mine 🙈 Sorry for wasting your time on this.

IanVS commented 1 year ago

Typescript itself will generate a declaration file with export {} even if there are no exports in the source file. See https://github.com/microsoft/TypeScript/issues/20496. In my case I might be getting an error from tsup, which I think uses this plugin. But I'm not sure where the problem actually lies.

await-ovo commented 1 year ago

Although typescript emitted export {} in declaration file, It seems that rollup itself will convert export {} to empty string. Can we add export { } directly to the generated emty code to avoid empty dts file:

const plugin = () => ({
  renderChunk: (code) => {
    if (!code) {
      code += 'export { }'
    }
    return code
  }
})