sveltejs / svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
MIT License
1.73k stars 147 forks source link

Request: Ability to send locals to Pug template #595

Open JL102 opened 1 year ago

JL102 commented 1 year ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. For example: I'm always frustrated when [...] I'm looking to port my existing web app from ExpressJS/Pug to SvelteKit, and it would be really helpful if I can use the Pug preprocessor to save us a lot of time, i.e. we could just copy the code over to our new Svelte files without having to rewrite everything to Svelte all at once. The issue preventing me from doing this is that we pass a lot of variables and functions into our Pug files, such as i18n code (e.g. div!=msg('index.hello') becomes <div>Hello world!</div>) and variables from the database. It seems like the Svelte code transformer doesn't pass any locales into the compiled Pug function, so I don't see a way to pass our i18n msg function into there.

Describe the solution you'd like I would really like it if I can pass data into the template function. Maybe like this:


<script lang='ts'>

    import type { PageData } from './$types';
    import { msg } from '$lib/i18n';

    export let data: PageData;

</script>

<template lang="pug" locales={{
    data,
    msg
}}>

    if data.foo
        div!=msg('hello world')

</template>

Describe alternatives you've considered For if / each statements, I imagine I could end the