New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

@begin/enhance

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@begin/enhance

Server-side rendering for custom elements with template and slots support

  • 2.0.1-next.0
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
4
Weekly downloads
 
Created

enhance✨

Enhance is a module for rendering custom elements on the server.

It enables a web component workflow that embraces templates and slots.

Enhance works really well with Architect.

Install

npm i @begin/enhance

Usage

Author your HTML with custom elements

const html = require('@begin/enhance')()
console.log(html`<hello-world greeting="Well hi!"></hello-world>`)

By default enhance looks for templates in your projects /src/views/templates directory but you can configure where it should look by passing an options object.

const html = require('@begin/enhance')({ templates: '/components' })
console.log(html`<hello-world greeting="Well hi!"></hello-world>`)

An example template used for Server Side Rendering

// Template
module.exports = function HelloWorldTemplate(state={}, html) {
  const { greeting='Hello World' } = state

  return html`
    <style>
      h1 {
        color: red;
      }
    </style>

    <h1>${greeting}</h1>

    <script type=module>
      class HelloWorld extends HTMLElement {
        constructor () {
          super()
          const template = document.getElementById('single-file')
          this.attachShadow({ mode: 'open' })
            .appendChild(template.content.cloneNode(true))
        }

        connectedCallback () {
          console.log('Why hello there 👋')
        }
      }

      customElements.define('hello-world', HelloWorld)
    </script>
  `
}

The template added to the server rendered HTML page

// Output
<template id="hello-world-template">
  <style>
    h1 {
      color: red;
    }
  </style>

  <h1>Hello World</h1>

  <script type=module>
    class HelloWorld extends HTMLElement {
      constructor () {
        super()
        const template = document.getElementById('single-file')
        this.attachShadow({ mode: 'open' })
          .appendChild(template.content.cloneNode(true))
      }

      connectedCallback () {
        console.log('Why hello there 👋')
      }
    }

    customElements.define('hello-world', HelloWorld)
  </script>
</template>

This could also be used to as a static site generator locally. Just console log the output and pipe it to an html page.

FAQs

Package last updated on 02 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts