privatenumber / pkgroll

📦 Zero-config package bundler for Node.js + TypeScript
MIT License
1.01k stars 23 forks source link

Cyclic dependency when using dynamic imports #65

Closed lirc571 closed 2 months ago

lirc571 commented 2 months ago

Problem

I have a TypeScript project with the following dependency graph (server.ts dynamically imports subprocess.ts):

index.ts  =>  server.ts  =dynamic=>  subprocess.ts  =>  log.ts

It is bundled into the following by pkgroll:

index.mjs  =>  index-CbJ.mjs  =dynamic=>  subprocess-D4M.mjs =>  index-Cbj.mjs

Somehow index.ts and log.ts are bundled into index-Cbj.mjs, which creates a cyclic dependency. index.mjs contains only a list of top-level imports.

When I run it with Node.js, it exits with code 13 (Unsettled Top-Level Await).

The same code runs successfully with tsx.

I am not sure what the root cause is, but it is very strange to bundle the entry file index.ts with some other exports because that will certainly cause cyclic dependency, similar to https://github.com/nodejs/node/issues/44601#issue-1368994246.

Expected behavior

Bundling should not create cyclic dependencies.

The entrypoint should not be merged with exports.

Minimal reproduction URL

https://github.com/lirc571/pkgroll-65

Version

v2.0.2

Node.js version

v20.12.2

Package manager

pnpm

Operating system

Linux

Contributions

privatenumber commented 2 months ago

Thanks for the issue. Usually, looking at the minimal repro is the fastest way for me to understand what the problem is, validate it, and debug it. LMK when you have that ready and we can re-open this.

If you need help, you can sponsor me and join my private org and I can also help you out there.

lirc571 commented 2 months ago

@privatenumber I have created a repo at https://github.com/lirc571/pkgroll-65. Bundled output at /dist included.

lirc571 commented 4 weeks ago

Switched to tsup which works out of the box and is actively maintained.

privatenumber commented 4 weeks ago

pkgroll is a wrapper around Rollup so any bundling issue should be filed there. The reason it works in tsup is because of esbuild, not tsup.

Switched to tsup which works out of the box and is actively maintained.

pkgroll is actively maintained. You're just not entitled to free support.

lirc571 commented 4 weeks ago

Thank you! That's perfectly understandable!

I just wanted to provide my solution here to wrap up this issue so someone experiencing similar issues would know what to do.