11ty / eleventy-plugin-bundle

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

Include bundle type to modify specific bundle output in transforms #9

Closed hexagoncircle closed 1 year ago

hexagoncircle commented 1 year ago

In the Modify the bundle output section of the README, it includes an example of how to integrate postcss in a transform function. Without a way of knowing the bundle type, this transform would cause the build to fail because it cannot process non-CSS code.

This PR will fix that issue by exposing the bundle name in a type variable available to the transform callbacks. This would allow for the postcss example to work like so:

eleventyConfig.addPlugin(pluginBundle, {
    transforms: [
      async function (content) {
        if (this.type === "css") {
          let result = await postcss([postcssNested]).process(content, {
            from: this.page.inputPath,
            to: null,
          });
          return result.css;
        }

        return content;
      },
    ],
  });

I've included a test and also updated the README to reflect this change. Let me know if there's anything else I can provide!

zachleat commented 1 year ago

Shipping with v1.0.4, thank you!!