0.0.12 • Published 1 year ago

svelte-inline-compiler v0.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

MemberDescriptionrequireddefault
modulePath to your .svelte component module. It uses relative path to your package.json.true
nameName of your module. Used as placeholder inside compiled code.true
generateType of bundle to generate.true
propsProps to send to your component.false{}
targetWhere 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.bodyfalsedocument.body
contextUse to send context to the component.falsenew Map()
useCacheCache built bundles. It significantly speeds load times. Uses node-cache.falsefalse
cacheKeyCustom cache key. If none provided, the key that will be used follows ${module}:${name}:${generate} naming conventionfalseundefined
svelteOptionsAll options from esbuild-svelte package except generate, hydratable, immutable, css, preserveComments, preserveWhitespace buildOptions. Those are reserverd for the libraryfalse{}
esbuildOptionsAll esbuild options except entryPoints, write, absWorkingDir, mainFields, plugins. Those are reserved for the libraryfalse{}
esbuildPluginsExtra 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.
0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago