next-cloudinary 2.1.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,400 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
52
+
53
+ // src/components/CldImage/CldImage.tsx
54
+ import { useState } from "react";
55
+ import Image from "next/image";
56
+ import { getTransformations } from "@cloudinary-util/util";
57
+ import { transformationPlugins } from "@cloudinary-util/url-loader";
58
+
59
+ // src/lib/cloudinary.ts
60
+ function pollForProcessingImage(options) {
61
+ return __async(this, null, function* () {
62
+ const { src } = options;
63
+ try {
64
+ yield new Promise((resolve, reject) => {
65
+ fetch(src).then((res) => {
66
+ if (!res.ok) {
67
+ reject(res);
68
+ return;
69
+ }
70
+ resolve(res);
71
+ });
72
+ });
73
+ } catch (e) {
74
+ if (e.status === 423) {
75
+ return yield pollForProcessingImage(options);
76
+ }
77
+ return false;
78
+ }
79
+ return true;
80
+ });
81
+ }
82
+
83
+ // src/loaders/cloudinary-loader.ts
84
+ import { constructCloudinaryUrl } from "@cloudinary-util/url-loader";
85
+
86
+ // src/constants/analytics.ts
87
+ import nextPkg from "next/package.json";
88
+
89
+ // package.json
90
+ var package_default = {
91
+ name: "next-cloudinary",
92
+ version: "2.3.0",
93
+ main: "./dist/index.js",
94
+ module: "./dist/index.mjs",
95
+ types: "./dist/index.d.ts",
96
+ source: "src/index.ts",
97
+ scripts: {
98
+ build: "tsup src/index.ts --format esm,cjs --dts --external react",
99
+ dev: "tsup src/index.ts --format esm,cjs --watch --dts --external react",
100
+ prepublishOnly: "cp ../README.md . && yarn build",
101
+ postpublish: "rm ./README.md",
102
+ test: "jest"
103
+ },
104
+ dependencies: {
105
+ "@cloudinary-util/url-loader": "^1.3.0",
106
+ "@cloudinary-util/util": "^1.2.0",
107
+ "@cloudinary/url-gen": "^1.8.6"
108
+ },
109
+ devDependencies: {
110
+ "@babel/core": "^7.19.6",
111
+ "@babel/preset-env": "^7.19.4",
112
+ "@types/jest": "^29.2.0",
113
+ "@types/react": "^18.0.28",
114
+ "@types/react-dom": "^18.0.11",
115
+ "babel-jest": "^29.2.2",
116
+ dotenv: "^16.0.3",
117
+ jest: "^29.2.2",
118
+ "jest-environment-jsdom": "^29.2.2",
119
+ "ts-jest": "^29.0.3",
120
+ tsup: "^6.6.3",
121
+ typescript: "^4.9.4"
122
+ },
123
+ peerDependencies: {
124
+ next: "^12 || ^13",
125
+ react: "^17 || ^18"
126
+ }
127
+ };
128
+
129
+ // src/constants/analytics.ts
130
+ var NEXT_CLOUDINARY_ANALYTICS_ID = "V";
131
+ var NEXT_CLOUDINARY_VERSION = package_default.version;
132
+ var NEXT_VERSION = nextPkg.version;
133
+
134
+ // src/loaders/cloudinary-loader.ts
135
+ function cloudinaryLoader({ loaderOptions, imageProps, cldOptions, cldConfig = {} }) {
136
+ const options = __spreadValues(__spreadValues({}, imageProps), cldOptions);
137
+ options.width = typeof options.width === "string" ? parseInt(options.width) : options.width;
138
+ options.height = typeof options.height === "string" ? parseInt(options.height) : options.height;
139
+ if (typeof (loaderOptions == null ? void 0 : loaderOptions.width) === "number" && typeof options.width === "number" && loaderOptions.width !== options.width) {
140
+ const multiplier = loaderOptions.width / options.width;
141
+ options.widthResize = loaderOptions.width;
142
+ if (typeof options.height === "number") {
143
+ options.heightResize = Math.round(options.height * multiplier);
144
+ }
145
+ }
146
+ return constructCloudinaryUrl({
147
+ // @ts-expect-error
148
+ options,
149
+ config: __spreadValues({
150
+ cloud: {
151
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
152
+ }
153
+ }, cldConfig),
154
+ analytics: {
155
+ sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
156
+ sdkSemver: NEXT_CLOUDINARY_VERSION,
157
+ techVersion: NEXT_VERSION,
158
+ feature: ""
159
+ }
160
+ });
161
+ }
162
+
163
+ // src/components/CldImage/CldImage.tsx
164
+ import { jsx } from "react/jsx-runtime";
165
+ "use client";
166
+ var CldImage = (props) => {
167
+ const CLD_OPTIONS = [
168
+ "deliveryType",
169
+ "preserveTransformations"
170
+ ];
171
+ transformationPlugins.forEach(({ props: props2 = [] }) => {
172
+ props2.forEach((prop) => {
173
+ if (CLD_OPTIONS.includes(prop)) {
174
+ throw new Error(`Option ${prop} already exists!`);
175
+ }
176
+ CLD_OPTIONS.push(prop);
177
+ });
178
+ });
179
+ const imageProps = {
180
+ alt: props.alt,
181
+ src: props.src
182
+ };
183
+ Object.keys(props).filter((key) => !CLD_OPTIONS.includes(key)).forEach((key) => imageProps[key] = props[key]);
184
+ const defaultImgKey = Object.keys(imageProps).map((key) => `${key}:${imageProps[key]}`).join(";");
185
+ const [imgKey, setImgKey] = useState(defaultImgKey);
186
+ const cldOptions = {};
187
+ CLD_OPTIONS.forEach((key) => {
188
+ if (props[key]) {
189
+ cldOptions[key] = props[key] || void 0;
190
+ }
191
+ });
192
+ if (props.preserveTransformations) {
193
+ try {
194
+ const transformations = getTransformations(props.src);
195
+ cldOptions.rawTransformations = [...transformations.flat(), ...props.rawTransformations || []];
196
+ } catch (e) {
197
+ console.warn(`Failed to preserve transformations: ${e.message}`);
198
+ }
199
+ }
200
+ function handleOnError(options) {
201
+ return __async(this, null, function* () {
202
+ const result = yield pollForProcessingImage({ src: options.target.src });
203
+ if (result) {
204
+ setImgKey(`${defaultImgKey};${Date.now()}`);
205
+ }
206
+ });
207
+ }
208
+ return /* @__PURE__ */ jsx(
209
+ Image,
210
+ __spreadProps(__spreadValues({}, imageProps), {
211
+ loader: (loaderOptions) => cloudinaryLoader({ loaderOptions, imageProps, cldOptions }),
212
+ onError: handleOnError
213
+ }),
214
+ imgKey
215
+ );
216
+ };
217
+ var CldImage_default = CldImage;
218
+
219
+ // src/components/CldOgImage/CldOgImage.tsx
220
+ import Head from "next/head";
221
+ import { constructCloudinaryUrl as constructCloudinaryUrl2 } from "@cloudinary-util/url-loader";
222
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
223
+ var IMAGE_WIDTH = 2400;
224
+ var IMAGE_HEIGHT = 1200;
225
+ var TWITTER_CARD = "summary_large_image";
226
+ var CldOgImage = (_a) => {
227
+ var _b = _a, { excludeTags = [], twitterTitle } = _b, props = __objRest(_b, ["excludeTags", "twitterTitle"]);
228
+ const options = __spreadProps(__spreadValues({}, props), {
229
+ alt: props.alt,
230
+ crop: props.crop || "fill",
231
+ gravity: props.gravity || "center",
232
+ height: props.height || IMAGE_HEIGHT,
233
+ src: props.src,
234
+ width: props.width || IMAGE_WIDTH
235
+ });
236
+ const ogImageUrl = constructCloudinaryUrl2({
237
+ options,
238
+ config: {
239
+ cloud: {
240
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
241
+ }
242
+ },
243
+ analytics: {
244
+ sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
245
+ sdkSemver: NEXT_CLOUDINARY_VERSION,
246
+ techVersion: NEXT_VERSION,
247
+ feature: ""
248
+ }
249
+ });
250
+ return /* @__PURE__ */ jsxs(Head, { children: [
251
+ /* @__PURE__ */ jsx2("meta", { property: "og:image", content: ogImageUrl }),
252
+ /* @__PURE__ */ jsx2("meta", { property: "og:image:secure_url", content: ogImageUrl }),
253
+ /* @__PURE__ */ jsx2("meta", { property: "og:image:width", content: `${options.width}` }),
254
+ /* @__PURE__ */ jsx2("meta", { property: "og:image:height", content: `${options.height}` }),
255
+ options.alt && /* @__PURE__ */ jsx2("meta", { property: "og:image:alt", content: options.alt }),
256
+ !excludeTags.includes("twitter:title") && /* @__PURE__ */ jsx2("meta", { property: "twitter:title", content: twitterTitle || " " }),
257
+ /* @__PURE__ */ jsx2("meta", { property: "twitter:card", content: TWITTER_CARD }),
258
+ /* @__PURE__ */ jsx2("meta", { property: "twitter:image", content: ogImageUrl })
259
+ ] });
260
+ };
261
+ var CldOgImage_default = CldOgImage;
262
+
263
+ // src/components/CldUploadWidget/CldUploadWidget.tsx
264
+ import Script from "next/script";
265
+
266
+ // src/lib/util.ts
267
+ function triggerOnIdle(callback) {
268
+ if (window && "requestIdleCallback" in window) {
269
+ return requestIdleCallback(callback);
270
+ }
271
+ return setTimeout(() => callback(), 1);
272
+ }
273
+
274
+ // src/components/CldUploadWidget/CldUploadWidget.tsx
275
+ import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
276
+ var cloudinary;
277
+ var widget;
278
+ var CldUploadWidget = ({
279
+ children,
280
+ onUpload,
281
+ options,
282
+ signatureEndpoint,
283
+ uploadPreset
284
+ }) => {
285
+ const signed = !!signatureEndpoint;
286
+ function handleOnLoad() {
287
+ if (!cloudinary) {
288
+ cloudinary = window.cloudinary;
289
+ }
290
+ triggerOnIdle(() => {
291
+ if (!widget) {
292
+ widget = createWidget();
293
+ }
294
+ });
295
+ }
296
+ function generateSignature(callback, paramsToSign) {
297
+ if (typeof signatureEndpoint === "undefined") {
298
+ throw Error("Failed to generate signature: signatureEndpoint undefined.");
299
+ }
300
+ fetch(signatureEndpoint, {
301
+ method: "POST",
302
+ body: JSON.stringify({
303
+ paramsToSign
304
+ })
305
+ }).then((r) => r.json()).then(({ signature }) => {
306
+ callback(signature);
307
+ });
308
+ }
309
+ function createWidget() {
310
+ const uploadOptions = __spreadValues({
311
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
312
+ uploadPreset: uploadPreset || process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET,
313
+ apiKey: process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY
314
+ }, options);
315
+ if (signed) {
316
+ uploadOptions.uploadSignature = generateSignature;
317
+ if (!uploadOptions.apiKey) {
318
+ return new Error("Signed Upload needs apiKey!");
319
+ }
320
+ }
321
+ return cloudinary == null ? void 0 : cloudinary.createUploadWidget(
322
+ uploadOptions,
323
+ function(error, result) {
324
+ if ((error || result.event === "success") && typeof onUpload === "function") {
325
+ onUpload(error, result, widget);
326
+ }
327
+ }
328
+ );
329
+ }
330
+ function open() {
331
+ if (!widget) {
332
+ widget = createWidget();
333
+ }
334
+ widget && widget.open();
335
+ }
336
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
337
+ typeof children === "function" && children({
338
+ cloudinary,
339
+ widget,
340
+ open
341
+ }),
342
+ /* @__PURE__ */ jsx3(
343
+ Script,
344
+ {
345
+ id: `cloudinary-${Math.floor(Math.random() * 100)}`,
346
+ src: "https://widget.cloudinary.com/v2.0/global/all.js",
347
+ onLoad: handleOnLoad,
348
+ onError: (e) => console.error(`Failed to load Cloudinary: ${e.message}`)
349
+ }
350
+ )
351
+ ] });
352
+ };
353
+ var CldUploadWidget_default = CldUploadWidget;
354
+
355
+ // src/components/CldUploadButton/CldUploadButton.tsx
356
+ import { Fragment as Fragment2, jsx as jsx4 } from "react/jsx-runtime";
357
+ var CldUploadButton = (_a) => {
358
+ var _b = _a, {
359
+ children,
360
+ onClick,
361
+ onUpload,
362
+ options,
363
+ signatureEndpoint,
364
+ uploadPreset
365
+ } = _b, props = __objRest(_b, [
366
+ "children",
367
+ "onClick",
368
+ "onUpload",
369
+ "options",
370
+ "signatureEndpoint",
371
+ "uploadPreset"
372
+ ]);
373
+ return /* @__PURE__ */ jsx4(Fragment2, { children: /* @__PURE__ */ jsx4(
374
+ CldUploadWidget_default,
375
+ {
376
+ onUpload,
377
+ options,
378
+ signatureEndpoint,
379
+ uploadPreset,
380
+ children: ({ open }) => {
381
+ function handleOnClick(e) {
382
+ e.preventDefault();
383
+ open();
384
+ if (typeof onClick === "function") {
385
+ onClick(e);
386
+ }
387
+ }
388
+ return /* @__PURE__ */ jsx4("button", __spreadProps(__spreadValues({}, props), { onClick: handleOnClick, children: children || "Upload" }));
389
+ }
390
+ }
391
+ ) });
392
+ };
393
+ var CldUploadButton_default = CldUploadButton;
394
+ export {
395
+ CldImage_default as CldImage,
396
+ CldOgImage_default as CldOgImage,
397
+ CldUploadButton_default as CldUploadButton,
398
+ CldUploadWidget_default as CldUploadWidget,
399
+ cloudinaryLoader
400
+ };
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "next-cloudinary",
3
- "version": "2.1.2",
4
- "main": "dist/next-cloudinary.umd.js",
5
- "module": "dist/next-cloudinary.module.js",
6
- "umd:main": "dist/next-cloudinary.umd.js",
3
+ "version": "2.3.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
7
  "source": "src/index.ts",
8
8
  "scripts": {
9
- "build": "microbundle --no-compress --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react",
10
- "dev": "microbundle watch --no-compress --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react",
9
+ "build": "tsup src/index.ts --format esm,cjs --dts --external react",
10
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts --external react",
11
11
  "prepublishOnly": "cp ../README.md . && yarn build",
12
12
  "postpublish": "rm ./README.md",
13
13
  "test": "jest"
14
14
  },
15
15
  "dependencies": {
16
- "@cloudinary-util/url-loader": "^1.2.8",
16
+ "@cloudinary-util/url-loader": "^1.3.0",
17
17
  "@cloudinary-util/util": "^1.2.0",
18
18
  "@cloudinary/url-gen": "^1.8.6"
19
19
  },
@@ -21,12 +21,14 @@
21
21
  "@babel/core": "^7.19.6",
22
22
  "@babel/preset-env": "^7.19.4",
23
23
  "@types/jest": "^29.2.0",
24
+ "@types/react": "^18.0.28",
25
+ "@types/react-dom": "^18.0.11",
24
26
  "babel-jest": "^29.2.2",
25
27
  "dotenv": "^16.0.3",
26
28
  "jest": "^29.2.2",
27
29
  "jest-environment-jsdom": "^29.2.2",
28
- "microbundle": "^0.15.1",
29
30
  "ts-jest": "^29.0.3",
31
+ "tsup": "^6.6.3",
30
32
  "typescript": "^4.9.4"
31
33
  },
32
34
  "peerDependencies": {
package/tsconfig.json CHANGED
@@ -1,30 +1,36 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/tsconfig",
3
- "display": "Next.js",
3
+ "display": "Next Cloudinary",
4
4
  "compilerOptions": {
5
- "target": "es5",
6
- "lib": ["dom", "dom.iterable", "esnext"],
7
- "allowJs": true,
8
- "skipLibCheck": true,
9
- "strict": true,
10
- "forceConsistentCasingInFileNames": true,
11
- "noImplicitAny": false,
5
+ "target": "es6",
6
+ "composite": false,
7
+ "declaration": true,
8
+ "declarationMap": true,
12
9
  "esModuleInterop": true,
13
- "module": "esnext",
10
+ "forceConsistentCasingInFileNames": true,
11
+ "inlineSources": false,
12
+ "isolatedModules": true,
13
+ "module": "ESNext",
14
14
  "moduleResolution": "node",
15
+ "noUnusedLocals": false,
16
+ "noUnusedParameters": false,
17
+ "preserveWatchOutput": true,
15
18
  "resolveJsonModule": true,
16
- "isolatedModules": true,
17
- "jsx": "preserve",
18
- "incremental": true,
19
+ "skipLibCheck": true,
20
+ "strict": true,
21
+ "lib": [
22
+ "dom",
23
+ "dom.iterable",
24
+ "esnext",
25
+ "ES2015"
26
+ ],
19
27
  "plugins": [
20
28
  {
21
29
  "name": "next"
22
30
  }
23
31
  ],
24
- "rootDir": ".",
25
- "composite": true,
26
- "declaration": true,
27
- "declarationDir": "dist",
32
+ "jsx": "react-jsx"
33
+
28
34
  },
29
- "include": ["./package.json", "src/**/*"],
35
+ "exclude": ["node_modules"]
30
36
  }