-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Support for Cloudfront Functions #102
Comments
They have some peculiar limitations.
Restricted features: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html#writing-functions-javascript-features-restricted-features
|
Interesting! Thanks for the reading links. I guess with esbuild we would be able to set the compile target to |
Would probably be a lot of work, and difficult to configure, but maybe eslint rules? Probably outside the scope of this package though. |
This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. |
This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label. |
Cloudfront Functions requires ES5 which esbuild cannot target right now. |
Definitely having eslint rules for Cloudfront Functions would be amazing |
I've actually had decent luck with this build({
entryPoints: [join(cloudFrontFunction, "src", "index.ts")],
outdir: join(cloudFrontFunction, "dist"),
// Make compatible with CloudFront Functions limited ES5 JavaScript runtime
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html#writing-functions-javascript-features-core
format: "cjs",
target: "es5",
platform: "neutral",
treeShaking: true,
banner: {
js: "var module = {};", // allows exporting functions from TS files to unit test
},
minifyIdentifiers: false,
supported: {
"const-and-let": false, // throws a build-time error if you use `const` or `let`, but at least warns you
"exponent-operator": true,
"template-literal": true,
arrow: true,
"rest-argument": true,
"regexp-named-capture-groups": true,
},
}); It definitely doesn't transpile everything, but it works pretty well! |
Cool, than you! I might pull this config out into a Cloudfront Function Construct. |
Sounds good - I'd be happy to collab on that PR if it would be helpful. For me, it has been great to write my CloudFront functions in TS, write tests in There are some intricacies we'd probably need to document. For instance, with the config I posted and this very simple example: import type { CloudFrontFunctionsEvent } from "aws-lambda";
export function handler(event: CloudFrontFunctionsEvent) {
const request = event.request;
return request;
} You get this error:
so you have to write: import type { CloudFrontFunctionsEvent } from "aws-lambda";
export function handler(event: CloudFrontFunctionsEvent) {
var request = event.request;
return request;
} which is surprising. Also, writing |
Maybe it'd be possible to achieve this via esbuild plugins? Or ESLint rules? |
Disabling features in esbuild and causing a build failure seems like the earliest possible point that fits the remit of this construct. Ideally, we would have eslint rules and plugins available. Maybe a types package that is used instead of |
I wonder if this is as relevant anymore now that runtime 2.0 is out: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html |
There are still many restricted features: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html#writing-functions-javascript-features-restricted-features-20 |
Found this today, but did not try yet: https://www.npmjs.com/package/esbuild-cf-functions-plugin |
Nice find! Using a plugin is going to be annoying, but the settings might be easy enough to adapt. Although from looking at the source, it seems to do fairly basic stuff. |
Cloudfront supports small inline JavaScript functions. They have some limitations, mainly regarding size and used memory.
We should evaluate if the existing inline code works with it, or add support and document the usage.
The text was updated successfully, but these errors were encountered: