salesforce / lwc

⚡️ LWC - A Blazing Fast, Enterprise-Grade Web Components Foundation
https://lwc.dev
Other
1.6k stars 386 forks source link

Add a single-file component (SFC) format #3965

Open AllanOricil opened 5 months ago

AllanOricil commented 5 months ago

Is your feature request related to a problem? Please describe. I was reading some vue docs and started to think:

Describe the solution you'd like I would like to be able to create lwc components declaring .html, .css and .js in a single file called my-component.lwc, like in Vue or Svelte.

<template></template>
<script></script>
<style></style>

<!-- meta: platform specific metadata-->
<metadata></metadata>

Describe alternatives you've considered There isn't an alternative.

Additional context N/A

nolanlawson commented 5 months ago

Thanks for opening the issue! This is something we've discussed internally, but haven't committed to implementing. Metadata would probably not be included, but having the template/script/style in one file is something we've considered.

/cc @rui-rayqiu

AllanOricil commented 5 months ago

Did you guys discuss about support that feature that allows a component to have multiple templates, and choose 1 to render at a time based on the render method response? It would be nice if we could still use multiple <template> when using a single file.

You could require a boolean variable in all other templates. The ones without it is the default one. There could exist only 1 without a boolean expression.

<template></template> <!--default-->
<template render={renderA}>A</template>
<template render={renderB}>B</template>

<script>
...
function renderA(){...}
function renderB(){...}
...
</script>

If more than 1 template is due to be rendered at the same time, then throw an exception.