Skip to content

Commit

Permalink
Feature/global default cloudinary transforms (#12)
Browse files Browse the repository at this point in the history
* Extend cloudinaryrc to support defaultTransforms

* Update README with documentation for defaultTransforms

* Update CHANGELOG
  • Loading branch information
danielcaldas authored Aug 21, 2019
1 parent c9ceb9f commit 183c96e
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 6 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

## master

-
- [#12](https://github.com/trivago/babel-plugin-cloudinary/pull/12) Global default cloudinary transforms with `defaultTransforms`.

## [0.0.3](https://github.com/trivago/babel-plugin-cloudinary/tree/0.0.3)

### Bugfix

- Update dependencies based on npm security audit
- [#11](https://github.com/trivago/babel-plugin-cloudinary/pull/11) Update dependencies based on npm security audit.

## [0.0.2](https://github.com/trivago/babel-plugin-cloudinary/tree/0.0.2)

### Bugfix

- [#7](https://github.com/trivago/babel-plugin-cloudinary/issues/7) Support asset names in `getBaseImageUrl` that are URL-encoded by Cloudinary API.
- Update dependencies based on npm security audit
- [#10](https://github.com/trivago/babel-plugin-cloudinary/pull/10) Update dependencies based on npm security audit

## [0.0.1](https://github.com/trivago/babel-plugin-cloudinary/tree/0.0.1)

Expand Down
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,
},
});

0 comments on commit 183c96e

Please sign in to comment.