Skip to content

Commit

Permalink
Fix bundle bloat due to Apollo Client imports (#307)
Browse files Browse the repository at this point in the history
* Migrate Apollo client to deep entrypoint imports

See: https://www.apollographql.com/docs/react/development-testing/reducing-bundle-size#picking-an-import-style

* Bump version and fix dependencies

* Cleanups

* Add changeset

* Simplify versioning (#309)
  • Loading branch information
rubendinho authored Dec 18, 2024
1 parent 803a4d0 commit d0033cd
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 118 deletions.
8 changes: 8 additions & 0 deletions .changeset/silver-melons-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@quiltt/react": minor
"@quiltt/core": minor
"@quiltt/react-native": minor
---

- Significantly reduce bundle size by migrating @apollo/client to "deep entrypoint import style"
- Bump @apollo/client to v3.11.8
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prebuild": "[[ $CI = true ]] && npx pnpm@9 install --store=node_modules/.pnpm-store || echo skipping pnpm install; turbo run prebuild",
"build": "turbo run build",
"build:packages": "turbo run build --filter='./packages/*'",
"changeset": "pnpm -r exec pnpm run --if-present addVersion && npx changeset",
"changeset": "npx changeset",
"check:packages": "pnpm check-dependency-version-consistency .",
"clean": "turbo run clean",
"coverage": "vitest run --coverage",
Expand All @@ -19,7 +19,7 @@
"publish": "pnpm run build && npx changeset tag && pnpm -r publish packages --no-git-checks --filter=./packages/**",
"test": "turbo run test",
"typecheck": "turbo run typecheck",
"version": "npx changeset version && turbo run addVersion",
"version": "npx changeset version",
"postversion": "pnpm exec pnpm install --filter=./packages/** "
},
"devDependencies": {
Expand All @@ -32,7 +32,6 @@
"@vitejs/plugin-react": "4.3.3",
"@vitest/coverage-v8": "2.0.5",
"check-dependency-version-consistency": "5.0.0",
"genversion": "3.2.0",
"happy-dom": "15.10.2",
"turbo": "2.3.3",
"vite": "5.4.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"typecheck": "tsc --project tsconfig.json --noEmit"
},
"dependencies": {
"@apollo/client": "^3.9.11",
"@apollo/client": "^3.11.8",
"@rails/actioncable": "^7.2.200",
"braces": "^3.0.3",
"cross-fetch": "^4.0.0",
Expand Down
23 changes: 15 additions & 8 deletions packages/core/src/api/graphql/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ApolloClientOptions, Operation } from '@apollo/client/index.js'
import { ApolloClient, ApolloLink } from '@apollo/client/index.js'
import type { ApolloClientOptions, Operation } from '@apollo/client/core'
import { ApolloClient, ApolloLink } from '@apollo/client/core'

import { debugging } from '../../configuration'
import {
Expand Down Expand Up @@ -44,9 +44,16 @@ export class QuilttClient<T> extends ApolloClient<T> {
}
}

export { InMemoryCache, gql, useMutation, useQuery, useSubscription } from '@apollo/client/index.js'
export type {
NormalizedCacheObject,
OperationVariables,
ApolloError,
} from '@apollo/client/index.js'
/**
/* Export Apollo GraphQL assets using deep-imports to prevent unnecessary imports
/* and make tree-shaking more effective
*/

/** Client and Tooling */
export { gql } from '@apollo/client/core'
export { InMemoryCache } from '@apollo/client/cache'
export type { ApolloError, OperationVariables } from '@apollo/client/core'
export type { NormalizedCacheObject } from '@apollo/client/cache'

/** React hooks used by @quiltt/react-native and @quiltt/react */
export { useMutation, useQuery, useSubscription } from '@apollo/client/react/hooks'
6 changes: 4 additions & 2 deletions packages/core/src/api/graphql/links/ActionCableLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FetchResult, NextLink, Operation } from '@apollo/client/core/index.js'
import { ApolloLink, Observable } from '@apollo/client/core/index.js'
import { ApolloLink } from '@apollo/client/core'
import { Observable } from '@apollo/client/utilities'
import type { FetchResult, NextLink, Operation } from '@apollo/client/core'

import { createConsumer } from '@rails/actioncable'
import type { Consumer } from '@rails/actioncable'
import { print } from 'graphql'
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/api/graphql/links/AuthLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FetchResult, NextLink, Observable, Operation } from '@apollo/client/index.js'
import { ApolloLink } from '@apollo/client/index.js'
import { ApolloLink } from '@apollo/client/core'
import type { FetchResult, NextLink, Operation } from '@apollo/client/core'
import type { Observable } from '@apollo/client/utilities'

import { GlobalStorage } from '@/storage'

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/BatchHttpLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BatchHttpLink as ApolloHttpLink } from '@apollo/client/link/batch-http/index.js'
import { BatchHttpLink as ApolloHttpLink } from '@apollo/client/link/batch-http'
import crossfetch from 'cross-fetch'

import { endpointGraphQL } from '@/configuration'
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/api/graphql/links/ErrorLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GlobalStorage } from '@/storage'

import type { ServerError } from '@apollo/client/index.js'
import { onError } from '@apollo/client/link/error/index.js'
import type { ServerError } from '@apollo/client/core'
import { onError } from '@apollo/client/link/error'

export const ErrorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/ForwardableLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloLink } from '@apollo/client/index.js'
import { ApolloLink } from '@apollo/client/core'

export const ForwardableLink = new ApolloLink((operation, forward) => forward(operation))

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/HttpLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpLink as ApolloHttpLink } from '@apollo/client/link/http/index.js'
import { HttpLink as ApolloHttpLink } from '@apollo/client/link/http'
import crossfetch from 'cross-fetch'

// Use `cross-fetch` only if `fetch` is not available on the `globalThis` object
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/RetryLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RetryLink as ApolloRetryLink } from '@apollo/client/link/retry/index.js'
import { RetryLink as ApolloRetryLink } from '@apollo/client/link/retry'

export const RetryLink = new ApolloRetryLink({
attempts: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/TerminatingLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloLink } from '@apollo/client/index.js'
import { ApolloLink } from '@apollo/client/core'

export const TerminatingLink = new ApolloLink(() => null)

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/graphql/links/VersionLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloLink } from '@apollo/client/index.js'
import { ApolloLink } from '@apollo/client/core'

import { version } from '@/configuration'

Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"files": ["dist/**", "src/**", "CHANGELOG.md"],
"scripts": {
"addApiKey": "node scripts/addApiKey.js",
"addVersion": "genversion --esm -f src/version.ts",
"build": "pnpm run addApiKey && pnpm run addVersion && bunchee",
"build": "pnpm run addApiKey && bunchee",
"clean": "rimraf .turbo dist",
"dev": "bunchee --watch",
"lint": "TIMING=1 biome check src/ tests/ --fix",
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// Generated by genversion.
export const version = '3.8.2'
export { version } from '../package.json'
5 changes: 2 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
"types": "./dist/index.d.ts",
"files": ["dist/**", "src/**", "CHANGELOG.md"],
"scripts": {
"addVersion": "genversion --esm -f src/version.ts",
"build": "pnpm run addVersion && bunchee",
"build": "bunchee",
"clean": "rimraf .turbo dist",
"dev": "bunchee --watch",
"lint": "TIMING=1 biome check src/ tests/ --fix",
"typecheck": "tsc --project tsconfig.json --noEmit"
},
"dependencies": {
"@apollo/client": "^3.11.8",
"@quiltt/core": "workspace:*"
},
"devDependencies": {
"@apollo/client": "3.9.11",
"@biomejs/biome": "1.9.4",
"@types/node": "22.10.2",
"@types/react": "18.3.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useQuilttClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useApolloClient } from '@apollo/client/index.js'
import { useApolloClient } from '@apollo/client/react/hooks'

export const useQuilttClient = useApolloClient

Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/providers/QuilttAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import type { FC, PropsWithChildren } from 'react'
import { useEffect, useMemo } from 'react'

import { ApolloProvider } from '@apollo/client'
import { ApolloProvider } from '@apollo/client/react'

import { InMemoryCache, QuilttClient } from '@quiltt/core'

import { useQuilttSession } from '../hooks'

type QuilttAuthProviderProps = PropsWithChildren & {
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// Generated by genversion.
export const version = '3.8.2'
export { version } from '../package.json'
Loading

0 comments on commit d0033cd

Please sign in to comment.