Hello, I'm looking for guidance on how to use vitest with tsdown. I have an API binding library that is working when compiled using tsdown + tsgo. However, I read that vitest uses vite, which has a separate configuration format. I tried to switch to using rolldown-vite + rolldown-dts-plugin + tsgo, but I can't get it to generate the correct typings. With vite, it doesn't generate imported file typings, and the entry-point typing file is empty. tsdown gets this correct out of the box, so I know this is possible. How do I debug this issue? I don't want to give up using rolldown or tsgo if possible.
#tsdown vs vite-rolldown with vitest
14 messages · Page 1 of 1 (latest)
what exactly do you mean by "with vitest"?
You might need to set up your test env config but that should be independent of how your build step can look like
I guess my confusion is: vitest using the tsdown built code or is it trying to rebuild it using vite?
likely it will try using vite under the hood. But it won't "build it" and instead run the code (which is what matters)
my problem is that I couldn't get a vite config that would replicate my tsdown results, so I couldn't be sure they'd be the same
this is generated code, so there's a lot of typescript stuff in there that needs to transpile properly
Mostly enums and such
that should still be supported with either tool
do you have a repro by any chance?
I'm using unplugin-dts which also supports Rolldown: https://github.com/qmhc/unplugin-dts
I don't recommend building your library with Vite, but if you have to, you'll have to use Library Mode, which is a bit of a neglected feature intended for browser-facing libraries. Also, if it's an ESM-only library and you're using Vite v8, you'll have to use esmExternalRequirePlugin to properly externalize modules from node_modules, if that's your goal. https://main.vite.dev/guide/migration#require-calls-for-externalized-modules
I haven't used uplugin-dts with Rolldown yet, but in order to get satisfactory types with Rollup Vite I had to enable bundleTypes in the plugin configuration and generally fiddle a bit with the configuration to get things right. Dual publish was messy AF, I hope you don't have to do that, but it was possible. Not officially, though.
As Alex said, it's best if you share what you have.
What you can try to do is build your library with tsdown and import modules from the build directory in your tests, i.e. your build library, as sort an end-to-end test. And you can configure Vitest to use an environment variable to flip between testing source code and testing build code via import alias. That's what I would try to do in your place.
ok, so my current solution is to run unbuilt locally with vite/vitest, with a customCondition pointing to the source, and running tsgo as a type-checker, and rolldown at the end for prod builds.
This seems pretty clunky, but the biome import check lint rule is buggy, and tsdown doesn't actually check imports
Would love a nicer solution if anyone could think of one
What is unbuilt, and what is customCondition?
Seems like you can't really share your situation, you can mostly talk about solutions, and that just leaves me guessing what you might be talking about. If you manage to come up with a repro, you'll increase your chances of getting better help.