Skip to content

Commit 9e1b7aa

Browse files
committed
cleaning up unused variables, clarifying logs, preventing functionality where data is unavailable
1 parent 03098ba commit 9e1b7aa

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
[plugins.inputs]
1212
cloudName = "colbycloud"
13-
# deliveryType = "upload"
13+
deliveryType = "upload"

src/index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
2121

2222
module.exports = {
2323
async onPreBuild({ netlifyConfig, constants, inputs, utils }) {
24-
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
25-
26-
if ( !host ) {
27-
console.warn('Cannot determine Netlify host, not proceeding with on-page image replacement.');
28-
console.log('Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
29-
return;
30-
}
24+
console.log('Collecting Cloudinary asset configurations...')
3125

3226
const { PUBLISH_DIR } = constants;
3327

@@ -69,22 +63,17 @@ module.exports = {
6963
try {
7064
await Promise.all(imagesFiles.map(async image => {
7165
const publishPath = image.replace(PUBLISH_DIR, '');
72-
const publishUrl = `${host}${publishPath}`;
73-
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, publishPath)}`;
74-
const cldAssetUrl = `${host}${cldAssetPath}`;
7566

7667
const cloudinary = await getCloudinaryUrl({
7768
deliveryType,
7869
folder,
7970
path: publishPath,
8071
localDir: PUBLISH_DIR,
81-
uploadPreset,
82-
remoteHost: host,
72+
uploadPreset
8373
});
8474

8575
return {
8676
publishPath,
87-
publishUrl,
8877
...cloudinary
8978
}
9079
}));
@@ -96,25 +85,27 @@ module.exports = {
9685
netlifyConfig.build.environment.CLOUDINARY_ASSETS = {
9786
images
9887
}
99-
},
10088

101-
async onBuild({ netlifyConfig, constants, inputs, utils }) {
102-
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
89+
console.log('Done.');
90+
},
10391

104-
if ( !host ) {
105-
console.warn('Cannot determine Netlify host, not proceeding with on-page image replacement.');
106-
console.log('Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
107-
return;
108-
}
92+
async onBuild({ netlifyConfig, inputs, utils }) {
93+
console.log('Creating redirects...');
10994

110-
const { PUBLISH_DIR } = constants;
95+
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
11196

11297
const {
11398
deliveryType,
11499
uploadPreset,
115100
folder = process.env.SITE_NAME
116101
} = inputs;
117102

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+
118109
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
119110
const apiKey = process.env.CLOUDINARY_API_KEY;
120111
const apiSecret = process.env.CLOUDINARY_API_SECRET;
@@ -135,8 +126,15 @@ module.exports = {
135126
// for each asset uploaded
136127

137128
if ( deliveryType === 'upload' ) {
138-
await Promise.all(Object.keys(netlifyConfig.build.environment.CLOUDINARY_ASSETS).flatMap(mediaType => {
139-
return netlifyConfig.build.environment.CLOUDINARY_ASSETS[mediaType].map(async asset => {
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 => {
140138
const { publishPath, cloudinaryUrl } = asset;
141139

142140
netlifyConfig.redirects.unshift({
@@ -154,7 +152,7 @@ module.exports = {
154152
// path, so that we can safely allow Cloudinary to fetch the media remotely
155153

156154
if ( deliveryType === 'fetch' ) {
157-
await Promise.all(CLOUDINARY_ASSET_DIRECTORIES.map(async ({ name: mediaName, inputKey, path: defaultPath }) => {
155+
await Promise.all(CLOUDINARY_ASSET_DIRECTORIES.map(async ({ inputKey, path: defaultPath }) => {
158156
const mediaPath = inputs[inputKey] || defaultPath;
159157
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, mediaPath)}`;
160158
const cldAssetUrl = `${host}/${cldAssetPath}`;
@@ -182,16 +180,19 @@ module.exports = {
182180
}));
183181
}
184182

183+
console.log('Done.');
185184
},
186185

187186
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
188187
// This only solves on-page references until any JS refreshes the DOM
189188

190189
async onPostBuild({ netlifyConfig, constants, inputs, utils }) {
190+
console.log('Replacing on-page images with Cloudinary URLs...');
191+
191192
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
192193

193194
if ( !host ) {
194-
console.warn('Cannot determine Netlify host, not proceeding with on-page image replacement.');
195+
console.warn('Cannot determine Netlify host. Can not proceed with on-page image replacement.');
195196
console.log('Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
196197
return;
197198
}

0 commit comments

Comments
 (0)