@@ -21,13 +21,7 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
21
21
22
22
module . exports = {
23
23
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...' )
31
25
32
26
const { PUBLISH_DIR } = constants ;
33
27
@@ -69,22 +63,17 @@ module.exports = {
69
63
try {
70
64
await Promise . all ( imagesFiles . map ( async image => {
71
65
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 } ` ;
75
66
76
67
const cloudinary = await getCloudinaryUrl ( {
77
68
deliveryType,
78
69
folder,
79
70
path : publishPath ,
80
71
localDir : PUBLISH_DIR ,
81
- uploadPreset,
82
- remoteHost : host ,
72
+ uploadPreset
83
73
} ) ;
84
74
85
75
return {
86
76
publishPath,
87
- publishUrl,
88
77
...cloudinary
89
78
}
90
79
} ) ) ;
@@ -96,25 +85,27 @@ module.exports = {
96
85
netlifyConfig . build . environment . CLOUDINARY_ASSETS = {
97
86
images
98
87
}
99
- } ,
100
88
101
- async onBuild ( { netlifyConfig , constants , inputs , utils } ) {
102
- const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
89
+ console . log ( 'Done.' ) ;
90
+ } ,
103
91
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...' ) ;
109
94
110
- const { PUBLISH_DIR } = constants ;
95
+ const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
111
96
112
97
const {
113
98
deliveryType,
114
99
uploadPreset,
115
100
folder = process . env . SITE_NAME
116
101
} = inputs ;
117
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
+
118
109
const cloudName = process . env . CLOUDINARY_CLOUD_NAME || inputs . cloudName ;
119
110
const apiKey = process . env . CLOUDINARY_API_KEY ;
120
111
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
@@ -135,8 +126,15 @@ module.exports = {
135
126
// for each asset uploaded
136
127
137
128
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 => {
140
138
const { publishPath, cloudinaryUrl } = asset ;
141
139
142
140
netlifyConfig . redirects . unshift ( {
@@ -154,7 +152,7 @@ module.exports = {
154
152
// path, so that we can safely allow Cloudinary to fetch the media remotely
155
153
156
154
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 } ) => {
158
156
const mediaPath = inputs [ inputKey ] || defaultPath ;
159
157
const cldAssetPath = `/${ path . join ( PUBLIC_ASSET_PATH , mediaPath ) } ` ;
160
158
const cldAssetUrl = `${ host } /${ cldAssetPath } ` ;
@@ -182,16 +180,19 @@ module.exports = {
182
180
} ) ) ;
183
181
}
184
182
183
+ console . log ( 'Done.' ) ;
185
184
} ,
186
185
187
186
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
188
187
// This only solves on-page references until any JS refreshes the DOM
189
188
190
189
async onPostBuild ( { netlifyConfig, constants, inputs, utils } ) {
190
+ console . log ( 'Replacing on-page images with Cloudinary URLs...' ) ;
191
+
191
192
const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
192
193
193
194
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.' ) ;
195
196
console . log ( 'Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.' ) ;
196
197
return ;
197
198
}
0 commit comments