Fix image integration crash on Netlify Functions due to import.meta.url
#5888
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
Netlify Functions are still on CJS, however they support ESM files by running them through
esbuild
, this is great, butesbuild
doesn't polyfillimport.meta.url
so our function just immediately crash.This PR add a helper that either return
import.meta.url
if supported, or an URL using__dirname
so it works both directly in ESM and hazardly transpiled CJSFix #5774
Note
There's apparently a way to get Netlify functions to run in pure ESM mode already, using a per-function config file (an undocumented feature) and a (also undocumented) flag, but I could never get it to work despite multiple attempts 🤷♀️
Netlify is shipping pure ESM functions natively with no hidden flag soon, so this is really just a temporary workaround that we'll be able to remove in the future (or keep if it affects other hosts, who knows)
Testing
Tested manually, this issue only shows up when running directly on Netlify
Docs
N/A