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

svelte-subcomponent-preprocessor

v0.0.1

Published

svelte-subcomponent-preprocessor ================================

Downloads

8

Readme

svelte-subcomponent-preprocessor

The svelte-subcomponent-preprocessor allows you to write more than one component within a svelte file. The subcomponents are written in a {#component} block and remain local to the svelte file. Here's an example

<!-- List.svelte -->
<script>
  export let items = ['svelte', 'subcomponent', 'preprocessor'];
</script>

<ul>
  {#each items as item}
    <Item {item} />
  {/each}
</ul>

{#component Item}
  <script>
    import { onMount } from 'svelte';
    export let item;

    onMount(() => {
      console.log(item);
    });
  </script>

  <li>{item}!!!</li>

  <style>
    li {
      color: red;
    }
  </style>
{/component}

Installation and Configuration

npm install --save-dev svelte-subcomponent-preprocessor

In your svelte config import the preprocessor and add it to the preprocess array.

import subcomponentPreprocessor from 'svelte-subcomponent-preprocessor';
import sveltePreprocess from 'svelte-preprocess';

svelte({
  preprocess: [
    subcomponentPreprocessor(),
    sveltePreprocess() // must come after subcomponentPreprocessor
  ]
})

If you're using svelte-preprocess it must run after svelte-subcomponent-preprocessor.

svelte-subcomponent-preprocessor works by extracting the {#component} blocks from your svelte code and writing them to disk. By default they are written to ./node_modules/.svelte-subcomponent-preprocessor/. This can be changed with a configuration object passed to the preprocessor.

subcomponentPreprocessor({ out: './subcomponents' });

Config with Vite or SvelteKit

If you're using Vite or SvelteKit you'll need a bit of extra configuration to get the subcomponents to work with the dev server. You need the following in your vite config.

{
  // ...
  server: {
    watch: {
      ignored: ['!**/node_modules/.svelte-subcomponent-preprocessor/**']
    }
  },
  optimizeDeps: {
    exclude: ['.svelte-subcomponent-preprocessor']
  },
  // ...
}

Full vite configuration.

import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [svelte()],
  server: {
    watch: {
      ignored: ['!**/node_modules/.svelte-subcomponent-preprocessor/**']
    }
  },
  optimizeDeps: {
    exclude: ['.svelte-subcomponent-preprocessor']
  }
});

Or in SvelteKit the server and optimizeDeps config would go inside the svelte.config.js vite object.

Config with Snowpack

With snowpack the default out configuration will not work. Change the out location to someplace that will be watched by the snowpack dev server.

Usage

To define a subcomponent put your component code inside a #component block. Pass the name of the subcomponent to the block, like so...

{#component ComponentName}    
  <h1>My Component</h1>
{/component}

<div>
  <ComponentName />
<div>

Limitations

  • Does not currently work with snowpack. It may be possible to change the snowpack config to get it to work, but I'm not sure how.
  • Subcomponents cannot have interdependencies. i.e. Only the default export component can use the subcomponents.