@@ -5,7 +5,7 @@ const glob = require('glob');
5
5
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require ( './lib/cloudinary' ) ;
6
6
const { PUBLIC_ASSET_PATH } = require ( './data/cloudinary' ) ;
7
7
8
- const CLOUDINARY_MEDIA_FUNCTIONS = [
8
+ const CLOUDINARY_ASSET_DIRECTORIES = [
9
9
{
10
10
name : 'images' ,
11
11
inputKey : 'imagesPath' ,
@@ -21,7 +21,7 @@ const CLOUDINARY_MEDIA_FUNCTIONS = [
21
21
module . exports = {
22
22
23
23
async onBuild ( { netlifyConfig, constants, inputs } ) {
24
- const host = process . env . DEPLOY_PRIME_URL ;
24
+ const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
25
25
26
26
if ( ! host ) {
27
27
console . warn ( 'Cannot determine Netlify host, not proceeding with on-page image replacement.' ) ;
@@ -51,31 +51,27 @@ module.exports = {
51
51
apiSecret
52
52
} ) ;
53
53
54
- const imagesDirectory = glob . sync ( `${ PUBLISH_DIR } /images/**/*` ) ;
55
- const imagesFiles = imagesDirectory . filter ( file => ! ! path . extname ( file ) ) ;
56
-
57
- await Promise . all ( imagesFiles . map ( async file => {
58
- const filePath = file . replace ( PUBLISH_DIR , '' ) ;
59
-
60
- const cldAssetPath = `/${ path . join ( PUBLIC_ASSET_PATH , filePath ) } ` ;
54
+ await Promise . all ( CLOUDINARY_ASSET_DIRECTORIES . map ( async ( { name : mediaName , inputKey, path : defaultPath } ) => {
55
+ const mediaPath = inputs [ inputKey ] || defaultPath ;
56
+ const cldAssetPath = `/${ path . join ( PUBLIC_ASSET_PATH , mediaPath ) } ` ;
61
57
const cldAssetUrl = `${ host } /${ cldAssetPath } ` ;
62
58
63
59
const assetRedirectUrl = await getCloudinaryUrl ( {
64
60
deliveryType : 'fetch' ,
65
61
folder,
66
- path : `${ cldAssetUrl } :splat` ,
62
+ path : `${ cldAssetUrl } / :splat` ,
67
63
uploadPreset
68
64
} ) ;
69
65
70
66
netlifyConfig . redirects . unshift ( {
71
67
from : `${ cldAssetPath } *` ,
72
- to : `${ filePath } :splat` ,
68
+ to : `${ mediaPath } / :splat` ,
73
69
status : 200 ,
74
70
force : true
75
71
} ) ;
76
72
77
73
netlifyConfig . redirects . unshift ( {
78
- from : `${ filePath } *` ,
74
+ from : `${ mediaPath } / *` ,
79
75
to : assetRedirectUrl ,
80
76
status : 302 ,
81
77
force : true
0 commit comments