4.0.0 • Published 5 months ago

@ethercorps/svelte-h2j v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Contributors Forks Stargazers Issues MIT License LinkedIn

Key Features

  • Convert html/css to jsx
    • Useful when you are going to use satori with Svelte & Kit.
  • Compatible with new version of svelte
    • As new versions are coming for better performance in svelte we maintain that.
  • Easy to use with all available syntax in html css.
    • Support for class based internal style.
    • inline css

How To Use

  • Install @ethercorps/svelte-h2j using your favourite node package manager.
# NPM
$ npm install @ethercorps/svelte-h2j css-tree
# PNPM
$ pnpm install @ethercorps/svelte-h2j css-tree
# Yarn
$ yarn add @ethercorps/svelte-h2j css-tree
# bun
$ bun install @ethercorps/svelte-h2j css-tree
  • Add vite plugin to vite.config.[js,ts]
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import {vitePluginSvelteH2J} from "@ethercorps/svelte-h2j/vite"

export default defineConfig({
	plugins: [sveltekit(), vitePluginSvelteH2J()],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}']
	}
});

This is required as we build for production and add css-tree while build.

  • To convert html, css to JSX (Also supports Tailwind, inline css)
import {toReactElement} from "@ethercorps/svelte-h2j"
const htmlString =`
 <div tw="bg-gray-50 flex w-full h-full items-center justify-center">
    <div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
      <h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
        <span>Ready to dive in?</span>
        <span tw="text-indigo-600">Start your free trial today.</span>
      </h2>
      <div tw="mt-8 flex md:mt-0">
        <div tw="flex rounded-md shadow">
          <a href="#" tw="flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-white">Get started</a>
        </div>
        <div tw="ml-3 flex rounded-md shadow">
          <a href="#" tw="flex items-center justify-center rounded-md border border-transparent bg-white px-5 py-3 text-base font-medium text-indigo-600">Learn more</a>
        </div>
      </div>
    </div>
  </div>
`;

const jsx = toReactElement(htmlTemplate)  // Takes a string only
const svg = await satori(jsx, {
  width: options.width,
  height: options.height,
  debug: options.debug,
  fonts: options.fonts || [
    {
      name: 'sans serif',
      data: fontData,
      style: 'normal',
      weight: 700
    }
  ]
});
  • We also provide a server side api for converting svelte components to JSX.
import {svelteComponentToJsx} from "@ethercorps/svelte-h2j";
import SvelteComponent from "SvelteComponent.svelte"
const jsx = svelteComponentToJsx(SvelteComponent, props = {
	a: 2 // if you have `export let a;` in component
}) // Takes two parameters 1. Component 2. Component Props

const svg = await satori(jsx, {
  width: options.width,
  height: options.height,
  debug: options.debug,
  fonts: options.fonts || [
    {
      name: 'sans serif',
      data: fontData,
      style: 'normal',
      weight: 700
    }
  ]
});

Credits

This software uses the following open source packages:

Related

satori-html - Similar but uses other libraries as dependency when we can approach with svelte compiler in sveltekit.

You may also like...

License

MIT


ethercorps.io  ·  GitHub @theetherGit  ·  Twitter @theether0

4.0.0

5 months ago

0.1.0

7 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.1

9 months ago