Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Shrink apollo-link[-http-common] packages. #969

Merged
merged 4 commits into from
Mar 5, 2019
Merged

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Mar 5, 2019

As apollographql/react-apollo#2839 has helped to demonstrate, sharing dependencies between Apollo packages is a great way to improve overall bundle size, especially if the shared dependencies can be tree-shaken to remove unused exports.

This PR includes two changes to the apollo-link package, which is used directly or indirectly by any application that uses any of the packages in this repository:

  1. Import getOperationName from apollo-utilities, since that implementation is almost certainly already imported by apollo-client.

  2. Use invariant.warn and InvariantError instead of console.warn and Error, since those utilities allow long error strings to be stripped in production, thanks to https://github.com/apollographql/invariant-packages.

Together, these tweaks reduce the minified+gzip size of the apollo-link package from 1271 bytes to 1123 bytes, a 12% improvement.

Since every application that uses any links uses both the apollo-link
package and the apollo-client package, and apollo-client already imports
getOperationName from apollo-utilities, we can avoid duplicating that code
in apollo-link.

This only saves 37 bytes after gzip (84 bytes minified), but that's not
nothing.
This saves 93 bytes after gzip (161 bytes minified).
@codecov-io
Copy link

codecov-io commented Mar 5, 2019

Codecov Report

Merging #969 into master will not change coverage.
The diff coverage is 90.9%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #969   +/-   ##
=======================================
  Coverage   95.23%   95.23%           
=======================================
  Files          22       22           
  Lines        1028     1028           
  Branches      109      107    -2     
=======================================
  Hits          979      979           
  Misses         44       44           
  Partials        5        5
Impacted Files Coverage Δ
packages/apollo-link-http-common/src/index.ts 91.66% <100%> (+0.14%) ⬆️
packages/apollo-link/src/linkUtils.ts 98% <100%> (-0.08%) ⬇️
packages/apollo-link/src/link.ts 90.56% <66.66%> (+0.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ecd543...fbae453. Read the comment docs.

@@ -70,6 +77,9 @@ export default name => [
},
},
}),
invariantPlugin({
errorCodes: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See apollographql/apollo-client#4521 for more explanation of what this option does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually a really nice solution for having some errors in production. That rollup plugin is really smart

package.json Outdated Show resolved Hide resolved
packages/apollo-link/package.json Show resolved Hide resolved
This reduces the minified+gzip byte size of the package from 1227 to 1045,
a 15% improvement.
@benjamn benjamn changed the title Shrink apollo-link package. Shrink apollo-link[-http-common] packages. Mar 5, 2019
@JoviDeCroock
Copy link
Contributor

I'll probably try to find some low-hanging in link-packages today, so that might further reduce the bundlesize.

Copy link
Contributor

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really promising, I think the apollo repositories are really on the right track when it comes to bundle size. Good job @benjamn

@benjamn benjamn merged commit ef1c2d1 into master Mar 5, 2019
@benjamn benjamn deleted the shrink-apollo-link branch March 5, 2019 20:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants