Skip to content

fix: avoid bundling package.json in output - #672

Open
ajth-in wants to merge 1 commit into
cloudinary-community:mainfrom
ajth-in:fix/avoid-bundling-package-json
Open

ajth-in wants to merge 1 commit into
cloudinary-community:mainfrom
ajth-in:fix/avoid-bundling-package-json

Conversation

@ajth-in

@ajth-in ajth-in commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

This is an optimization that reduces the client bundle size by ~1.1 KB by no longer bundling entire package.json files just to read version numbers used for analytics.

getCloudinaryAnalytics in next-cloudinary/src/lib/cloudinary.ts:70 attaches SDK/tech versions to every generated Cloudinary URL:

export function getCloudinaryAnalytics(analytics?: AnalyticsOptions) {
  return Object.assign({
    product: NEXT_CLOUDINARY_ANALYTICS_PRODUCT_ID,
    sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
    sdkSemver: NEXT_CLOUDINARY_VERSION,
    techVersion: NEXT_VERSION,
    feature: ''
  }, analytics)
}

Those constants come from next-cloudinary/src/constants/analytics.ts, which imported both:

  • next/package.json (the peer dependency), and
  • ../../package.json (this package).
  1. Next.js version: the entire next/package.json from the consumer's peer dependency gets pulled into the bundle.
before-fix
  1. next-cloudinary version: the entire package.json was inlined as a JS object inside dist/index.js
before-fix-index

This PR mainly fixes the second issue (Fixes #671)

Changes

  • Replaced import pkg from '../../package.json' with a build-time constant, NEXT_CLOUDINARY_VERSION, injected via tsup's define option. Only the version string is now emitted instead of the whole manifest (including scripts, devDependencies, repository, etc.).
  • Switched the Next.js import to a named import (import { version as nextVersion } from 'next/package.json') so bundlers that support JSON tree-shaking can drop unused fields.
  • Added vitest.config.ts with the same define so the unit tests continue to resolve NEXT_CLOUDINARY_VERSION.

Result

dist/index.mjs size is reduced as shown in the comparison below

image

the dist/index.js file before and after the change

index.js
index.js

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Fix or improve the documentation
  • This change requires a documentation update

Checklist

  • I have followed the contributing guidelines of this project as mentioned in CONTRIBUTING.md
  • I have created an issue ticket for this PR
  • I have checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of my own code
  • I have run tests locally to ensure they all pass
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes needed to the documentation

@vercel

vercel Bot commented Oct 21, 2026

Copy link
Copy Markdown

@ajth-in is attempting to deploy a commit to the Cloudinary DevX Team on Vercel.

A member of the Team first needs to authorize it.

@ajth-in
ajth-in force-pushed the fix/avoid-bundling-package-json branch from 9170e0c to 932a9c5 Compare October 21, 2026 18:08
@ajth-in

ajth-in commented Oct 22, 2026 •

Copy link
Copy Markdown
Author

Another way I came across for getting the Next.js version is window.next.version, which Next.js exposes on the client. This avoids reading next/package.json directly, but since it doesnt work in server I dont think we can use it here.
I couldn't find an equivalent built-in way to access the version on the server, so reading it from the package metadata seemed like the most straightforward approach.
Let me know if you have any other ideas.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] bundling package.json in output

1 participant