eleventy
eleventy copied to clipboard
Document returning binary data from transforms
So maybe this wasn't the intent, but... I've built a demo that uses transforms to notice when the destination is PDF. When it has, it creates a PDF version of the HTML and returns the binary data. Initially I was getting a corrupted PDF. But I noticed that if I returned null from transform, I got an error:
[11ty] TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received null
So a whim, I did this:
return Buffer.from(contents,'binary');
And it worked well. I'd say this should be documented, or if it's not intended, then only allow strings back. (I won't be upset. ;)
Note that you can bypass the output of a file altogether with
return;
as the sole return line.
this is documented for 11ty.js templates here btw https://www.11ty.dev/docs/languages/javascript/#function
and i think it makes sense that that can be done in a transform too, even if it's not explicitly mentioned in the transform documentation, because a transform is about taking content and returning transformed content.