@@ -4,7 +4,7 @@ const glob = require('glob');
4
4
5
5
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require ( './lib/cloudinary' ) ;
6
6
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' ) ;
8
8
9
9
const CLOUDINARY_ASSET_DIRECTORIES = [
10
10
{
@@ -19,28 +19,30 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
19
19
* - Handle srcset
20
20
*/
21
21
22
+ const _cloudinaryAssets = { } ;
23
+
22
24
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 ...' ) ;
25
27
26
28
const { PUBLISH_DIR } = constants ;
27
29
30
+ const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
31
+
28
32
const {
29
33
deliveryType,
30
34
uploadPreset,
31
- folder = process . env . SITE_NAME
35
+ folder = process . env . SITE_NAME ,
36
+ imagesPath = CLOUDINARY_ASSET_DIRECTORIES . find ( ( { inputKey } ) => inputKey === 'imagesPath' ) . path
32
37
} = inputs ;
33
38
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 ) ;
39
42
return ;
40
43
}
41
44
42
45
const cloudName = process . env . CLOUDINARY_CLOUD_NAME || inputs . cloudName ;
43
-
44
46
const apiKey = process . env . CLOUDINARY_API_KEY ;
45
47
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
46
48
@@ -55,21 +57,23 @@ module.exports = {
55
57
apiSecret
56
58
} ) ;
57
59
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
60
62
61
- let images ;
63
+ const imagesDirectory = glob . sync ( `${ PUBLISH_DIR } /${ imagesPath } /**/*` ) ;
64
+ const imagesFiles = imagesDirectory . filter ( file => ! ! path . extname ( file ) ) ;
62
65
63
66
try {
64
- await Promise . all ( imagesFiles . map ( async image => {
67
+ _cloudinaryAssets . images = await Promise . all ( imagesFiles . map ( async image => {
65
68
const publishPath = image . replace ( PUBLISH_DIR , '' ) ;
66
69
67
70
const cloudinary = await getCloudinaryUrl ( {
68
71
deliveryType,
69
72
folder,
70
73
path : publishPath ,
71
74
localDir : PUBLISH_DIR ,
72
- uploadPreset
75
+ uploadPreset,
76
+ remoteHost : host
73
77
} ) ;
74
78
75
79
return {
@@ -82,59 +86,13 @@ module.exports = {
82
86
return ;
83
87
}
84
88
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
-
124
89
// If the delivery type is set to upload, we need to be able to map individual assets based on their public ID,
125
90
// which would require a dynamic middle solution, but that adds more hops than we want, so add a new redirect
126
91
// for each asset uploaded
127
92
128
93
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 => {
138
96
const { publishPath, cloudinaryUrl } = asset ;
139
97
140
98
netlifyConfig . redirects . unshift ( {
@@ -186,14 +144,14 @@ module.exports = {
186
144
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
187
145
// This only solves on-page references until any JS refreshes the DOM
188
146
189
- async onPostBuild ( { netlifyConfig , constants, inputs, utils } ) {
147
+ async onPostBuild ( { constants, inputs, utils } ) {
190
148
console . log ( 'Replacing on-page images with Cloudinary URLs...' ) ;
191
149
192
150
const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
193
151
194
152
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 ) ;
197
155
return ;
198
156
}
199
157
@@ -227,7 +185,7 @@ module.exports = {
227
185
const sourceHtml = await fs . readFile ( page , 'utf-8' ) ;
228
186
229
187
const { html, errors } = await updateHtmlImagesToCloudinary ( sourceHtml , {
230
- assets : netlifyConfig . build . environment . CLOUDINARY_ASSETS ,
188
+ assets : _cloudinaryAssets ,
231
189
deliveryType,
232
190
uploadPreset,
233
191
folder,
0 commit comments