4 ms·
I've been trying to upgrade a couple of libraries we use internally at work to output CJS + ESM, from a typescript project, and have the output be 1-to-1 files
by c-hendricks 3y ago
I've been trying to upgrade a couple of libraries we use internally at work to output CJS + ESM, from a typescript project, and have the output be 1-to-1 files (so, no bundling / rollup / whatever you want to call it).
What a frustrating experience it has been.
Using unbuild "works", but
- For the life of me, I can't get unbuild to generate `.d.mts` files when my source files don't have `.mts` extensions. Luckily, when the library is used in a downstream project and a `.mjs` file is imported, TypeScript properly loads the `.d.ts` file anyways
- When it comes to a downstream project, TypeScript doesn't seem to work with export maps. People say it does, but maybe because I don't have `.d.mts` files TypeScript is saying it can't find type information? It _does_ work with simple export maps, but if I say `'./': './dist/esm/'` so the downstream project doesn't have to manually import from `dist/esm` I see the issue
- Using the "esm" module / moduleResolutions + converting my files to `.mts` + changing imports in the source to import the non-existant `.mjs` files results in a CJS bundle that tries to require a `.mjs` file, which unbuild has built with a `.js` extension, so it throws an error because it can't find the file.
- For some reason unbuild mucks with the hashbang line in my _CJS_ `bin` script, the ESM one it doesn't touch
Ugh.
- moltar 3y agoTry tsup it does just work unless your package is doing something funny. Then do a check with attw cli tool and it’ll report your compat mistakes with links to explainer docs.
- c-hendricks 3y agoThat's what I tried using before and I had even less luck / had to do more fandangling, but thanks for the suggestion. Like I said, these are libraries whose "build" before was just `tsc`, there's nothing funny going on.
- moltar 3y agoShare a repro I’ll sort you out. Gotten good at this bullshit :D