unbuild icon indicating copy to clipboard operation
unbuild copied to clipboard

Unexpected token export declare

Open Shana-AE opened this issue 2 years ago • 9 comments
trafficstars

Environment

ubuild v2.0.0 node 18 typescript 5.2.2 vue3.3.4

Reproduction

build a project that import vue3.3.4 types

Describe the bug

when I build, I got image image

Additional context

No response

Logs

No response

Shana-AE avatar Sep 25 '23 06:09 Shana-AE

Please provide a reproduction 🙏🏼

pi0 avatar Sep 26 '23 13:09 pi0

Please provide a reproduction 🙏🏼

sorry, I lost my env and cannot reproduce. I'll keep an eye on this.

Shana-AE avatar Sep 28 '23 07:09 Shana-AE

@Shana-AE Please reopen, I have reproduced it @pi0 https://github.com/wkeylin/unbuild-demo

wkeylin avatar Apr 13 '24 02:04 wkeylin

related #303 Can still be reproduced in [email protected]. The problem can be located here: https://github.com/unjs/unbuild/blob/3b5ec7e3ba830b392af7fb116ff79f4c1709009b/src/builder/rollup.ts#L196 If the dts plugin is moved before other plugins, it can run properly. But I'm not sure which plugin specifically conflicts with dts (@rollup/plugin-node-resolve or @rollup/plugin-commonjs)

Bernankez avatar Apr 17 '24 01:04 Bernankez

I'm having the same issue.

masterT avatar Apr 17 '24 15:04 masterT

I'm having the same issue.

Eddie344 avatar Oct 10 '24 14:10 Eddie344

Is there any workaround at this point? I can repro by adding debug (to any project that otherwise compiles nicely with unbuild) to the dependencies and @types/debug to the devDependencies. Any export of this module's usage then causes the problem. Mere use (IOW, omitting the export {a}) does not.

import * as debug from "debug"

const a = debug.debug("horst");
export { a }

The offending line in @types/debug starts with declare var, the token that seems to cause the SyntaxError is var.

jan-tee avatar Dec 09 '24 08:12 jan-tee

@jan-tee can you reproduce with the latest unbuild? I tried your example code, and it's working fine.

The output index.d.ts:

import * as debug from 'debug';

declare const a: debug.Debugger;

export { a };

kricsleo avatar Feb 08 '25 04:02 kricsleo

@Shana-AE Please reopen, I have reproduced it @pi0 https://github.com/wkeylin/unbuild-demo

This reproduction contains ~~two issues~~ (now 3 issues 😅):

  1. Plugin conflicts, the rollup-plugin-dts vs the @rollup/plugin-commonjs.

This causes following error:

[12:18:12 PM]  ERROR  Expected '{', got 'declare' in /.../node_modules/.pnpm/[email protected][email protected]/node_modules/vue/dist/vue.d.ts

And should be resolved by the pr #495

  1. The rollup-plugin-dts does not support "non-modular type import analysis".

I don't know how to accurately describe it, but you can refer to this issue: https://github.com/microsoft/TypeScript/issues/57764 and specially this comment: https://github.com/microsoft/TypeScript/issues/57764#issuecomment-1995320147

Once the first issue is resolved, this second error will arise:

 RollupError: "DefaultData" is not exported by "node_modules/.pnpm/[email protected]/node_modules/vue/types/options.d.ts", imported by "node_modules/.pnpm/[email protected]/node_modules/vue/types/umd.d.ts".
...

And the minimal reproduction: https://stackblitz.com/edit/stackblitz-starters-cpdq49tq?file=src%2Findex.ts

~~I think this should be supported by rollup-plugin-dts.~~ Update issue 2: upstream issue: https://github.com/Swatinem/rollup-plugin-dts/issues/334 and related PR: https://github.com/Swatinem/rollup-plugin-dts/pull/335 (merged but not released yet 🕛)

  1. (Added) The rollup-plugin-dts does not support "named imports from namespace equals exports" feature. The types of Vue 2 is using this for backward compatibility, https://github.com/vuejs/vue/blob/9e88707940088cb1f4cd7dd210c9168a50dc347c/types/umd.d.ts#L10-L11 and it's hard to support in the rollup-plugin-dts (I think).

Therefore, it is advisable to avoid bundling Vue's types and instead externalize them.

kricsleo avatar Feb 08 '25 07:02 kricsleo