unbuild
unbuild copied to clipboard
Unexpected token export declare
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
Additional context
No response
Logs
No response
Please provide a reproduction 🙏🏼
Please provide a reproduction 🙏🏼
sorry, I lost my env and cannot reproduce. I'll keep an eye on this.
@Shana-AE Please reopen, I have reproduced it @pi0 https://github.com/wkeylin/unbuild-demo
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)
I'm having the same issue.
I'm having the same issue.
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 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 };
@Shana-AE Please reopen, I have reproduced it @pi0 https://github.com/wkeylin/unbuild-demo
This reproduction contains ~~two issues~~ (now 3 issues 😅):
- 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
- 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 🕛)
- (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.