Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for v2 runtime #154

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-laws-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"esbuild-cf-functions-plugin": minor
---

Added support for the Cloudfront Function 2.0 runtime
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ According to them, it

> ... is compliant with ECMAScript (ES) version 5.1 and also supports some features of ES versions 6 through 9.

This plugin does its best to enable and disable transpiling features as the [documentation says is available][runtime].
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.

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

Expand Down Expand Up @@ -52,7 +52,14 @@ void build({
})
```

To enable v2 runtime features:

```js
plugins: [CloudFrontFunctionsPlugin({ runtimeVersion: 2 })],
```

_The plugin overrides the `format` and `target` options, unless I did something wrong._

[cf-functions]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html
[runtime]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html
[runtime-v2]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html
235 changes: 222 additions & 13 deletions src/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`allows exponent operator 1`] = `
exports[`runtimeVersion 1 > allows exponent operator 1`] = `
"
var foo = 2 ** 2;
console.log(foo);
"
`;

exports[`allows template strings 1`] = `
exports[`runtimeVersion 1 > allows template strings 1`] = `
"
var foo = \`Hello\`;
var bar = \`\${foo}\`;
"
`;

exports[`arrays > does not modify supported functions 1`] = `
exports[`runtimeVersion 1 > arrays > does not modify supported functions 1`] = `
"
var foo = Array.of(1, 2, 3);
var foo = [].copyWithin(10, 0, 2);
Expand All @@ -25,7 +25,7 @@ var foo = [].includes("1");
"
`;

exports[`arrays > does not modify supported typed arrays 1`] = `
exports[`runtimeVersion 1 > arrays > does not modify supported typed arrays 1`] = `
"
var foo = new Int8Array([1, 2, 3]);
var foo = new Uint8Array([1, 2, 3]);
Expand All @@ -47,14 +47,14 @@ var foo = new Float64Array([1, 2, 3]).toString();
"
`;

exports[`const, let, var > lets vars through 1`] = `
exports[`runtimeVersion 1 > const, let, var > lets vars through 1`] = `
"
var foo = "bar";
console.log(foo);
"
`;

exports[`does not modify crypto imports 1`] = `
exports[`runtimeVersion 1 > does not modify crypto imports 1`] = `
"
import crypto from "crypto";
var hash = crypto.createHash("sha1");
Expand All @@ -63,14 +63,14 @@ hash.digest("base64");
"
`;

exports[`does not modify named capture groups 1`] = `
exports[`runtimeVersion 1 > does not modify named capture groups 1`] = `
"
var regex = /(?<foo>.*+)/;
var matches = regex.exec("Hello world");
"
`;

exports[`does not modify supported functions 1`] = `
exports[`runtimeVersion 1 > does not modify supported functions 1`] = `
"
var foo = String.fromCodePoint(12);
var foo = "bar".codePointAt(0);
Expand All @@ -85,7 +85,7 @@ var foo = "bar".trimEnd();
"
`;

exports[`does not modify supported functions 2`] = `
exports[`runtimeVersion 1 > does not modify supported functions 2`] = `
"
var foo = Number.isFinite(10);
var foo = Number.isInteger(10);
Expand All @@ -107,7 +107,7 @@ var foo = 10 .toPrecision(10);
"
`;

exports[`does not modify supported functions 3`] = `
exports[`runtimeVersion 1 > does not modify supported functions 3`] = `
"
new Promise((resolve, reject) => resolve());
new Promise((resolve, reject) => reject());
Expand All @@ -117,7 +117,7 @@ new Promise((resolve, reject) => reject()).finally(console.log);
"
`;

exports[`functions > allows arrow functions 1`] = `
exports[`runtimeVersion 1 > functions > allows arrow functions 1`] = `
"
var foo = () => {
return true;
Expand All @@ -126,7 +126,7 @@ console.log(foo());
"
`;

exports[`functions > allows spread parameters 1`] = `
exports[`runtimeVersion 1 > functions > allows spread parameters 1`] = `
"
var foo = (...rest) => {
return rest[0];
Expand All @@ -135,7 +135,216 @@ console.log(foo("test"));
"
`;

exports[`minification does not rename handler function 1`] = `
exports[`runtimeVersion 1 > minification does not rename handler function 1`] = `
"
function handler(event){console.log("test")}
"
`;

exports[`runtimeVersion 2 > allows await/async 1`] = `
"
void (async () => {
var func = async () => true;
var result = await func();
})();
"
`;

exports[`runtimeVersion 2 > allows exponent operator 1`] = `
"
var foo = 2 ** 2;
console.log(foo);
"
`;

exports[`runtimeVersion 2 > allows template strings 1`] = `
"
var foo = \`Hello\`;
var bar = \`\${foo}\`;
"
`;

exports[`runtimeVersion 2 > arrays > does not modify supported functions 1`] = `
"
var foo = Array.of(1, 2, 3);
var foo = [].copyWithin(10, 0, 2);
var foo = [].fill("foo", 0, 20);
var foo = [].find(() => true);
var foo = [].findIndex(() => true);
var foo = [].includes("1");
"
`;

exports[`runtimeVersion 2 > arrays > does not modify supported typed arrays 1`] = `
"
var foo = new Int8Array([1, 2, 3]);
var foo = new Uint8Array([1, 2, 3]);
var foo = new Uint8ClampedArray([1, 2, 3]);
var foo = new Int16Array([1, 2, 3]);
var foo = new Uint16Array([1, 2, 3]);
var foo = new Int32Array([1, 2, 3]);
var foo = new Uint32Array([1, 2, 3]);
var foo = new Float32Array([1, 2, 3]);
var foo = new Float64Array([1, 2, 3]);
var foo = new Float64Array([1, 2, 3]);
var foo = new Float64Array([1, 2, 3]).copyWithin(10, 0, 2);
var foo = new Float64Array([1, 2, 3]).fill(1, 20);
var foo = new Float64Array([1, 2, 3]).join("\\n");
new Float64Array([1, 2, 3]).set([1, 2, 3]);
var foo = new Float64Array([1, 2, 3]).slice(0, 1);
var foo = new Float64Array([1, 2, 3]).subarray(0, 1);
var foo = new Float64Array([1, 2, 3]).toString();
"
`;

exports[`runtimeVersion 2 > const, let, var > lets const through 1`] = `
"
const foo = "bar";
console.log(foo);
"
`;

exports[`runtimeVersion 2 > const, let, var > lets let through 1`] = `
"
let foo = "bar";
console.log(foo);
"
`;

exports[`runtimeVersion 2 > const, let, var > lets vars through 1`] = `
"
var foo = "bar";
console.log(foo);
"
`;

exports[`runtimeVersion 2 > does not atob and btoa 1`] = `
"
atob(btao("Hello World"));
"
`;

exports[`runtimeVersion 2 > does not atob and btoa functions 1`] = `
"
atob(btao("Hello World"));
"
`;

exports[`runtimeVersion 2 > does not modify atob and btoa functions 1`] = `
"
atob(btao("Hello World"));
"
`;

exports[`runtimeVersion 2 > does not modify buffer imports 1`] = `
"
import buffer from "buffer";
var b = buffer.from("aString", "utf8");
b.toString("utf8");
"
`;

exports[`runtimeVersion 2 > does not modify buffer imports 2`] = `
"
import buffer from "buffer";
var b = buffer.from("aString", "utf8");
b.toString("utf8");
"
`;

exports[`runtimeVersion 2 > does not modify crypto imports 1`] = `
"
import crypto from "crypto";
var hash = crypto.createHash("sha1");
hash.update("data");
hash.digest("base64");
"
`;

exports[`runtimeVersion 2 > does not modify named capture groups 1`] = `
"
var regex = /(?<foo>.*+)/;
var matches = regex.exec("Hello world");
"
`;

exports[`runtimeVersion 2 > does not modify supported Promise functions 1`] = `
"
new Promise((resolve, reject) => resolve());
new Promise((resolve, reject) => reject());
new Promise((resolve, reject) => reject()).catch(console.log);
new Promise((resolve, reject) => reject()).then(console.log);
new Promise((resolve, reject) => reject()).finally(console.log);
Promise.all([new Promise((resolve, reject) => resolve())]);
"
`;

exports[`runtimeVersion 2 > does not modify supported functions 1`] = `
"
var foo = String.fromCodePoint(12);
var foo = "bar".codePointAt(0);
var foo = "bar".includes("ar");
var foo = "bar".startsWith("ba");
var foo = "bar".endsWith("ar");
var foo = "bar".repeat(2);
var foo = "bar".padStart(10);
var foo = "bar".padEnd(10);
var foo = "bar".trimStart();
var foo = "bar".trimEnd();
"
`;

exports[`runtimeVersion 2 > does not modify supported functions 2`] = `
"
var foo = Number.isFinite(10);
var foo = Number.isInteger(10);
var foo = Number.isNaN(10);
var foo = Number.isSafeInteger(10);
var foo = Number.parseFloat("10.0");
var foo = Number.parseInt("10");
var foo = Number.EPSILON;
var foo = Number.MAX_SAFE_INTEGER;
var foo = Number.MAX_VALUE;
var foo = Number.MIN_SAFE_INTEGER;
var foo = Number.MIN_VALUE;
var foo = Number.NEGATIVE_INFINITY;
var foo = Number.NaN;
var foo = Number.POSITIVE_INFINITY;
var foo = 10 .toExponential();
var foo = 10 .toFixed();
var foo = 10 .toPrecision(10);
"
`;

exports[`runtimeVersion 2 > does not modify supported functions 3`] = `
"
new Promise((resolve, reject) => resolve());
new Promise((resolve, reject) => reject());
new Promise((resolve, reject) => reject()).catch(console.log);
new Promise((resolve, reject) => reject()).then(console.log);
new Promise((resolve, reject) => reject()).finally(console.log);
"
`;

exports[`runtimeVersion 2 > functions > allows arrow functions 1`] = `
"
var foo = () => {
return true;
};
console.log(foo());
"
`;

exports[`runtimeVersion 2 > functions > allows spread parameters 1`] = `
"
var foo = (...rest) => {
return rest[0];
};
console.log(foo("test"));
"
`;

exports[`runtimeVersion 2 > minification does not rename handler function 1`] = `
"
function handler(event){console.log("test")}
"
Expand Down
Loading