All Projects → joshnuss → micro-svelte-compiler

joshnuss / micro-svelte-compiler

Licence: MIT License
Micro Svelte compiler (naive clone)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to micro-svelte-compiler

Palettes
A tool for creating nice, percerptually correct and colorblind-safe color palettes.
Stars: ✭ 155 (+176.79%)
Mutual labels:  generator, svelte
kumiko
A kumiko pattern generator, based on any image
Stars: ✭ 42 (-25%)
Mutual labels:  generator, svelte
yii2-rest-doc
Yii2 REST doc generator
Stars: ✭ 35 (-37.5%)
Mutual labels:  generator
EddieHubCommunity.github.io
Information about our community
Stars: ✭ 88 (+57.14%)
Mutual labels:  svelte
plate-app
🍛 What's on your Plate?
Stars: ✭ 49 (-12.5%)
Mutual labels:  svelte
agnosticui
AgnosticUI is a set of UI primitives that start their lives in clean HTML and CSS. These standards compliant components are then copied to our framework implementations in: React, Vue 3, Angular, and Svelte.
Stars: ✭ 326 (+482.14%)
Mutual labels:  svelte
metagraf
metaGraf is a opinionated specification for describing a software component and what its requirements are from the runtime environment. The mg command, turns metaGraf specifications into Kubernetes resources, supporting CI, CD and GitOps software delivery.
Stars: ✭ 15 (-73.21%)
Mutual labels:  generator
dotto.x
A tiny state manager for React, Svelte, Vue and vanilla JS
Stars: ✭ 104 (+85.71%)
Mutual labels:  svelte
awesome-svelte-kit
Curated resources on building sites with SvelteKit - A server-less-first answer to "the Next.js experience" by the Svelte community
Stars: ✭ 55 (-1.79%)
Mutual labels:  svelte
svelte-headlessui
Unofficial Svelte port of Headless UI components
Stars: ✭ 564 (+907.14%)
Mutual labels:  svelte
svelte-sheets
Blazing fast excel sheets in the browser, hugely inspired by JExcel, built with Svelte and XLSX.
Stars: ✭ 45 (-19.64%)
Mutual labels:  svelte
programmingtil-svelte
No description or website provided.
Stars: ✭ 59 (+5.36%)
Mutual labels:  svelte
p5-svelte
Easily add p5 sketches to a Svelte project 🍛 🌱
Stars: ✭ 94 (+67.86%)
Mutual labels:  svelte
routify-ts
Use Typescript in Routify Starter Template.
Stars: ✭ 21 (-62.5%)
Mutual labels:  svelte
blog-template
A markdown-style blog template for Sapper
Stars: ✭ 29 (-48.21%)
Mutual labels:  svelte
svelte-spinkit
A collection of loading indicators animated with CSS for Svelte
Stars: ✭ 27 (-51.79%)
Mutual labels:  svelte
username-generation-guide
A definitive guide to generating usernames for OSINT purposes
Stars: ✭ 38 (-32.14%)
Mutual labels:  generator
yang-schematics
Yet Another Angular Generator - based on @schematics/angular
Stars: ✭ 16 (-71.43%)
Mutual labels:  generator
mosaic-node-generator
Generate mosaic images in Node.
Stars: ✭ 25 (-55.36%)
Mutual labels:  generator
DeLorean
Time-traveling debugger for Svelte applications
Stars: ✭ 58 (+3.57%)
Mutual labels:  svelte

Micro Svelte Compiler

A demonstration of how the Svelte.js compiler works under the hood.

Installation

Download and install dependencies:

git clone https://github.com/joshnuss/micro-svelte-compiler
cd micro-svelte-compiler && yarn

Compiler Stages

This compiler has multiple stages:

  1. Parse the .svelte file and extract code from <script> tags and build a list of non-<script> tags.
  2. Parse the code and determine props (anything with export let ... is a prop)
  3. Parse the tags recursively and make an ordered list of nodes and event listeners
  4. Generate JavaScript code using props, nodes, listeners, and code from script tags
  5. Format the JavaScript code
  6. Print the result to stdout

Dependencies

It uses similar dependencies to svelte.js (except for HTML parsing).

  • acorn: Parses JavaScript text into AST.
  • code-red: Generates JavaScript AST from template strings. Converts AST back to string.
  • js-beautify: Formats JavaScript text.
  • parse5: Parses HTML tags.

Usage

Say you have a .svelte file like examples/basic.svelte:

<script>
  export let name;

  function handleClick(e) {
    e.preventDefault()
    alert(`Hello ${name}!`)
  }
</script>

<h1 class="snazzy" on:click=handleClick>Hello {name}!</h1>

Run the compiler on it:

npx msv examples/basic.svelte > examples/basic.js

It generates a JavaScript file that looks like this:

export default function component({ target, props }) {
  let { name } = props;

  function handleClick(e) {
    e.preventDefault();
    alert(`Hello ${name}!`);
  }

  let e0, t1, b2, t3;

  return {
    create() {
      e0 = document.createElement("h1")
      t1 = document.createTextNode("Hello ")
      b2 = document.createTextNode(name)
      t3 = document.createTextNode("!")

      e0.setAttribute("class", "snazzy")
      e0.addEventListener("click", handleClick)
    },
    mount() {
      e0.appendChild(t1)
      e0.appendChild(b2)
      e0.appendChild(t3)

      target.append(e0)
    },
    update(changes) {
      if (changes.name) {
        b2.data = name = changes.name
      }
    },
    detach() {
      target.removeChild(e0)
    }
  };
}

To host the component in the browser:

<script src="example/basic.js"></script>
<script>
  // instantiate the component with a target node and some props
  const c = component({target: document.body, props: {name: "Steve Wozniak"}})
  // create DOM nodes
  c.create()
  // mount DOM nodes
  c.mount()

  // you can update props anytime:
  c.update({name: "Elon Musk"})

  // and cleanup the dom when it's no longer needed
  c.detach()
</script>

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].