File tree Expand file tree Collapse file tree 5 files changed +535
-0
lines changed Expand file tree Collapse file tree 5 files changed +535
-0
lines changed Original file line number Diff line number Diff line change
1
+ node_modules
Original file line number Diff line number Diff line change
1
+ name : netlify-plugin-cloudinary
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " netlify-plugin-cloudinary" ,
3
+ "version" : " 0.0.1" ,
4
+ "description" : " " ,
5
+ "main" : " src/index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "author" : {
10
+ "name" : " Colby Fayock" ,
11
+
12
+ "url" : " https://twitter.com/colbyfayock"
13
+ },
14
+ "license" : " MIT" ,
15
+ "dependencies" : {
16
+ "fs" : " ^0.0.1-security" ,
17
+ "glob" : " ^7.2.0" ,
18
+ "jsdom" : " ^18.1.1"
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ const glob = require ( 'glob' ) ;
2
+ const fs = require ( 'fs' ) . promises ;
3
+ const { JSDOM } = require ( 'jsdom' ) ;
4
+
5
+ const CLOUD_NAME = 'colbycloud' ;
6
+
7
+ module . exports = {
8
+
9
+ async onPostBuild ( { constants } ) {
10
+ const { PUBLISH_DIR } = constants ;
11
+
12
+ const pages = glob . sync ( `${ PUBLISH_DIR } /**/*.html` ) ;
13
+
14
+
15
+ for ( const page of pages ) {
16
+ const html = await fs . readFile ( page , 'utf-8' ) ;
17
+ const dom = new JSDOM ( html ) ;
18
+ const images = Array . from ( dom . window . document . querySelectorAll ( 'img' ) ) ;
19
+
20
+ for ( const $img of images ) {
21
+ const imgSrc = $img . getAttribute ( 'src' ) ;
22
+ let cloudinarySrc = imgSrc
23
+
24
+ if ( ! imgSrc . startsWith ( 'http' ) ) {
25
+ if ( ! imgSrc . startsWith ( '/' ) ) {
26
+ cloudinarySrc = `/${ cloudinarySrc } ` ;
27
+ }
28
+ cloudinarySrc = `${ process . env . DEPLOY_URL } ${ cloudinarySrc } ` ;
29
+ }
30
+
31
+ cloudinarySrc = `https://res.cloudinary.com/${ CLOUD_NAME } /image/fetch/${ cloudinarySrc } ` ;
32
+
33
+ $img . setAttribute ( 'src' , cloudinarySrc )
34
+
35
+ await fs . writeFile ( page , dom . serialize ( ) ) ;
36
+ }
37
+ }
38
+ }
39
+
40
+ }
You can’t perform that action at this time.
0 commit comments