11ty / eleventy-plugin-bundle

Little bundles of code, little bundles of joy.
72 stars 3 forks source link

imperatively adding code to bundles #25

Open mayank99 opened 6 months ago

mayank99 commented 6 months ago

Currently, the only way to add code to a bundle is by rendering it through a template. This works fine for simple cases, but it limits the usefulness of bundles, since not everything is always an eleventy template.

It would be useful to be able to incrementally add code to the bundle without/before rendering. For example, this would come in handy if you want to populate the bundle using arbitrary JS in the ---js frontmatter or an .11ty.js file or even in the 11ty config file.

This could be exposed using an additional method under this. For example:

this.writeBundle("css", "p { color: red; }");

this.getBundle("css"); // p { color: red; }

Edits:

Related: this.getBundle currently returns a placeholder comment until after the build finishes.

(Maybe also related: I just noticed the bundleExportKey option in the readme. I wonder if that could help with the flexibility of bundles since it's JS-related. 🤔)

zachleat commented 4 months ago

A few examples documenting bundleExportKey have popped up over at #28

mayank99 commented 4 months ago

Ah that does clarify bundleExportKey. It's useful but not related to imperatively adding code to bundles.