Skip to content

Commit a334de0

Browse files
erikfriedbeeequeueErik Fried
authored
feat: add support for v2 runtime (#154)
Co-authored-by: Adam Haglund <ahaglund@paypal.com> Co-authored-by: Erik Fried <erik.fried@schibsted.com>
1 parent c00add2 commit a334de0

File tree

5 files changed

+806
-220
lines changed

5 files changed

+806
-220
lines changed

.changeset/flat-laws-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"esbuild-cf-functions-plugin": minor
3+
---
4+
5+
Added support for the Cloudfront Function 2.0 runtime

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ According to them, it
1313

1414
> ... is compliant with ECMAScript (ES) version 5.1 and also supports some features of ES versions 6 through 9.
1515
16-
This plugin does its best to enable and disable transpiling features as the [documentation says is available][runtime].
16+
This plugin does its best to enable and disable transpiling features as the documentation says is available for the [v1 runtime][runtime] and [v2 runtime][runtime-v2]. By default the v1 runtime is assumed.
1717

1818
**Check out the [example](./example)!**
1919

@@ -52,7 +52,14 @@ void build({
5252
})
5353
```
5454

55+
To enable v2 runtime features:
56+
57+
```js
58+
plugins: [CloudFrontFunctionsPlugin({ runtimeVersion: 2 })],
59+
```
60+
5561
_The plugin overrides the `format` and `target` options, unless I did something wrong._
5662

5763
[cf-functions]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html
5864
[runtime]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html
65+
[runtime-v2]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html

src/__snapshots__/plugin.test.ts.snap

Lines changed: 222 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`allows exponent operator 1`] = `
3+
exports[`runtimeVersion 1 > allows exponent operator 1`] = `
44
"
55
var foo = 2 ** 2;
66
console.log(foo);
77
"
88
`;
99

10-
exports[`allows template strings 1`] = `
10+
exports[`runtimeVersion 1 > allows template strings 1`] = `
1111
"
1212
var foo = \`Hello\`;
1313
var bar = \`\${foo}\`;
1414
"
1515
`;
1616

17-
exports[`arrays > does not modify supported functions 1`] = `
17+
exports[`runtimeVersion 1 > arrays > does not modify supported functions 1`] = `
1818
"
1919
var foo = Array.of(1, 2, 3);
2020
var foo = [].copyWithin(10, 0, 2);
@@ -25,7 +25,7 @@ var foo = [].includes("1");
2525
"
2626
`;
2727

28-
exports[`arrays > does not modify supported typed arrays 1`] = `
28+
exports[`runtimeVersion 1 > arrays > does not modify supported typed arrays 1`] = `
2929
"
3030
var foo = new Int8Array([1, 2, 3]);
3131
var foo = new Uint8Array([1, 2, 3]);
@@ -47,14 +47,14 @@ var foo = new Float64Array([1, 2, 3]).toString();
4747
"
4848
`;
4949

50-
exports[`const, let, var > lets vars through 1`] = `
50+
exports[`runtimeVersion 1 > const, let, var > lets vars through 1`] = `
5151
"
5252
var foo = "bar";
5353
console.log(foo);
5454
"
5555
`;
5656

57-
exports[`does not modify crypto imports 1`] = `
57+
exports[`runtimeVersion 1 > does not modify crypto imports 1`] = `
5858
"
5959
import crypto from "crypto";
6060
var hash = crypto.createHash("sha1");
@@ -63,14 +63,14 @@ hash.digest("base64");
6363
"
6464
`;
6565

66-
exports[`does not modify named capture groups 1`] = `
66+
exports[`runtimeVersion 1 > does not modify named capture groups 1`] = `
6767
"
6868
var regex = /(?<foo>.*+)/;
6969
var matches = regex.exec("Hello world");
7070
"
7171
`;
7272
73-
exports[`does not modify supported functions 1`] = `
73+
exports[`runtimeVersion 1 > does not modify supported functions 1`] = `
7474
"
7575
var foo = String.fromCodePoint(12);
7676
var foo = "bar".codePointAt(0);
@@ -85,7 +85,7 @@ var foo = "bar".trimEnd();
8585
"
8686
`;
8787
88-
exports[`does not modify supported functions 2`] = `
88+
exports[`runtimeVersion 1 > does not modify supported functions 2`] = `
8989
"
9090
var foo = Number.isFinite(10);
9191
var foo = Number.isInteger(10);
@@ -107,7 +107,7 @@ var foo = 10 .toPrecision(10);
107107
"
108108
`;
109109
110-
exports[`does not modify supported functions 3`] = `
110+
exports[`runtimeVersion 1 > does not modify supported functions 3`] = `
111111
"
112112
new Promise((resolve, reject) => resolve());
113113
new Promise((resolve, reject) => reject());
@@ -117,7 +117,7 @@ new Promise((resolve, reject) => reject()).finally(console.log);
117117
"
118118
`;
119119
120-
exports[`functions > allows arrow functions 1`] = `
120+
exports[`runtimeVersion 1 > functions > allows arrow functions 1`] = `
121121
"
122122
var foo = () => {
123123
return true;
@@ -126,7 +126,7 @@ console.log(foo());
126126
"
127127
`;
128128
129-
exports[`functions > allows spread parameters 1`] = `
129+
exports[`runtimeVersion 1 > functions > allows spread parameters 1`] = `
130130
"
131131
var foo = (...rest) => {
132132
return rest[0];
@@ -135,7 +135,216 @@ console.log(foo("test"));
135135
"
136136
`;
137137
138-
exports[`minification does not rename handler function 1`] = `
138+
exports[`runtimeVersion 1 > minification does not rename handler function 1`] = `
139+
"
140+
function handler(event){console.log("test")}
141+
"
142+
`;
143+
144+
exports[`runtimeVersion 2 > allows await/async 1`] = `
145+
"
146+
void (async () => {
147+
var func = async () => true;
148+
var result = await func();
149+
})();
150+
"
151+
`;
152+
153+
exports[`runtimeVersion 2 > allows exponent operator 1`] = `
154+
"
155+
var foo = 2 ** 2;
156+
console.log(foo);
157+
"
158+
`;
159+
160+
exports[`runtimeVersion 2 > allows template strings 1`] = `
161+
"
162+
var foo = \`Hello\`;
163+
var bar = \`\${foo}\`;
164+
"
165+
`;
166+
167+
exports[`runtimeVersion 2 > arrays > does not modify supported functions 1`] = `
168+
"
169+
var foo = Array.of(1, 2, 3);
170+
var foo = [].copyWithin(10, 0, 2);
171+
var foo = [].fill("foo", 0, 20);
172+
var foo = [].find(() => true);
173+
var foo = [].findIndex(() => true);
174+
var foo = [].includes("1");
175+
"
176+
`;
177+
178+
exports[`runtimeVersion 2 > arrays > does not modify supported typed arrays 1`] = `
179+
"
180+
var foo = new Int8Array([1, 2, 3]);
181+
var foo = new Uint8Array([1, 2, 3]);
182+
var foo = new Uint8ClampedArray([1, 2, 3]);
183+
var foo = new Int16Array([1, 2, 3]);
184+
var foo = new Uint16Array([1, 2, 3]);
185+
var foo = new Int32Array([1, 2, 3]);
186+
var foo = new Uint32Array([1, 2, 3]);
187+
var foo = new Float32Array([1, 2, 3]);
188+
var foo = new Float64Array([1, 2, 3]);
189+
var foo = new Float64Array([1, 2, 3]);
190+
var foo = new Float64Array([1, 2, 3]).copyWithin(10, 0, 2);
191+
var foo = new Float64Array([1, 2, 3]).fill(1, 20);
192+
var foo = new Float64Array([1, 2, 3]).join("\\n");
193+
new Float64Array([1, 2, 3]).set([1, 2, 3]);
194+
var foo = new Float64Array([1, 2, 3]).slice(0, 1);
195+
var foo = new Float64Array([1, 2, 3]).subarray(0, 1);
196+
var foo = new Float64Array([1, 2, 3]).toString();
197+
"
198+
`;
199+
200+
exports[`runtimeVersion 2 > const, let, var > lets const through 1`] = `
201+
"
202+
const foo = "bar";
203+
console.log(foo);
204+
"
205+
`;
206+
207+
exports[`runtimeVersion 2 > const, let, var > lets let through 1`] = `
208+
"
209+
let foo = "bar";
210+
console.log(foo);
211+
"
212+
`;
213+
214+
exports[`runtimeVersion 2 > const, let, var > lets vars through 1`] = `
215+
"
216+
var foo = "bar";
217+
console.log(foo);
218+
"
219+
`;
220+
221+
exports[`runtimeVersion 2 > does not atob and btoa 1`] = `
222+
"
223+
atob(btao("Hello World"));
224+
"
225+
`;
226+
227+
exports[`runtimeVersion 2 > does not atob and btoa functions 1`] = `
228+
"
229+
atob(btao("Hello World"));
230+
"
231+
`;
232+
233+
exports[`runtimeVersion 2 > does not modify atob and btoa functions 1`] = `
234+
"
235+
atob(btao("Hello World"));
236+
"
237+
`;
238+
239+
exports[`runtimeVersion 2 > does not modify buffer imports 1`] = `
240+
"
241+
import buffer from "buffer";
242+
var b = buffer.from("aString", "utf8");
243+
b.toString("utf8");
244+
"
245+
`;
246+
247+
exports[`runtimeVersion 2 > does not modify buffer imports 2`] = `
248+
"
249+
import buffer from "buffer";
250+
var b = buffer.from("aString", "utf8");
251+
b.toString("utf8");
252+
"
253+
`;
254+
255+
exports[`runtimeVersion 2 > does not modify crypto imports 1`] = `
256+
"
257+
import crypto from "crypto";
258+
var hash = crypto.createHash("sha1");
259+
hash.update("data");
260+
hash.digest("base64");
261+
"
262+
`;
263+
264+
exports[`runtimeVersion 2 > does not modify named capture groups 1`] = `
265+
"
266+
var regex = /(?<foo>.*+)/;
267+
var matches = regex.exec("Hello world");
268+
"
269+
`;
270+
271+
exports[`runtimeVersion 2 > does not modify supported Promise functions 1`] = `
272+
"
273+
new Promise((resolve, reject) => resolve());
274+
new Promise((resolve, reject) => reject());
275+
new Promise((resolve, reject) => reject()).catch(console.log);
276+
new Promise((resolve, reject) => reject()).then(console.log);
277+
new Promise((resolve, reject) => reject()).finally(console.log);
278+
Promise.all([new Promise((resolve, reject) => resolve())]);
279+
"
280+
`;
281+
282+
exports[`runtimeVersion 2 > does not modify supported functions 1`] = `
283+
"
284+
var foo = String.fromCodePoint(12);
285+
var foo = "bar".codePointAt(0);
286+
var foo = "bar".includes("ar");
287+
var foo = "bar".startsWith("ba");
288+
var foo = "bar".endsWith("ar");
289+
var foo = "bar".repeat(2);
290+
var foo = "bar".padStart(10);
291+
var foo = "bar".padEnd(10);
292+
var foo = "bar".trimStart();
293+
var foo = "bar".trimEnd();
294+
"
295+
`;
296+
297+
exports[`runtimeVersion 2 > does not modify supported functions 2`] = `
298+
"
299+
var foo = Number.isFinite(10);
300+
var foo = Number.isInteger(10);
301+
var foo = Number.isNaN(10);
302+
var foo = Number.isSafeInteger(10);
303+
var foo = Number.parseFloat("10.0");
304+
var foo = Number.parseInt("10");
305+
var foo = Number.EPSILON;
306+
var foo = Number.MAX_SAFE_INTEGER;
307+
var foo = Number.MAX_VALUE;
308+
var foo = Number.MIN_SAFE_INTEGER;
309+
var foo = Number.MIN_VALUE;
310+
var foo = Number.NEGATIVE_INFINITY;
311+
var foo = Number.NaN;
312+
var foo = Number.POSITIVE_INFINITY;
313+
var foo = 10 .toExponential();
314+
var foo = 10 .toFixed();
315+
var foo = 10 .toPrecision(10);
316+
"
317+
`;
318+
319+
exports[`runtimeVersion 2 > does not modify supported functions 3`] = `
320+
"
321+
new Promise((resolve, reject) => resolve());
322+
new Promise((resolve, reject) => reject());
323+
new Promise((resolve, reject) => reject()).catch(console.log);
324+
new Promise((resolve, reject) => reject()).then(console.log);
325+
new Promise((resolve, reject) => reject()).finally(console.log);
326+
"
327+
`;
328+
329+
exports[`runtimeVersion 2 > functions > allows arrow functions 1`] = `
330+
"
331+
var foo = () => {
332+
return true;
333+
};
334+
console.log(foo());
335+
"
336+
`;
337+
338+
exports[`runtimeVersion 2 > functions > allows spread parameters 1`] = `
339+
"
340+
var foo = (...rest) => {
341+
return rest[0];
342+
};
343+
console.log(foo("test"));
344+
"
345+
`;
346+
347+
exports[`runtimeVersion 2 > minification does not rename handler function 1`] = `
139348
"
140349
function handler(event){console.log("test")}
141350
"

0 commit comments

Comments
 (0)