Broken build if a dependency's package.json uses the production export condition
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 11.2k
- Forks
- 899
- Avg merge
- 2d 24m
- Merged PRs (30d)
- 40
Description
Environment
- Node v18.19.0
- Nitro current v2 branch
Reproduction
I've updated one of the nitro examples here to surface the bug: https://github.com/marbemac/nitro/commit/9ecc35e38226aa0757f8f6ffa166f3ec74d29cca
In this example I've added the @zedux/react package, which uses the production conditional in their package.json exports.
- switch to that commit or copy the changes
pnpm icd examples/nano-jsxpnpm buildnode .output/server/index.mjs- open server url in browser, see error in console
Describe the bug
In the example described in the reproduction.
Here is what the original package.json looks like (from node_modules):
{
"name": "@zedux/react",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"production": {
"import": "./dist/zedux-react.es.min.js",
"require": "./dist/zedux-react.umd.min.js",
"default": "./dist/zedux-react.umd.min.js"
},
"default": "./dist/cjs/index.js"
}
}
}
And here is what it looks like in the build .output folder's node_modules:
{
"name": "@zedux/react",
"exports": {
".": {
"import": "./dist/zedux-react.es.min.js",
"require": "./dist/zedux-react.umd.min.js",
"production": {
"import": "./dist/zedux-react.es.min.js",
"require": "./dist/zedux-react.umd.min.js",
"default": "./dist/zedux-react.umd.min.js"
},
"default": "./dist/zedux-react.umd.min.js"
}
}
}
The exports were correctly re-written to all match the production export (tests cover this here -> https://github.com/nitrojs/nitro/blob/v2/test/unit/externals.test.ts). However, note in the screenshot below that only the dist/esm folder is included in the build output's node_modules, perhaps because Nitro ignored the production conditional, and moved files over according to the original "import" export (before it is rewritten)? No idea as I'm not familiar with the Nitro internals, but maybe in the buildEnd step in the externals.ts rollup plugin.
This results in a mismatch, and runtime error when importing the affected package(s).
Additional context
I believe this will affect any projects with dependencies that make use of the production conditional in their package.json exports. Perhaps other conditionals, not sure. Might be related to #2242.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure in examples/nano-jsx with the listed pnpm build and node commands, then inspect the buildEnd step in the externals.ts Rollup plugin. Read test/unit/externals.test.ts and its existing export-rewrite coverage first. Done means dependencies using the production export condition have matching rewritten exports and included files, with the reproduction running without the runtime error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100