11ty / eleventy-plugin-bundle

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

Allow missing outputPath when running under Serverless #18

Closed ashur closed 1 year ago

ashur commented 1 year ago

Summary

This PR updates the bundle transform function, allowing page.outputPath to be falsy when running under Eleventy Serverless.

This fixes an issue that causes placeholder strings to be rendered in Eleventy Serverless responses.

Details & Context

When using the shipping version of the bundler plugin in an Eleventy Serverless context, placeholder values are unexpectedly rendered to the resulting page. For example:

{# src/_includes/layouts/base.njk #}

<!-- Here comes a bundle result! -->
{%- getBundle "html", "head-preconnect" -%}

results in an un-transformed placeholder rendered in the response:

<!-- Here comes a bundle result! -->
/*__EleventyBundle:get:html:head-preconnect:EleventyBundle__*/

This appears to be the result of the test for page.outputPath:

https://github.com/11ty/eleventy-plugin-bundle/blob/5a95ce2e0863722ed8242f3e44cdcd94323a4982/eleventy.shortcodes.js#L63-L66

which is false when the page is rendered by Eleventy Serverless:

{
  date: 2023-11-24T20:16:56.706Z,
  inputPath: './src/cms/preview.njk',
  fileSlug: 'preview',
  filePathStem: '/cms/preview',
  outputFileExtension: 'html',
  templateSyntax: 'njk',
  url: '/preview/drafts.19ce8ff9-90a7-4507-b3cf-957440d170b4',
  outputPath: false // 👈
}

Removing that check when running under Serverless — leaving only the test of whether content is a string — results in placeholder replacement by the plugin transform, as expected.

zachleat commented 1 year ago

I think this is good! Looks like the origins of that check were to limit to .html outputs:

https://github.com/11ty/eleventy-plugin-bundle/blame/2d0b9f51bd668555fbfd80c26022a2e6d99622c8/eleventy.shortcodes.js#L55C3-L55C55

zachleat commented 1 year ago

Shipped with v1.0.5!