sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.76k stars 4.23k forks source link

Save source of Svelte components to a variable for documentation purposes #4035

Closed Wolfr closed 4 years ago

Wolfr commented 4 years ago

Is your feature request related to a problem? Please describe.

In a template-based environment one can easily re-include an include as source code. But documenting the source code of Svelte components as they are is not possible at the moment, because the code immediately gets parsed.

Describe the solution you'd like

When you add @saveSource="varName" to a component, a variable that contains that component’s source code is saved to the global namespace by the compiler without the @saveSource attribute and its value.

This is useful because then the source can be re-used in documentation without having to manually repeat it:

Example:

<Alert type="default" @saveSource="alertDefault">
<Prism code="alertDefault">

(using svelte-prism)

How important is this feature to you?

This is important to me to be able to develop a Svelte based UI kit. Svelte code is largely very elegant. Repeating code blocks in multiline strings is not.

Additional context

I am working on a project called Bedrock 2, it is the successor to Bedrock. It is a few things a) way to prototype web applications b) a method to document design systems c) a “design deliverable” to a larger development team.

Screenshot 2019-12-01 at 18 27 47

You can find the code that I find inelegant here.

You can view a demo of Bedrock 2 here.

You can find examples of Material Design and Bootstrap documented Bedrock style here:

This should give you an idea of what I aim to achieve in the end using newer technology (Svelte instead of a custom SSG)

Conduitry commented 4 years ago

Since the compiler would have no way of knowing which components you want to be able to use @saveSource= on, it would have to support it on all of them - which would mean that every component's compiled code would have to contain its .svelte source just in case you want to use @saveSource. This isn't acceptable.

This seems like a rather niche case, and not something that would warrant adding and documenting a compiler flag for.

To avoid duplicating code, I'd suggest writing a custom preprocessor that does this for you,