1
1
const fs = require ( 'fs-extra' )
2
2
const path = require ( 'path' ) ;
3
3
const glob = require ( 'glob' ) ;
4
- const ncc = require ( '@vercel/ncc' ) ;
5
4
6
5
const { configureCloudinary, updateHtmlImagesToCloudinary } = require ( './lib/cloudinary' ) ;
7
- const { PREFIX , PUBLIC_ASSET_PATH } = require ( './data/cloudinary' ) ;
6
+ const { PUBLIC_ASSET_PATH } = require ( './data/cloudinary' ) ;
8
7
9
8
const CLOUDINARY_MEDIA_FUNCTIONS = [
10
9
{
@@ -22,150 +21,72 @@ const CLOUDINARY_MEDIA_FUNCTIONS = [
22
21
module . exports = {
23
22
24
23
async onBuild ( { netlifyConfig, constants, inputs } ) {
25
- const { FUNCTIONS_SRC , INTERNAL_FUNCTIONS_SRC , PUBLISH_DIR } = constants ;
24
+ const host = process . env . DEPLOY_PRIME_URL ;
25
+
26
+ if ( ! host ) {
27
+ console . warn ( 'Can not 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
+ }
31
+
32
+ const { PUBLISH_DIR } = constants ;
33
+
26
34
const {
27
35
deliveryType,
28
- folder = process . env . SITE_NAME ,
29
36
uploadPreset,
37
+ folder = process . env . SITE_NAME
30
38
} = inputs ;
31
39
32
40
const cloudName = process . env . CLOUDINARY_CLOUD_NAME || inputs . cloudName ;
41
+ const apiKey = process . env . CLOUDINARY_API_KEY ;
42
+ const apiSecret = process . env . CLOUDINARY_API_SECRET ;
33
43
34
44
if ( ! cloudName ) {
35
- throw new Error ( 'Cloudinary Cloud Name required. Please set cloudName input or use environment variable CLOUDINARY_CLOUD_NAME' ) ;
45
+ throw new Error ( 'Cloudinary Cloud Name required. Please use environment variable CLOUDINARY_CLOUD_NAME' ) ;
36
46
}
37
47
48
+ configureCloudinary ( {
49
+ cloudName,
50
+ apiKey,
51
+ apiSecret
52
+ } ) ;
53
+
38
54
const imagesDirectory = glob . sync ( `${ PUBLISH_DIR } /images/**/*` ) ;
39
55
const imagesFiles = imagesDirectory . filter ( file => ! ! path . extname ( file ) ) ;
40
56
41
- imagesFiles . forEach ( file => {
57
+ await Promise . all ( imagesFiles . map ( async file => {
42
58
const filePath = file . replace ( PUBLISH_DIR , '' ) ;
43
59
44
- netlifyConfig . redirects . unshift ( {
45
- from : `${ path . join ( PUBLIC_ASSET_PATH , filePath ) } *` ,
46
- to : `${ filePath } :splat` ,
47
- status : 200 ,
48
- force : true
49
- } ) ;
60
+ const cldAssetPath = path . join ( PUBLIC_ASSET_PATH , filePath ) ;
61
+ const cldAssetUrl = `${ host } /${ cldAssetPath } ` ;
50
62
51
- netlifyConfig . redirects . unshift ( {
52
- from : ` ${ filePath } *` ,
53
- to : `https://res.cloudinary.com/colbycloud/image/fetch/f_auto,q_auto/ ${ process . env . DEPLOY_PRIME_URL } / ${ path . join ( PUBLIC_ASSET_PATH , filePath ) } :splat` ,
54
- status : 302 ,
55
- force : true
63
+ const assetRedirectUrl = await getCloudinaryUrl ( {
64
+ deliveryType : 'fetch' ,
65
+ folder ,
66
+ path : ` ${ cldAssetUrl } :splat` ,
67
+ uploadPreset
56
68
} ) ;
57
69
58
- // netlifyConfig.redirects.unshift({
59
- // from: `${filePath}*`,
60
- // to: `/.netlify/functions/${functionName}?path=${mediaPathSplat}&${paramsString}`,
61
- // status: 302,
62
- // force: true,
63
- // });
64
- } )
65
-
66
- return ;
67
-
68
- CLOUDINARY_MEDIA_FUNCTIONS . forEach ( ( { name : mediaName , inputKey, path : defaultPath } ) => {
69
- const mediaPath = inputs [ inputKey ] || defaultPath ;
70
- const mediaPathSplat = path . join ( mediaPath , ':splat' ) ;
71
- const functionName = `${ PREFIX } _${ mediaName } ` ;
72
-
73
70
netlifyConfig . redirects . unshift ( {
74
- from : path . join ( PUBLIC_ASSET_PATH , mediaPath , '*' ) ,
75
- to : mediaPathSplat ,
71
+ from : ` ${ cldAssetPath } *` ,
72
+ to : ` ${ filePath } :splat` ,
76
73
status : 200 ,
77
74
force : true
78
75
} ) ;
79
76
80
77
netlifyConfig . redirects . unshift ( {
81
- from : path . join ( mediaPath , '*' ) ,
82
- to : `/.netlify/functions/ ${ functionName } ?path= ${ mediaPathSplat } & ${ paramsString } ` ,
78
+ from : ` ${ filePath } *` ,
79
+ to : assetRedirectUrl ,
83
80
status : 302 ,
84
- force : true ,
85
- } ) ;
86
- } ) ;
87
-
88
-
89
-
90
-
91
- console . log ( 'PUBLISH_DIR' , PUBLISH_DIR ) ;
92
- console . log ( 'imagesFiles' , imagesFiles ) ;
93
-
94
- throw new Error ( 'asdf' ) ;
95
-
96
- const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC ;
97
-
98
- // Copy all of the templates over including the functions to deploy
99
-
100
- const functionTemplatesPath = path . join ( __dirname , 'templates/functions' ) ;
101
- const functionTemplates = await fs . readdir ( functionTemplatesPath ) ;
102
-
103
- if ( ! Array . isArray ( functionTemplates ) || functionTemplates . length == 0 ) {
104
- throw new Error ( `Failed to generate templates: can not find function templates in ${ functionTemplatesPath } ` ) ;
105
- }
106
-
107
- try {
108
- await Promise . all ( functionTemplates . map ( async templateFileName => {
109
- const bundle = await ncc ( path . join ( functionTemplatesPath , templateFileName ) ) ;
110
- const { name, base } = path . parse ( templateFileName ) ;
111
- const templateDirectory = path . join ( functionsPath , name ) ;
112
- const filePath = path . join ( templateDirectory , base ) ;
113
-
114
- await fs . ensureDir ( templateDirectory ) ;
115
- await fs . writeFile ( filePath , bundle . code , 'utf8' ) ;
116
- } ) ) ;
117
- } catch ( e ) {
118
- throw new Error ( `Failed to generate templates: ${ e } ` ) ;
119
- }
120
-
121
- // Configure reference parameters for Cloudinary delivery to attach to redirect
122
-
123
- const params = {
124
- uploadPreset,
125
- deliveryType,
126
- cloudName,
127
- folder
128
- }
129
-
130
- const paramsString = Object . keys ( params )
131
- . filter ( key => typeof params [ key ] !== 'undefined' )
132
- . map ( key => `${ key } =${ encodeURIComponent ( params [ key ] ) } ` )
133
- . join ( '&' ) ;
134
-
135
- // Redirect any requests that hits /[media type]/* to a serverless function
136
-
137
- CLOUDINARY_MEDIA_FUNCTIONS . forEach ( ( { name : mediaName , inputKey, path : defaultPath } ) => {
138
- const mediaPath = inputs [ inputKey ] || defaultPath ;
139
- const mediaPathSplat = path . join ( mediaPath , ':splat' ) ;
140
- const functionName = `${ PREFIX } _${ mediaName } ` ;
141
-
142
- netlifyConfig . redirects . unshift ( {
143
- from : path . join ( PUBLIC_ASSET_PATH , mediaPath , '*' ) ,
144
- to : mediaPathSplat ,
145
- status : 200 ,
146
81
force : true
147
82
} ) ;
148
-
149
- netlifyConfig . redirects . unshift ( {
150
- from : path . join ( mediaPath , '*' ) ,
151
- to : `/.netlify/functions/${ functionName } ?path=${ mediaPathSplat } &${ paramsString } ` ,
152
- status : 302 ,
153
- force : true ,
154
- } ) ;
155
- } ) ;
156
-
83
+ } ) ) ;
157
84
} ,
158
85
159
86
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
160
87
// This only solves on-page references until any JS refreshes the DOM
161
88
162
89
async onPostBuild ( { constants, inputs } ) {
163
- const { PUBLISH_DIR } = constants ;
164
- const {
165
- deliveryType,
166
- uploadPreset,
167
- folder = process . env . SITE_NAME
168
- } = inputs ;
169
90
170
91
const host = process . env . DEPLOY_PRIME_URL ;
171
92
@@ -175,6 +96,13 @@ module.exports = {
175
96
return ;
176
97
}
177
98
99
+ const { PUBLISH_DIR } = constants ;
100
+ const {
101
+ deliveryType,
102
+ uploadPreset,
103
+ folder = process . env . SITE_NAME
104
+ } = inputs ;
105
+
178
106
const cloudName = process . env . CLOUDINARY_CLOUD_NAME || inputs . cloudName ;
179
107
const apiKey = process . env . CLOUDINARY_API_KEY ;
180
108
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
0 commit comments