11ty / webc

Single File Web Components
MIT License
1.31k stars 38 forks source link

webc:type="js" requires string output #84

Closed oliverjam closed 1 year ago

oliverjam commented 1 year ago

The renderer crashes with a slightly confusing error if a JS component's final statement is not a string. E.g.

<script webc:type="js" webc:is="template">
  1;
</script>

Produces this error:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering webc template ./index.html (via TemplateContentRenderError)
[11ty] 2. rawContent.includes is not a function (via TypeError)
[11ty]
[11ty] Original error stack trace: TypeError: rawContent.includes is not a function
[11ty]     at AstSerializer.compileString (file:///Users/oli/Code/webc-test/node_modules/@11ty/webc/src/ast.js:934:18)
...

Coercing the number to a string with String(1) fixes the error.

If this is a limitation of the implementation (I can see why you might not want to do any type coercion here) then maybe the error/docs could be updated to mention the string requirement?

zachleat commented 1 year ago

Shipping with WebC v0.8.0 thank you!

oliverjam commented 1 year ago

That was ridiculously fast, thank you!