npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ubeac/svelte-preprocessors

v0.0.3

Published

Collection of some useful svelte pre-processors

Downloads

40

Readme

Svelte Preprocessors

List of some useful preprocessors for svelte

Usage

you can install this package as devDependency of your svelte based project.

npm install -D @ubeac/svelte-preprocessors

then edit your svelte.config.js file:

import {ifProcessor, previewProcessor} from '@ubeac/svelte-preprocessors'

export default {
    preprocessors: [
        ifProcessor(), // to enable if preprocessor
        previewProcessor() // to enable Preview preprocessor
    ],
    ...other configs
    kit: {
        ...sveltekit configs
    }
}

if Processor

if processor transforms if={...} props to {#if ...} in svelte templates for all Components and DOM Elements.

after installation and enabling if Processor you should be able to use if prop in your .sevlte files.

<Button if={2 + 2 === 4}>
    My Button
</Button>

<p if={!!user}>
    name: {user.name}
    email: {user.email}
</p>

after build process above code will be changed to

{#if 2 + 2 === 4}
<Button>
    My Button
</Button>
{/if}

{#if !!user}
<p>
    name: {user.name}
    email: {user.email}
</p>
{/if}

Example

<div class="actions">
    <button if={user.hasAccess('add-item')}>
        Add
    </button>
    <button if={user.hasAccess('edit-item')}>
        Edit
    </button>
    <button if={user.isAdmin()}>
        Remove
    </button>
</div>

after if processor:

<div class="actions">
    {#if user.hasAccess('add-item')}
    <button>
        Add
    </button>
    {/if}
    {#if user.hasAccess('edit-item')}
    <button>
        Edit
    </button>
    {/if}
    {#if user.isAdmin()}
    <button>
        Remove
    </button>
    {/if}
</div>

typescript support:

you can add if as an attribute for all HTML Elements by adding below code in *.d.ts file in your project.

declare	namespace svelte.JSX {
    interface HTMLAttributes<T> {
        if?: boolean
    }
}

also you need to ignore your editor's Intellisense errors for if={...} prop in Components. everything works in dev-mode and after build. only editor doesn't know that you use ifProcessor.

Alternatively, you can add typing for if prop to your component.


<script lang="ts">

type $$Props = {
    color: string;
    size: string;
    // other props
    if: boolean
}

export let color: string = 'primary'
export let size: string = 'md'
// other props
</script>

<button ...>
    <slot/>
</button>

limitations:

one problem is that you cannot use ifProcessor with slot prop in same element:

<Card>
    <div if={hasHeader} slot="header">
        Header
    </div>
    Body
</Card>
<Card>
    {#if hasHeader}
        <div slot="header"> <!-- cannot have slot="**" inside if conditions -->
            Header
        </div>
    {/if}
    Body
</Card>

Preview Processor

using this processor you can extract source code of svelte components as string.

to get started you should have a Preview component which have some props:

Preview.svelte

<script lang="ts">
    export let markup: string | undefined = undefined;
    export let script: string | undefined = undefined;
    export let style: string | undefined = undefined;
    export let isTypescript: boolean | undefined = undefined;

    $: finalScript = isTypescript ? script.replace('<script>', '<script lang="ts">')
</script>

{finalScript}
<br/>
{markup}
<br />
{style}

here is an example of Preview.svelte component which uses this preprocessor.

this processor is useful when you want to show source code of examples alongside live demo. Example