GithubHelp home page GithubHelp logo

srmullen / svelte-subcomponent-preprocessor Goto Github PK

View Code? Open in Web Editor NEW
44.0 5.0 2.0 109 KB

A Svelte preprocessor for writing more than one component per file.

CSS 4.10% HTML 1.81% JavaScript 81.82% Svelte 12.28%
svelte sveltejs sveltekit javascript preprocessor component-architecture

svelte-subcomponent-preprocessor's Introduction

svelte-subcomponent-preprocessor

npm package

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.

svelte-subcomponent-preprocessor's People

Contributors

srmullen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

svelte-subcomponent-preprocessor's Issues

A component can only have one instance-level <script> element

I'm giving this a try but getting:

1:48:40 PM [vite] Error when evaluating SSR module /src/routes/board/[boardId]/index.svelte: ParseError: A component can only have one instance-level <script> element

That file doesn't have multiple script tags or even a subcomponent defined

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.