Skip to content

Commit 128ebea

Browse files
committed
cleaning up error messaging, moving prebuild to build to avoid nonexistent build, removing build plugin env variable and storing data in top of scope
1 parent 279ee74 commit 128ebea

File tree

5 files changed

+32
-133
lines changed

5 files changed

+32
-133
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ yarn add netlify-plugin-cloudinary
7878
|-----------------|----------|-----------| ------------|
7979
| cloudName | No* | mycloud | Cloudinary Cloud Name |
8080
| deliveryType | No | fetch | The method in which Cloudinary stores and delivers images (Ex: fetch, upload) |
81-
| imagePath | No | /assets | Local path application serves image assets from |
81+
| imagesPath | No | /assets | Local path application serves image assets from |
8282
| folder | No | myfolder | Folder all media will be stored in. Defaults to Netlify site name |
8383
| uploadPreset | No | my-preset | Defined set of asset upload defaults in Cloudinary |
8484

src/data/errors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
ERROR_CLOUD_NAME_REQUIRED: 'A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME'
2+
ERROR_CLOUD_NAME_REQUIRED: 'A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME',
3+
ERROR_NETLIFY_HOST_UNKNOWN: 'Cannot determine Netlify host, can not proceed with plugin.',
4+
EEROR_NETLIFY_HOST_CLI_SUPPORT: 'Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.'
35
}

src/index.js

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const glob = require('glob');
44

55
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require('./lib/cloudinary');
66
const { PUBLIC_ASSET_PATH } = require('./data/cloudinary');
7-
const { ERROR_CLOUD_NAME_REQUIRED } = require('./data/errors');
7+
const { ERROR_CLOUD_NAME_REQUIRED, ERROR_NETLIFY_HOST_UNKNOWN, EEROR_NETLIFY_HOST_CLI_SUPPORT } = require('./data/errors');
88

99
const CLOUDINARY_ASSET_DIRECTORIES = [
1010
{
@@ -19,28 +19,30 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
1919
* - Handle srcset
2020
*/
2121

22+
const _cloudinaryAssets = {};
23+
2224
module.exports = {
23-
async onPreBuild({ netlifyConfig, constants, inputs, utils }) {
24-
console.log('Collecting Cloudinary asset configurations...')
25+
async onBuild({ netlifyConfig, constants, inputs, utils }) {
26+
console.log('Creating redirects...');
2527

2628
const { PUBLISH_DIR } = constants;
2729

30+
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
31+
2832
const {
2933
deliveryType,
3034
uploadPreset,
31-
folder = process.env.SITE_NAME
35+
folder = process.env.SITE_NAME,
36+
imagesPath = CLOUDINARY_ASSET_DIRECTORIES.find(({ inputKey }) => inputKey === 'imagesPath').path
3237
} = inputs;
3338

34-
// If we're using the fetch API, we don't need to worry about uploading any
35-
// of the media as it will all be publicly accessible, so we can skip this step
36-
37-
if ( deliveryType === 'fetch' ) {
38-
console.log('Skipping: Delivery type set to fetch.')
39+
if ( !host && deliveryType === 'fetch' ) {
40+
console.warn(ERROR_NETLIFY_HOST_UNKNOWN);
41+
console.log(EEROR_NETLIFY_HOST_CLI_SUPPORT);
3942
return;
4043
}
4144

4245
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
43-
4446
const apiKey = process.env.CLOUDINARY_API_KEY;
4547
const apiSecret = process.env.CLOUDINARY_API_SECRET;
4648

@@ -55,21 +57,23 @@ module.exports = {
5557
apiSecret
5658
});
5759

58-
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/images/**/*`);
59-
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
60+
// Look for any available images in the provided imagesPath to collect
61+
// asset details and to grab a Cloudinary URL to use later
6062

61-
let images;
63+
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/${imagesPath}/**/*`);
64+
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
6265

6366
try {
64-
await Promise.all(imagesFiles.map(async image => {
67+
_cloudinaryAssets.images = await Promise.all(imagesFiles.map(async image => {
6568
const publishPath = image.replace(PUBLISH_DIR, '');
6669

6770
const cloudinary = await getCloudinaryUrl({
6871
deliveryType,
6972
folder,
7073
path: publishPath,
7174
localDir: PUBLISH_DIR,
72-
uploadPreset
75+
uploadPreset,
76+
remoteHost: host
7377
});
7478

7579
return {
@@ -82,59 +86,13 @@ module.exports = {
8286
return;
8387
}
8488

85-
netlifyConfig.build.environment.CLOUDINARY_ASSETS = {
86-
images
87-
}
88-
89-
console.log('Done.');
90-
},
91-
92-
async onBuild({ netlifyConfig, inputs, utils }) {
93-
console.log('Creating redirects...');
94-
95-
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
96-
97-
const {
98-
deliveryType,
99-
uploadPreset,
100-
folder = process.env.SITE_NAME
101-
} = inputs;
102-
103-
if ( !host && deliveryType === 'fetch' ) {
104-
console.warn('Cannot determine Netlify host, can not proceed with creating redirects for fetch delivery type.');
105-
console.log('Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
106-
return;
107-
}
108-
109-
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
110-
const apiKey = process.env.CLOUDINARY_API_KEY;
111-
const apiSecret = process.env.CLOUDINARY_API_SECRET;
112-
113-
if ( !cloudName ) {
114-
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
115-
return;
116-
}
117-
118-
configureCloudinary({
119-
cloudName,
120-
apiKey,
121-
apiSecret
122-
});
123-
12489
// If the delivery type is set to upload, we need to be able to map individual assets based on their public ID,
12590
// which would require a dynamic middle solution, but that adds more hops than we want, so add a new redirect
12691
// for each asset uploaded
12792

12893
if ( deliveryType === 'upload' ) {
129-
const assets = netlifyConfig.build.environment.CLOUDINARY_ASSETS;
130-
131-
if ( !assets ) {
132-
utils.build.failBuild('Can not find build assets.');
133-
return;
134-
}
135-
136-
await Promise.all(Object.keys(assets).flatMap(mediaType => {
137-
return assets[mediaType].map(async asset => {
94+
await Promise.all(Object.keys(_cloudinaryAssets).flatMap(mediaType => {
95+
return _cloudinaryAssets[mediaType].map(async asset => {
13896
const { publishPath, cloudinaryUrl } = asset;
13997

14098
netlifyConfig.redirects.unshift({
@@ -186,14 +144,14 @@ module.exports = {
186144
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
187145
// This only solves on-page references until any JS refreshes the DOM
188146

189-
async onPostBuild({ netlifyConfig, constants, inputs, utils }) {
147+
async onPostBuild({ constants, inputs, utils }) {
190148
console.log('Replacing on-page images with Cloudinary URLs...');
191149

192150
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
193151

194152
if ( !host ) {
195-
console.warn('Cannot determine Netlify host. Can not proceed with on-page image replacement.');
196-
console.log('Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
153+
console.warn(ERROR_NETLIFY_HOST_UNKNOWN);
154+
console.log(EEROR_NETLIFY_HOST_CLI_SUPPORT);
197155
return;
198156
}
199157

@@ -227,7 +185,7 @@ module.exports = {
227185
const sourceHtml = await fs.readFile(page, 'utf-8');
228186

229187
const { html, errors } = await updateHtmlImagesToCloudinary(sourceHtml, {
230-
assets: netlifyConfig.build.environment.CLOUDINARY_ASSETS,
188+
assets: _cloudinaryAssets,
231189
deliveryType,
232190
uploadPreset,
233191
folder,

src/lib/cloudinary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async function updateHtmlImagesToCloudinary(html, options = {}) {
196196
return [publishPath, publishUrl].includes(imgSrc);
197197
});
198198

199-
if ( asset ) {
199+
if ( asset && deliveryType === 'upload' ) {
200200
cloudinaryUrl = asset.cloudinaryUrl;
201201
}
202202

tests/on-build.test.js

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('onBuild', () => {
1919

2020
process.env.SITE_NAME = 'cool-site';
2121
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';
22+
process.env.NETLIFY_HOST = 'https://netlify-plugin-cloudinary.netlify.app';
2223

2324
const deliveryType = 'fetch';
2425
const imagesPath = '/images';
@@ -38,7 +39,7 @@ describe('onBuild', () => {
3839
await onBuild({
3940
netlifyConfig,
4041
constants: {
41-
PUBLISH_DIR: '.next/out/images'
42+
PUBLISH_DIR: `.next/out${imagesPath}`
4243
},
4344
inputs: {
4445
deliveryType
@@ -62,68 +63,6 @@ describe('onBuild', () => {
6263
expect(redirects[2]).toEqual(defaultRedirect);
6364
});
6465

65-
66-
test('should create redirects with upload delivery type and custom inputs', async () => {
67-
const imagesFunctionName = 'cld_images';
68-
69-
fs.readdir.mockResolvedValue([imagesFunctionName]);
70-
71-
process.env.SITE_NAME = 'cool-site';
72-
process.env.NETLIFY_HOST = 'https://test-netlify-site.netlify.app';
73-
74-
const cloudName = 'othercloud';
75-
const deliveryType = 'upload';
76-
const imagesPath = '/awesome';
77-
const folder = 'test-folder';
78-
const uploadPreset = 'my-upload-preset';
79-
80-
const defaultRedirect = {
81-
from: '/path',
82-
to: '/other-path',
83-
status: 200
84-
}
85-
86-
const redirects = [defaultRedirect];
87-
88-
const netlifyConfig = {
89-
redirects,
90-
build: {
91-
environment: {
92-
CLOUDINARY_ASSETS: {
93-
images: [
94-
{
95-
publishPath: `${imagesPath}/publicid.jpeg`,
96-
cloudinaryUrl: `https://res.cloudinary.com/colbycloud/image/upload/f_auto,q_auto/v1/netlify-plugin-cloudinary/publicid-1234`
97-
}
98-
]
99-
}
100-
}
101-
}
102-
};
103-
104-
await onBuild({
105-
netlifyConfig,
106-
constants: {
107-
},
108-
inputs: {
109-
cloudName,
110-
deliveryType,
111-
imagesPath,
112-
folder,
113-
uploadPreset
114-
}
115-
});
116-
117-
expect(redirects[0]).toEqual({
118-
from: `${netlifyConfig.build.environment.CLOUDINARY_ASSETS.images[0].publishPath}*`,
119-
to: netlifyConfig.build.environment.CLOUDINARY_ASSETS.images[0].cloudinaryUrl,
120-
status: 302,
121-
force: true
122-
});
123-
124-
expect(redirects[1]).toEqual(defaultRedirect);
125-
});
126-
12766
});
12867

12968
});

0 commit comments

Comments
 (0)