developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
7.99k stars 361 forks source link

Bundled type definitions for bundled dependencies? #1050

Open donmccurdy opened 1 year ago

donmccurdy commented 1 year ago

Context: As documented in #454 and How Microbundle decides which dependencies to bundle, one recommended way to bundle dependencies is to put the dependency in devDependencies rather than dependencies.

Problem: my-bundled-dependency is bundled into my-parent-library, and affects its exported type definitions. Recently a downstream user of my package reported an issue that surprised me – while the JavaScript is bundled as expected, the type definitions are not bundled, and contain an import from my-bundled-dependency. This causes builds to fail for users of my-parent-library if they've enabled TypeScript's skipLibCheck: false. If this is unexpected and a simple repro example would be helpful, I'm happy to create that.

Alternatives: Should the type definitions also be bundled by Microbundle? By TypeScript? Am I perhaps making a mistake by using a bundled dependency in my exported TypeScript API? I'm inclined to the idea that this is something TypeScript, not Microbundle, should ideally handle. But they've historically been pretty unenthusiastic about such requests.

Related:

rschristian commented 1 year ago

Indeed, and it will also cause issues if my-bundled-dependency's types are exposed in some way, as at best, they'll just be an any.

Its tough, as we don't really want to go too far off the TS reservation and have to deal with bundling .d.ts files ourselves, but it doesn't look like TS will address it either.

donmccurdy commented 1 year ago

Understood, and thanks for confirming! In this case I can remove my-bundled-dependency from the exported API of my-parent-library, working around the issue. Glad to defer to you on whether to close or continue tracking this ticket.

rschristian commented 1 year ago

Alternatively, you may be able to post-build copy the .d.ts files from the modules that you have inlined into your own dist/ folder or whatever. I get it's far from ideal, but could work.

We'll definitely keep this open, I don't think we have any other issues tracking it and it's a very valid pain point.

ScottAwesome commented 1 year ago

Have a look at API Extractor which can bundle types (uses rollup under the hood). Its maintained by Microsoft. I've used in the past. It works pretty well. Its not perfect, but it is really smooth most of the time.

I wish all library maintainers used it to package up their types with their outputs. It does a really good job of making sure your types match what you're exporting as a consumable API

donmccurdy commented 1 year ago

Thanks @ScottAwesome! I've used API Extractor elsewhere, though using it to bundle external type definitions hadn't occurred to me. It's been an effort to set up and run in the past — not the "zero configuration" appeal of Microbundle — so I don't feel it's something I can include in my builds as a library maintainer. I deeply wish TypeScript would reconsider their position on bundling, but that's another matter.

I am keeping an eye on dts-buddy as well, though it's still experimental at this point.