sveltejs / svelte

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

inline compilation of html #2345

Closed btakita closed 5 years ago

btakita commented 5 years ago

One advantage that react has over svelte is the ability to compile in a function. It would be nice if svelte could do the same.

Instead of manipulating a string in:

https://github.com/sveltejs/svelte.technology/blob/master/src/routes/blog/rss.xml.js

something like the following could be used:

import get_posts from '../api/blog/_posts.js';
import { interpret, deindent } from 'svelte/interpreter'

const months = ',Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split( ',' );

function formatPubdate ( str ) {
    const [ y, m, d ] = str.split( '-' );
    return `${d} ${months[+m]} ${y}`;
}

const rss = interpret({post, formatPubdate, get_posts}, deindent`
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
    <title>Svelte blog</title>
    <link>https://svelte.technology/blog</link>
    <description>News and information about the magical disappearing UI framework</description>
    <image>
        <url>https://svelte.technology/favicon.png</url>
        <title>Svelte</title>
        <link>https://svelte.technology/blog</link>
    </image>
    {#each get_posts() as post}
        <item>
            <title>{post.metadata.title}</title>
            <link>https://svelte.technology/blog/{post.slug}</link>
            <description>{post.metadata.description}</description>
            <pubDate>{formatPubdate(post.metadata.pubdate)}</pubDate>
        </item>
    {/each}
</channel>
</rss>
`)

export function get(req, res) {
    res.set({
        'Cache-Control': `max-age=${30 * 60 * 1e3}`,
        'Content-Type': 'application/rss+xml'
    });
    res.end(rss);
}
Rich-Harris commented 5 years ago

Not quite sure I understand the value of this proposal?

Conduitry commented 5 years ago

I don't think this is something we want to facilitate. You should just be compiling in SSR mode (at bundle time) and using that at runtime. If you need to have some sort of runtime compilation of components, you'll need to bring your own (runtime) bundler and set up the sandbox in the particular way that you need it, etc. We don't want to be in the business of blessing what is essentially a server-side eval or worrying about exposing the right things or worrying about keeping people safe. Closing.