Nested package.json files break tree shaking
See original GitHub issueIntended outcome:
When I import from @apollo/client
or any of its entrypoints (e.g. @apollo/client/react/hoc
), Webpack should be able to correct tree-shake unused exports.
Actual outcome:
Webpack ends up including files that aren’t actually required by the imported values.
This occurs because Webpack uses the closest ancestor package.json
to check for a "sideEffects"
field to determine if tree-shaking is permissible. The root package.json
in @apollo/client
does correctly declare that only a single file in the package has side effects. However, before publishing, ./config/prepareDist.js
creates additional, minimal package.json
files for all of the various entrypoints that the package exposes. These package.json
files do not have a "sideEffects"
field, which means Webpack will refuse to perform tree shaking on them.
I can put together a reproduction if desired by the maintainers, but this is a pretty cut-and-dry bug, and the fix seems relatively straightforward: add "sideEffects"
false to all nested package.json
files. This will break down for the one entrypoint with a file that does have side effects, but I imagine this could either be hardcoded in https://github.com/apollographql/apollo-client/blob/main/config/entryPoints.js, or else done dynamically based the contents of the sideEffect
field in the root package.json
file.
Versions
System:
OS: macOS 10.15.7
Binaries:
Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
Yarn: 1.22.10 - ~/git/universal-roundup-client/node_modules/.bin/yarn
npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm
Browsers:
Chrome: 90.0.4430.93
Edge: 90.0.818.56
Firefox: 88.0
Safari: 14.1
npmPackages:
@apollo/client: ^3.3.16 => 3.3.16
@apollo/react-testing: ^3.1.4 => 3.1.4
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:5 (3 by maintainers)
Top GitHub Comments
Looks great, thanks for the speedy fix! Excited to adopt this once 3.4.0 is released 🚀
@nwalters512 Just published this in
@apollo/client@3.3.18
as well, in case that’s an easier upgrade.