11ty / eleventy

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

Default passthrough copy directory #3089

Open darthmall opened 8 months ago

darthmall commented 8 months ago

Is your feature request related to a problem? Please describe.

You can’t include static assets such as images or external CSS files when using Eleventy without a config file.

Describe the solution you'd like

I’d like to be able to define passthrough copy on the command-line with flags. Additionally, I’d like a default value that passes the contents of an entire directory through to the output directory so that someone can use Eleventy per the instructions in the getting started docs. I think this will make it a lot easier for beginners to get started with Eleventy.

Describe alternatives you've considered

For now you can create a very minimal config for static assets like this:

module.exports = function (eleventyConfig) {
  eleventyConfig.addPassthroughCopy({ "public/": "/" });
  return {};
});

Which will let you put all your static assets in a public/ folder and have them copied to _site/.

Additional context

I was recently helping a friend who wants to learn HTML and CSS start building her website with Eleventy. She had followed the instructions for the zero-config use of Eleventy from the docs, and we wouldn’t have needed a config file at all if she hadn’t wanted to include images in her site (and who wouldn’t want to be able to put images on their website).

I think Eleventy could be a really great tool for people getting started with HTML and CSS if there was some default location for passthrough copy so that people can include static assets in their sites without having to set up a config. Even a minimal config can be a bit much for new web designers because of all the Node cruft, so it would be really nice to be able to avoid that until you start wanting to customize Eleventy with plugins, etc.

uncenter commented 8 months ago

Great idea. I'm a big fan of using the public/ directory as the default, that's what I use on my sites.

Snapstromegon commented 8 months ago

I'm personally not a big fan of adding either a "magic passthrough", which exists by default, nor the value being public (but this is personal taste, since I like the semantic assets better for such a folder, but that's up to debate).

My reason for this is, that any magic value doesn't fit in all cases, so it can't be there in all cases. But removing it (e.g. once a user has their own config) will lead to more confusion, why "something that wasn't touched" is no longer working.

On the other hand I think adding e.g. a --copy <glob> flag (that maybe can be used multiple times for multiple globs - don't know if minimist handles this) to the cmd for easily adding a passthrough copy without a config file is a good idea.

uncenter commented 8 months ago

Hmmm after some thought I can see where you are coming from @Snapstromegon. I wonder how disabling the default one would work if this suggestion was implemented @darthmall?

darthmall commented 7 months ago

My reason for this is, that any magic value doesn't fit in all cases, so it can't be there in all cases. But removing it (e.g. once a user has their own config) will lead to more confusion, why "something that wasn't touched" is no longer working.

Two things about this:

  1. Using addPassthroughCopy wouldn’t remove the default passthrough copy, because this configuration method just appends copy rules to the config. So I don’t think this is really a concern. If a person was using the default passthrough directory and then added a config with some new copy rules, those rules wouldn’t erase the default
  2. It’s already the case that when people add a configuration file to their Eleventy project we have this issue of changing defaults leading to confusion. I’ve been on the Eleventy Discord for a couple of years helping with Eleventy projects and I have a hard time believing that the addition of a default passthrough directory would exacerbate the problem

I wonder how disabling the default one would work if this suggestion was implemented @darthmall?

If we wanted to support disabling the default passthrough directory, I think Eleventy could add a setPassthroughCopy in addition to addPassthroughCopy, where setPassthroughCopy doesn’t append copy rules to the configuration, but resets the copy rules to match the argument.

All that said, as long as there’s a way to define passthrough copy rules from the command-line, that would be sufficient to enable the use of Eleventy without a config file, which is the goal.

Snapstromegon commented 7 months ago

If we wanted to support disabling the default passthrough directory

In my opinion this is a hard requirement. You can't (and IMO shouldn't try to) name a directory that will work for everyone globally to hold files that are always safe to copy to _site. In addition a project might start out copying assets like images and scripts to the output directory and at some later point want to replace those using some bundler or optimization tool. Forcing them to relocate them so the originals don't show up in the output is IMO not reasonable.

Adding something like setPassthroughCopy would probably be something that many users would need to call in every project's config and that might lead to accidental leaks in outputs by users who don't know about this magic default.

For these reasons (like mentioned above) I think adding a cmd argument is good, but adding a default is not.

Regenhardt commented 1 month ago

I just went through the getting started and have been wondering the same thing: Why aren't my assets (images and a css file) from the input folder copied to the output folder along with my markdown inputs?

I support either a default directory like _assets (because 1. assets just seems fitting for things that just get used by other stuff and 2. having a _ in front like the other default folders) so everything in there lands in the output, or somehow copying all non-convertable files from the input folder directly to the output folder.

At first I had everything in one folder: index.md, _include/layout.njk, node_modules, package.json, package-lock.json.
Since this is probably the absolute minimal default that people use as a minimal just-put-stuff-in-my-markdown website, I'd totally support some default of ignored things (node_modules, package.json, package-lock.json, and maybe .git and .gitignore) where everything else than can't be converted is just directly copied to output.

Then I moved my source files into src, including style.css, and added --input=src, again hoping that at this point, everything non-convertible from the src directory would be passed to the output, because at this point it's explicitly in my input directory.
So at this point, I actually expected the above behaviour to get my static assets.

I can totally also see a cmd arg like --assets=_assets or something, but that needs to be documented then, as it's not as intuitive as some default copying things from input to output.

In general, I see 11ty as a mid point between a big complex SSG, and the trivial version of just running my markdown files through markdown-to-html-cli and copying everything non-markdown in the directory to the output. So I was disappointed to have this rather big gap between zero-config and having to write the config to include my css and images explicitly if I want css and images on my website.

Sorry this got longer than I expected, don't hesitate to ask about stuff, I tried documenting my train of thought here which can be weird when I'm tired.

Edit: Gitlab pages uses the public directory as deployment source. So to deploy to Gitlab pages, everything to be deployed has to be put into the public directory, which is why for me, public feels weird as a static asset source folder.