Skip to content

Commit abef1c9

Browse files
committed
bundling function with ncc
1 parent ef4bf8d commit abef1c9

File tree

6 files changed

+203
-28
lines changed

6 files changed

+203
-28
lines changed

out/index.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/******/ (() => { // webpackBootstrap
2+
/******/ var __webpack_modules__ = ({
3+
4+
/***/ 103:
5+
/***/ ((module) => {
6+
7+
/**
8+
* isRemoteUrl
9+
*/
10+
11+
function isRemoteUrl(url) {
12+
return url.startsWith('http');
13+
}
14+
15+
module.exports.isRemoteUrl = isRemoteUrl;
16+
17+
/**
18+
* determineRemoteUrl
19+
*/
20+
21+
function determineRemoteUrl(url, host) {
22+
if ( isRemoteUrl(url) ) return url;
23+
24+
if ( !url.startsWith('/') ) {
25+
url = `/${url}`;
26+
}
27+
28+
url = `${host}${url}`;
29+
30+
return url;
31+
}
32+
33+
module.exports.determineRemoteUrl = determineRemoteUrl;
34+
35+
/**
36+
* getQueryParams
37+
*/
38+
39+
function getQueryParams(url) {
40+
if ( typeof url !== 'string') {
41+
throw new Error('Can not getQueryParams. Invalid URL');
42+
}
43+
44+
const params = {};
45+
46+
const urlSegments = url.split('?');
47+
48+
urlSegments[1] && urlSegments[1].split('&').forEach(segment => {
49+
const [key, value] = segment.split('=');
50+
params[key] = value;
51+
});
52+
53+
return params;
54+
}
55+
56+
module.exports.getQueryParams = getQueryParams;
57+
58+
/***/ })
59+
60+
/******/ });
61+
/************************************************************************/
62+
/******/ // The module cache
63+
/******/ var __webpack_module_cache__ = {};
64+
/******/
65+
/******/ // The require function
66+
/******/ function __nccwpck_require__(moduleId) {
67+
/******/ // Check if module is in cache
68+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
69+
/******/ if (cachedModule !== undefined) {
70+
/******/ return cachedModule.exports;
71+
/******/ }
72+
/******/ // Create a new module (and put it into the cache)
73+
/******/ var module = __webpack_module_cache__[moduleId] = {
74+
/******/ // no module.id needed
75+
/******/ // no module.loaded needed
76+
/******/ exports: {}
77+
/******/ };
78+
/******/
79+
/******/ // Execute the module function
80+
/******/ var threw = true;
81+
/******/ try {
82+
/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
83+
/******/ threw = false;
84+
/******/ } finally {
85+
/******/ if(threw) delete __webpack_module_cache__[moduleId];
86+
/******/ }
87+
/******/
88+
/******/ // Return the exports of the module
89+
/******/ return module.exports;
90+
/******/ }
91+
/******/
92+
/************************************************************************/
93+
/******/ /* webpack/runtime/compat */
94+
/******/
95+
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
96+
/******/
97+
/************************************************************************/
98+
var __webpack_exports__ = {};
99+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
100+
(() => {
101+
var exports = __webpack_exports__;
102+
const { getQueryParams } = __nccwpck_require__(103);
103+
104+
exports.handler = async function (event, context) {
105+
const { rawUrl } = event;
106+
107+
const rawUrlSegments = rawUrl.split('.netlify/functions/cld_images');
108+
const endpoint = rawUrlSegments[0].replace(/\/$/, '');
109+
const pathSegments = rawUrlSegments[1].split('?');
110+
const imagePath = `/cld-assets/images${pathSegments[0]}`;
111+
112+
const { deliveryType } = getQueryParams(rawUrl);
113+
114+
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || queryParams.cloudName;
115+
116+
const remoteUrl = encodeURIComponent(`${endpoint}${imagePath}`);
117+
118+
const cloudinaryUrl = `https://res.cloudinary.com/${cloudName}/image/${deliveryType}/f_auto,q_auto/${remoteUrl}`
119+
120+
console.log({
121+
rawUrl,
122+
pathSegments,
123+
imagePath,
124+
cloudName,
125+
endpoint,
126+
imagePath,
127+
remoteUrl,
128+
cloudinaryUrl
129+
})
130+
131+
return {
132+
statusCode: 302,
133+
headers: {
134+
Location: cloudinaryUrl
135+
}
136+
};
137+
};
138+
})();
139+
140+
module.exports = __webpack_exports__;
141+
/******/ })()
142+
;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const { getQueryParams } = require('../../lib/util');
4+
5+
exports.handler = async function (event, context) {
6+
const { rawUrl } = event;
7+
8+
const rawUrlSegments = rawUrl.split('.netlify/functions/cld_images');
9+
const endpoint = rawUrlSegments[0].replace(/\/$/, '');
10+
const pathSegments = rawUrlSegments[1].split('?');
11+
const imagePath = `/cld-assets/images${pathSegments[0]}`;
12+
13+
getQueryParams(rawUrl);
14+
15+
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || queryParams.cloudName;
16+
17+
const remoteUrl = encodeURIComponent(`${endpoint}${imagePath}`);
18+
19+
const cloudinaryUrl = `https://res.cloudinary.com/colbydemo/image/fetch/f_auto,q_auto/${remoteUrl}`;
20+
21+
console.log({
22+
rawUrl,
23+
pathSegments,
24+
imagePath,
25+
cloudName,
26+
endpoint,
27+
imagePath,
28+
remoteUrl,
29+
cloudinaryUrl
30+
});
31+
32+
return {
33+
statusCode: 302,
34+
headers: {
35+
Location: cloudinaryUrl
36+
}
37+
};
38+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"license": "MIT",
1515
"dependencies": {
16+
"@vercel/ncc": "^0.33.1",
1617
"cloudinary": "^1.27.1",
1718
"fs-extra": "^10.0.0",
1819
"glob": "^7.2.0",

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const fs = require('fs-extra')
33
const path = require('path');
44
const glob = require('glob');
5+
const ncc = require('@vercel/ncc');
56

67
const { getCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require('./lib/cloudinary');
78

@@ -31,10 +32,19 @@ module.exports = {
3132

3233
// Copy all of the templates over including the functions to deploy
3334

35+
const functionTemplatesPath = path.join(__dirname, 'templates/functions');
36+
const functionTemplates = await fs.readdir(functionTemplatesPath);
37+
3438
try {
35-
await fs.copy(path.join(__dirname, 'templates'), functionsPath);
39+
await Promise.all(functionTemplates.map(async templateFileName => {
40+
const bundle = await ncc(path.join(functionTemplatesPath, templateFileName));
41+
const { name, base } = path.parse(templateFileName);
42+
const templateDirectory = path.join(functionsPath, name);
43+
await fs.mkdir(templateDirectory);
44+
await fs.writeFile(path.join(templateDirectory, base), bundle.code, 'utf8');
45+
}));
3646
} catch(e) {
37-
console.log('Failed to copy templates:', e);
47+
console.log('Failed to generate templates:', e);
3848
throw e;
3949
}
4050

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { getQueryParams } = require('../../lib/util');
2+
13
exports.handler = async function (event, context) {
24
const { rawUrl } = event;
35

@@ -6,13 +8,13 @@ exports.handler = async function (event, context) {
68
const pathSegments = rawUrlSegments[1].split('?');
79
const imagePath = `/cld-assets/images${pathSegments[0]}`;
810

9-
const { deliveryType, uploadPreset } = getQueryParams(rawUrl);
11+
const { deliveryType } = getQueryParams(rawUrl);
1012

1113
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || queryParams.cloudName;
1214

1315
const remoteUrl = encodeURIComponent(`${endpoint}${imagePath}`);
1416

15-
const cloudinaryUrl = `https://res.cloudinary.com/colbydemo/image/fetch/f_auto,q_auto/${remoteUrl}`
17+
const cloudinaryUrl = `https://res.cloudinary.com/${cloudName}/image/${deliveryType}/f_auto,q_auto/${remoteUrl}`
1618

1719
console.log({
1820
rawUrl,
@@ -31,27 +33,4 @@ exports.handler = async function (event, context) {
3133
Location: cloudinaryUrl
3234
}
3335
};
34-
};
35-
36-
/**
37-
* getQueryParams
38-
*/
39-
40-
function getQueryParams(url) {
41-
if ( typeof url !== 'string') {
42-
throw new Error('Can not getQueryParams. Invalid URL');
43-
}
44-
45-
const params = {};
46-
47-
const urlSegments = url.split('?');
48-
49-
urlSegments[1] && urlSegments[1].split('&').forEach(segment => {
50-
const [key, value] = segment.split('=');
51-
params[key] = value;
52-
});
53-
54-
return params;
55-
}
56-
57-
module.exports.getQueryParams = getQueryParams;
36+
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@
597597
dependencies:
598598
"@types/yargs-parser" "*"
599599

600+
"@vercel/ncc@^0.33.1":
601+
version "0.33.1"
602+
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.33.1.tgz#b240080a3c1ded9446a30955a06a79851bb38f71"
603+
integrity sha512-Mlsps/P0PLZwsCFtSol23FGqT3FhBGb4B1AuGQ52JTAtXhak+b0Fh/4T55r0/SVQPeRiX9pNItOEHwakGPmZYA==
604+
600605
abab@^2.0.3, abab@^2.0.5:
601606
version "2.0.5"
602607
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"

0 commit comments

Comments
 (0)