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

@lcf.vs/dom-engine

v5.5.4

Published

A composable DOM based template engine

Downloads

23

Readme

@lcf.vs/dom-engine

A composable DOM based template engine

Install

npm i @lcf.vs/dom-engine

Usage

Markers

There is 2 type of markers:

  • Required: {name}
    • The value can't be nullish
  • Optional: {?name}
    • If a nullish value is provided for an attribute, that attribute is removed

Create a fragment template

import { template } from '@lcf.vs/dom-engine'

const pTemplate = template(`<p class="{?classes}">{salutations} {?name}</p>`, {
  classes: null,
  salutations: null,
  name: null
})

Create a fragment template, using the source symbol

import { source } from '@lcf.vs/dom-engine'

const pTemplate = {
  [source]: '<p class="{?classes}">{salutations} {?name}</p>',
  classes: null,
  salutations: null,
  name: null
}

Create a value filled node

// object representing <p>hello user-name</p>
const p = {
  ...pTemplate,
  salutations: 'hello',
  name: 'user-name'
}

// object representing <p>hello</p>
const p = {
  ...pTemplate,
  salutations: 'hello'
}

// Error: missing {salutations} (on serialization)
const p = {
  ...pTemplate
}

Use templates as value

const taggedName = {
  [source]: '<strong>{name}</strong>',
  name: null
}

// object representing <p>hello <string>user-name</strong></p>
const p = {
  ...pTemplate,
  salutations: 'hello',
  name: {
    ...taggedName,
    name: 'user-name'
  }
}

// object representing <p>hello <strong>user-name</strong></p>
const p = {
  ...pTemplate,
  salutations: 'hello',
  name: {
    ...taggedName,
    name: 'user-name'
  }
}
// object representing <p>hello <strong><span>user-name</span></strong></p>
const p = {
  ...pTemplate,
  salutations: 'hello',
  name: template(`<span>user-name</span>`)
}
// object representing <p>hello <strong><span>user</span>-<span>name</span></strong></p>
const p = {
  ...pTemplate,
  salutations: 'hello',
  name: [
    template(`<span>user</span>`),
    template(`-`),
    template(`<span>name</span>`)
  ]
}

Use object properties

// object representing <p>hello <strong>user</strong></p>
const pTemplate = {
  [source]: '<p>{salutations} <strong>{user.name}</strong></p>',
  salutations: 'hello',
  user: {
    name: 'user'
  }
}

Serialize a template

import { serialize } from '@lcf.vs/dom-engine'

const p = template(`<p>user-name</p>`)
const html = await serialize(p)

APIs

import { template, source } from '@lcf.vs/dom-engine'

const tpl = template(html, { ...fields } = {})

// or 

const tpl = {
  [source]: html,
  ...fields
}

Back-End

import { load } from '@lcf.vs/dom-engine'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...options } = {}, { ...data } = {})

Where the options are the async readFile() ones

import { serialize } from '@lcf.vs/dom-engine'

const htmlString = await serialize(template)

Or

import { render } from '@lcf.vs/dom-engine'

const htmlNode = render(template)

Front-End

import { load } from '@lcf.vs/dom-engine'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...options } = {}, { ...data } = {})

Where the options are the fetch() ones

import { serialize } from '@lcf.vs/dom-engine'

const htmlString = await serialize(template)

Or

import { render } from '@lcf.vs/dom-engine'

const htmlNode = render(template)

ServiceWorker

The engine also supports the rendering of some templates stored into a ServiceWorker.

To use it, you need to add the following components.

Into the window script

import '@lcf.vs/dom-engine/lib/worker-client.js'

Into the ServiceWorker script

import { load } from '@lcf.vs/dom-engine'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...optio*ns } = {}, { ...data } = {})

Where the options are the fetch() ones

import { serialize, source } from '@lcf.vs/dom-engine'

const text = 'This page is built by sw-template'

const view = {
  [source]: `<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{text}</title>
  <script src="/main.js" type="module"></script>
</head>
<body>
  <main>
    <h1>{text}</h1>
    <a href="/">back to home</a>
  </main>
</body>
</html>`
}

const html = await serialize({
  ...view,
  text
})

Just note that: if the browser doesn't support the ES6 modules, into the ServiceWorker, you should need to bundle your code.

A very basic demo

Security

To improve your client-side security, you can add the following header on your requests (or via a <meta>)

Content-Security-Policy: default-src 'none'; connect-src 'self'; script-src 'self'; style-src 'self'; require-trusted-types-for 'script'; trusted-types dom-engine

Since the templates filling is intended to avoid XSS injections, by design, you need to be sure about your templates.

Of course, it's just a minimal one, you can adapt it on your needs.

License

MIT