11ty / eleventy

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

Reading eleventyConfig in *.js Global Data Files #980

Open michrome opened 4 years ago

michrome commented 4 years ago

Hi

How do I read the eleventyConfig while in a *.js Global Data File? I'm trying to find the value of the Output Directory (i.e. the config that is '_site' by default).

Perhaps a more important question is "I am doing it right?"! I have images stored in Contentful that I want to copy into the output directory. My thinking is that I can have a _data/photos.js file that will:

Is this the correct approach or should I be downloading the images at a different point in the build lifecycle?

Thank you!

pdehaan commented 4 years ago

I usually just hardcode paths since I almost never change that, but you could maybe do one of the following:

pdehaan commented 4 years ago

As for the question of how and when to download external assets, that is a lot trickier to answer. A lot of times I'll just have some other npm script that I run manually to sync the data so I'm not constantly refetching data that hasn't changed or possibly hitting quota issues with external APIs (ie: GitHub).

michrome commented 4 years ago

Hey, @pdehaan thank you so much for taking the time to write your detailed reply.

My understanding then is that we can't reliably read the value of the Output Directory using JavaScript in Global Data Files. For example, if the Output Directory was set via the command line --output flag.

I was hoping there was a way of getting a reference to the Eleventy instance (so I could read its outputDir) or the configuration instance so I could read its dir.output.


Point taken on API limits: I planned to check if I already had the assets cached locally in the output directory before re-downloading them. Sounds like doing that from the Global Data File is reasonable as the images are data in my use case.