I'm building some code in src
to dist. I want any top-level files (dist/*.mjs
) and any second-level files (dist/*/index.mjs
) to be exports:
"exports": {
".": {
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./*": {
"import": "./dist/*.mjs",
"types": "./dist/*.d.ts"
},
"./*": {
"import": "./dist/*/index.mjs",
"types": "./dist/*/index.d.ts"
},
}
The last item for obvious reasons (can't have two identical keys in JSON) does not work correctly. I can get one or the other working, but I'd like it to try one and fallback to the next.
In desperation I tried using an array there but it didn't work. Is there a strategy I can use here that will make this feasible? The build-tooling I am currently stuck with (tsup) doesn't seem to be able to build src/foo.ts
into src/foo/index.mjs
and I'd rather not write a post-processor to move them around, but I will if I have to.
Unfortunately, this is not possible with Node.js.
Node.js will use at most one condition for each export, and each condition must have exactly one path.
(In case you are using Webpack, you can use an array: package exports alternatives.)
My suggestion is manually or automatically adding the index files:
{
"exports": {
".": {
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./*": {
"import": "./dist/*.mjs",
"types": "./dist/*.d.ts"
},
"./example": {
"import": "./dist/example/index.mjs",
"types": "./dist/example.d.ts"
}
}