Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/global default cloudinary transforms #12

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![npm version](https://img.shields.io/npm/v/babel-plugin-cloudinary.svg?style=flat-square)](https://www.npmjs.com/package/babel-plugin-cloudinary)

Compile cloudinary URLs at build time.
Compile cloudinary . These properties can still be overwritten for individual cases, by `__buildCloudinaryUrl` call.

## API

Expand All @@ -17,14 +17,21 @@ You can define the globals for your cloudinary URLs in a `cloudinaryrc.json` tha
"secure": true
},
"overrideBaseUrl": true,
"host": "trivago.images.com"
"host": "trivago.images.com",
"defaultTransforms": {
// NOT RELEASED YET
"fetch_format": "auto",
"quality": "auto"
}
}
```

- **native** - these are directly passed into the `cloudinary-core` upon instantiation, you can use
all the [configs in the official cloudinary API](https://cloudinary.com/documentation/solution_overview#configuration_parameters).
- **overrideBaseUrl** - set this to true if you want to override cloudinary default URL with the property **host**.
- **host** - a host to perform replace the default generated base URL (`res.cloudinary.com/trivago/image/upload`).
- **defaultTransforms** [**NOT RELEASED YET**] - an object that holds transforms that are applied by default
to all the generated cloudinary URLs. These properties can still be overwritten for individual cases, by passing the same property within the `option.transforms` of that `__buildCloudinaryUrl` call.

### \_\_buildCloudinaryUrl

Expand Down
6 changes: 5 additions & 1 deletion lib/cloudinary-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ const BASE_URL_PLACEHOLDER = new RegExp(`res.cloudinary.com/${runConfig.native.c
* @returns {string} base image URL for the provided assetName.
*/
function getBaseImageUrl(assetName, transforms) {
let url = cl.url(assetName, transforms);
const enrichedTransforms = runConfig.defaultTransforms
? { ...runConfig.defaultTransforms, ...transforms }
: transforms;

let url = cl.url(assetName, enrichedTransforms);

if (runConfig.overrideBaseUrl) {
if (!runConfig.host) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"babel-cli/chokidar": "^2.0.4"
},
"husky": {
"skipCI": true,
"hooks": {
"pre-commit": "lint-staged"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runtime-config-default-transforms-override 1`] = `"const imageUrl = \`\${\\"https://trivago.images.com/e_red:50,f_auto,o_30,q_auto:best/\\"}\${\\"my-picture.jpeg\\"}\${\\"\\"}\${\\"\\"}\${\\"\\"}\`;"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"native": {
"cloud_name": "trivago",
"secure": true
},
"host": "trivago.images.com",
"overrideBaseUrl": true,
"defaultTransforms": {
"fetch_format": "auto",
"quality": "auto"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const imageUrl = __buildCloudinaryUrl("my-picture.jpeg", {
transforms: {
effect: "red:50",
opacity: 30,
quality: "auto:best", // will override default 'quality' in cloudinaryrc.json
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runtime-config-default-transforms 1`] = `"const imageUrl = \`\${\\"https://trivago.images.com/e_red:50,f_auto,o_30,q_auto/\\"}\${\\"my-picture.jpeg\\"}\${\\"\\"}\${\\"\\"}\${\\"\\"}\`;"`;
12 changes: 12 additions & 0 deletions test/fixtures/runtime-config-default-transforms/cloudinaryrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"native": {
"cloud_name": "trivago",
"secure": true
},
"host": "trivago.images.com",
"overrideBaseUrl": true,
"defaultTransforms": {
"fetch_format": "auto",
"quality": "auto"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/runtime-config-default-transforms/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const imageUrl = __buildCloudinaryUrl("my-picture.jpeg", {
transforms: {
effect: "red:50",
opacity: 30,
},
});