wessberg / rollup-plugin-ts

A TypeScript Rollup plugin that bundles declarations, respects Browserslists, and enables seamless integration with transpilers such as babel and swc
MIT License
496 stars 32 forks source link

broken d.ts in projects with default exports in separate files #193

Open megastels opened 2 years ago

megastels commented 2 years ago

plugin generates broken declarations

Reproduction

Simple example

A.ts

export default "Module A";

B.ts

export default "Module B";

entry point

export { default as ModuleA } from './A';
export { default as ModuleB } from './B';

config

export default defineConfig({
    input: "src/index.ts",
    output: {
        file: "dist/index.js",
    },
    plugins: [
        ts(),
    ]
})

Expected Behavior

declare const _default_1: "Module A";
declare const _default_2: "Module B";
export { _default_1 as A, _default_2 as B };

Actual Behavior

// Cannot redeclare block-scoped variable '_default'.
declare const _default: "Module A";
declare const _default: "Module B";
export { _default as A, _default as B };
tvooo commented 1 year ago

I've been seeing the same issue for a while; thanks for providing the reproduction example, @megastels! I reckon this was caused by https://github.com/wessberg/rollup-plugin-ts/commit/16d24fa1660d581b0e93d1566ef9dea2dc8fd179

I'd love to see this addressed :-) Happy to help too, but not sure how much time I'll have in the coming weeks.

tvooo commented 1 year ago

@wessberg I managed to add a test case with the above example; as expected, it fails. However, I'm unsure where to continue - all my console.logs are being swallowed when running the tests, for example :smile: . Any pointers?

michaelfaith commented 1 year ago

Came here to report this myself. Slightly different but similar scenario, and likely the same root cause. This is a pretty significant gap imho. Exporting default symbols is a very common use case.

In my case: entry point:

export * from './colors';

export { default as ThemePalette } from './ThemePalette';
export * from './darkThemePalette';
export * from './lightThemePalette';

The bottom three work (even the default as ThemePalette, but the index.ts in the ./colors folder has a bunch more default exports, and those all end up broken.

/** colors/index.ts */
export { default as TonalPalette } from './TonalPalette';

export { default as common } from './common';
export { default as aqua } from './aqua';
export { default as blue } from './blue';
export { default as gray } from './gray';
export { default as grey } from './gray';
export { default as green } from './green';
export { default as orange } from './orange';
export { default as purple } from './purple';
export { default as red } from './red';
export { default as yellow } from './yellow';

Those all end up like this in the rolled up .d.ts:

export { default as TonalPalette, default as common, default as aqua, default as blue, default as gray, default as grey, default as green, default as orange, default as purple, default as red, default as yellow, ThemePalette, darkThemePalette, lightThemePalette };
wycats commented 1 year ago

I've hit this too. Is there a workaround?

runspired commented 6 months ago

also hit this, am being told the answer is to abandon this and rollup for vite and vite-plugin-ts 😭

ronny1020 commented 4 months ago

Also, hit this, Is there a walkaround now?

michaelfaith commented 4 months ago

Also, hit this, Is there a walkaround now?

This may not be the most helpful answer, but i ultimately switched to the official TS plugin for rollup: @rollup/plugin-typescript (README). It did everything i needed, and was easy to swap in. This project looks to be abandoned.

ronny1020 commented 4 months ago

I just switched torollup-plugin-dts for the definition files. I still use rollup-plugin-ts for the JS files, though.