Home

Awesome

babel-plugin-cloudinary

npm version

Compile cloudinary URLs at build time.

API

Globals

You can define the globals for your cloudinary URLs in a cloudinaryrc.json that should be placed in the root of your project.

{
  "native": {
    "cloud_name": "trivago",
    "secure": true
  },
  "overrideBaseUrl": true,
  "host": "trivago.images.com",
  "defaultTransforms": {
    "fetch_format": "auto",
    "quality": "auto"
  }
}

__buildCloudinaryUrl

__buildCloudinaryUrl(assetName, options);

http[s]://host/<transforms>/<prefix><assetName><postfix><resourceExtension>

Usage

Install the plugin

npm install babel-plugin-cloudinary

Add the plugin to your .babelrc

{
  "plugins": ["babel-plugin-cloudinary"]
}

Use it in your code

// gallery.js
function getImageUrl(imageName) {
  // compiles into "`${'https://res.cloudinary.com/<cloud_name>/image/upload/'}${imageName}${'.jpeg'}`;"
  return __buildCloudinaryUrl(imageName, {
    transforms: {
      width: 250,
      height: 250,
    },
    resourceExtension: ".jpeg",
  });
}

Additional configurations

This projects ships together with the plugin a types definition file (index.d.ts) that will be automatically recognized by IDEs and text editors with typescript based IntelliSense. In case you have some linting in place it might also be convenient to make __buildCloudinaryUrl a global. With eslint you can achieve this by adding a simple property to the globals block just like:

// .eslintrc.js
module.exports = {
  // ...
  globals: {
    // ...
    __buildCloudinaryUrl: true,
  },
  // ...
};