11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
16.58k stars 480 forks source link

Document returning binary data from transforms #2023

Open cfjedimaster opened 2 years ago

cfjedimaster commented 2 years ago

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. ;)

mwt commented 1 year ago

Note that you can bypass the output of a file altogether with

return;

as the sole return line.

chee commented 3 months ago

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.