From 76196162f6d1443e3810a3d7fc67303a9eb13c0d Mon Sep 17 00:00:00 2001 From: ajth-in Date: Thu, 29 Oct 2026 23:04:14 +0530 Subject: [PATCH] fix: avoid bundling package.json in output --- next-cloudinary/babel.config.js | 30 +++++++++++++++++----- next-cloudinary/src/constants/analytics.ts | 10 +++++--- next-cloudinary/vitest.config.mts | 9 +++++++ 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 next-cloudinary/vitest.config.mts diff --git a/next-cloudinary/babel.config.js b/next-cloudinary/babel.config.js index 4fa8bb05..e46847e1 100644 --- a/next-cloudinary/babel.config.js +++ b/next-cloudinary/babel.config.js @@ -1,14 +1,30 @@ -module.exports = api => { - const presets = [] +const { version } = require("./package.json"); - if ( api.env('test') ) { - presets.push([['@babel/preset-env', {targets: {node: 'current'}}]]); +module.exports = (api) => { + const presets = []; + + if (api.env("test")) { + presets.push([["@babel/preset-env", { targets: { node: "current" } }]]); } - presets.push(['@babel/preset-react', { runtime: 'automatic' }]); - presets.push(['@babel/preset-typescript']); + presets.push(["@babel/preset-react", { runtime: "automatic" }]); + presets.push(["@babel/preset-typescript"]); return { - presets + presets, + plugins: [ + // Inline the version used by src/constants/analytics.ts at build time instead of bundling package.json. + function inlineNextCloudinaryVersion() { + return { + name: "inline-next-cloudinary-version", + visitor: { + ReferencedIdentifier(path) { + if (path.node.name !== "__NEXT_CLOUDINARY_VERSION__") return; + path.replaceWith({ type: "StringLiteral", value: version }); + }, + }, + }; + }, + ], }; }; diff --git a/next-cloudinary/src/constants/analytics.ts b/next-cloudinary/src/constants/analytics.ts index cbcaa9e9..0e8f81c0 100644 --- a/next-cloudinary/src/constants/analytics.ts +++ b/next-cloudinary/src/constants/analytics.ts @@ -1,5 +1,7 @@ -import nextPkg from 'next/package.json'; -import pkg from '../../package.json'; +import { version as nextVersion } from 'next/package.json'; + +// Injected at build time to avoid bundling this package's package.json +declare const __NEXT_CLOUDINARY_VERSION__: string; // Detect if this project was created via create-cloudinary-react CLI (Integrations) // copied from https://github.com/cloudinary/frontend-frameworks/commit/00a8eb2b133c772fb6f54d840eba0012286b70b2#diff-9bc0325d4ce5c8af9b3be68a361f186552b51da8274a7a2f214f7deaa9b245af @@ -18,8 +20,8 @@ const isCLIDetected = isCLI(); // and use sdk code 'I' ('Next CLI' integration) instead of 'V' (Next.js sdk) export const NEXT_CLOUDINARY_ANALYTICS_PRODUCT_ID = (isCLIDetected ? 'B' : 'A'); export const NEXT_CLOUDINARY_ANALYTICS_ID = (isCLIDetected ? 'I' : 'V'); -export const NEXT_VERSION = normalizeVersion(nextPkg.version); -export const NEXT_CLOUDINARY_VERSION = normalizeVersion(pkg.version); +export const NEXT_VERSION = normalizeVersion(nextVersion); +export const NEXT_CLOUDINARY_VERSION = normalizeVersion(__NEXT_CLOUDINARY_VERSION__); function normalizeVersion(version: string) { let normalized = version; diff --git a/next-cloudinary/vitest.config.mts b/next-cloudinary/vitest.config.mts new file mode 100644 index 00000000..2a1bcf73 --- /dev/null +++ b/next-cloudinary/vitest.config.mts @@ -0,0 +1,9 @@ +import { defineConfig } from 'vitest/config'; +import pkg from './package.json'; + +export default defineConfig({ + // Mirror the Babel replacement for src/constants/analytics.ts during tests + define: { + __NEXT_CLOUDINARY_VERSION__: JSON.stringify(pkg.version), + }, +});