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-inline-bundler

v0.0.13

Published

This package is geared mainly towards those who want to build a svelte bundle server side. It can compiler `dom`, `ssr` or `hydrate` Svelte bundle. It uses `esbuild` in order to generate a bundle and returns the code.

Downloads

100

Readme

Svelte inline compiler

This package is geared mainly towards those who want to build a svelte bundle server side. It can compiler dom, ssr or hydrate Svelte bundle. It uses esbuild in order to generate a bundle and returns the code.

It provides a way to bundle Svelte code:

Examples

You can find examples on CodeSandbox here:

https://codesandbox.io/p/sandbox/svelte-inline-compiler-7uek70?file=%2Findex.js

Install

npm install svelte-inline-compiler

Bundling

In order to bundle Svelte app, you need to import the bundler.

import createBundle from "svelte-inline-compiler";

const bundle = await createBundle(options);
// createBundle will return following

// If building a "ssr" bundle

type SSRBundleResult = {
  css: {
    code: string;
    map: Map<string, string> | null;
  };
  head: string;
  html: string;
};

// If building a "hydrate" bundle

type HydratableBundleResult = {
  ssr: SSRBundleResult;
  dom: string;
};

// If building a "dom", it will return a string of created bundle

//Examples:

const { css, head, html } = createBundle(...options, {
  generate: "ssr",
});

const {
  ssr: { css, head, html },
  dom,
} = createBundle(...options, {
  generate: "hydrate",
});

const bundle = createBundle(...options, {
  generate: "dom",
});

createBundle takes the following options:

type CompilerArgs = {
  module: string;
  name: string;
  generate: "ssr" | "dom" | "hydrate";
  props?: unknown;
  target?: string;
  context?: Map<string, string>;
  useCache?: boolean;
  cacheKey?: string;
  svelteOptions?: SvelteOptions;
  esbuildOptions?: EsbuildOptions;
  esbuildPlugins?: EsbuildPlugins;
};

Options description

| Member | Description | required | default | | :------------- | :---------- | :------ | :----- | | module | Path to your .svelte component module. It uses relative path to your package.json. | true | | | name | Name of your module. Used as placeholder inside compiled code. | true | | | generate | Type of bundle to generate. | true | | | props | Props to send to your component. | false | {} | | target | Where the dom bundle will attach og hydrate bundle will hydrate. If you want to attach to some id in the dom, use # prefix. It uses document.query to find the element under the hood. Use: document.body or "#some-id". Defaults to document.body | false | document.body | | context | Use to send context to the component. | false | new Map() | | useCache | Cache built bundles. It significantly speeds load times. Uses node-cache. | false | false | | cacheKey | Custom cache key. If none provided, the key that will be used follows ${module}:${name}:${generate} naming convention | false | undefined | | svelteOptions | All options from esbuild-svelte package except generate, hydratable, immutable, css, preserveComments, preserveWhitespace buildOptions. Those are reserverd for the library | false | {} | | esbuildOptions | All esbuild options except entryPoints, write, absWorkingDir, mainFields, plugins. Those are reserved for the library | false | {} | | esbuildPlugins | Extra esbuild plugins. | false | [] |

Examples

examples folder contains an express server with examples and Svelte view.

It has 4 views (todo, virtual-list, hacker-news and smui) which all vary in complexity.

To see the views use base routes like this:

/hydratable route shows how hydratable bundle looks like when served with Express.

/ssr route shows how ssr bundle looks like when served with Express.

/dom route shows how client bundle looks like when served with Express.

And send view as a query parameter like this:

/hydratable/todo

List of all views

smui is the most resource heavy because it compiles components from Svelte Material UI.

Running the server

  1. cd examples
  2. npm install
  3. npm run dev
  4. Go to http://localhost:4000

This should display the view made with App.svelte.

To check the difference between cached and non cached views, send query param useCache=true|false on a route.

Example:

hydratable/smui?useCache=true|false

List of all example routes

Gotchas

  • ssr bundle will not expose any event handlers. It will only expose html, css and head from <svelte:head /> if used. In order to add events to this bundle, you will need to get elements in the <svelte:head /> and send that to the dom.
  • hydrate bundles both dom and ssr so it will take a bit longer time to compile and send.
  • dom bundle only bundles js code. All of the other pros and cons with client code rendering applies here as well.