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

@pepper-js/pepper

v0.2.5

Published

Bring your server-side templates to life

Downloads

10

Readme

Pepper

NOTE: Project is still a work-in-progress

Build interactive islands with plain HTML and a touch of JS.

Bundle size - pepper.js is 2.4 KB gzipped

Example

<!DOCTYPE html>
<html>
    <body>
        <div id="node-to-sync">
            <button on-click="onClick">Test</button>
        </div>
        <script type="module">
            import { Pepper, html } from 'https://unpkg.com/@pepper-js/pepper';
            const view = new Pepper({
                getHtml(data) {
                    return html`<button on-click="onClick">${data.text}</button>`;
                    // html tagged template literal escapes ${} interpolations and returns a string
                    // so that script tags or the like doesn't get executed.
                    // if the value is safe, then you can prefix interpolation by a $ sign,
                    // e.g.html`<div>$${'<img src="x" onerror="alert(1)">'}`.
                    //
                    // or you can instead use a template library here
                },
                
                data: { text: 'Test' },
                target: '#node-to-sync',
                hydrate: true, // optional
                
                onClick() {
                    this.data = { text: 'Clicked!' }; // this automatically updates the DOM
                }
            });
            // or you can call view.hydrate() here.
            // or call view.mount(), to create new DOM nodes
        </script>
    </body>
</html>

You can find examples for several templating language in the examples directory.

Import from CDN

<!-- Module import -->
<script type="module">
    import { Pepper } from 'https://unpkg.com/@pepper-js/pepper';
</script>

<!-- Global import -->
<script src="https://unpkg.com/@pepper-js/pepper/dist/browser/global/index.min.js"></script>
<script>const { Pepper } = PepperModule</script>

Update data and view

view.data = { text: 'Test 2' }; // uses setter to detect change

Or use view.assign() to not overwrite existing props

view.assign's signature is exactly like Object.assign().

Note: Updating states updates the DOM immediately (synchronous/blocking call). So it is generally a good idea to reduce state changes to a single call per user action.. for example a click action would call view.assign() only once. You can use temporary objects if needed to reduce calls.

Refs to DOM nodes

<button ref="btnEl" on-click="onClick">
    {{ text }}
</button>

Now you can use this.btnEl (inside a view method) or view.btnEl (from outside) to access the span element.

Debug access

One can do targetElement.pepperInstance to get access to the view object from the developer tools. It is only for debugging purposes. Never use it in code.

Pepper Store - for managing cross-view states

Pepper comes with a simplified global state store, so that you can have multiple views with common states stored in it. Updating the store data will re-render connected views automatically.

import { Store } from 'https://unpkg.com/@pepper-js/pepper';

// initialize global store
const store = new Store({
    count: 1
});

// create some views that use the store data.
const view1 = new Pepper({
    // if you want to be able to access a property from the store, then
    // you need to explicitly subscribe for that store property. This is a performance
    // optimization (like redux).
    stores: {
        counter: {
            store: store,
            props: ['count']
        }
    },
    getHtml: data => html`<span>Counter = ${ data.stores.counter.count }</span>`,
    target: '#myview1',
    mount: true,
});
const view2 = new Pepper({
    getHtml: html`<span>Counter = ${ data.stores.counter.count }</span>`,
    stores: {
        counter: {
            store: store,
            props: ['count']
        }
    },
    target: '#myview2',
    mount: true,
});

// demonstrating how updating one data source, re-renders multiple views
// so.. update counter
const incrementCounterAction = () => {
    store.assign({
        count: store.data.count + 1
    });
};
window.setInterval(incrementCounterAction, 1000);

Note that if you don't "connect" your view to specific properties from the Pepper store, then you cannot access those property at all.

Note that stores feature is meant for render performance improvement. You can naively put all your HTML within a single Pepper view and all the states within it. But that could take a hit on rendering performance. So Pepper Store gives you an option to make smaller views / islands, while sharing some states, keeping the rest of the HTML static and refreshing only the views that needs a refresh (with some manual "connecting" from the developer's end).

Run side effects on store properties change

You can listen to store changes outside of Pepper views and run side effects.

store.subscribe(['property1', 'property2'], function effect(propertiesThatChanged) {
    // if `property1` or `property2` (or both) changes this function is invoked
    // `propertiesThatChanged` gives you the exact properties that changed (array of strings).

    // do something here..
    // like lazy load your other views and hydrate them or whatever

    // optionally unsubscribe if you want to only run the effect once.
    store.unsubscribe(effect);
}, /* (optional param) context / this */);

Server-side rendering

If you use a template engine then that's your server-side rendering :)

But if you used no template engine, but hand-wrote getHtml(), then you can import Pepper and your views with node.js

// ESM
import { Pepper, Store, html } from '@pepper-js/pepper';
// CJS
const { Pepper, Store, html } = require('@pepper-js/pepper')

// const pepperView = new Pepper(...)
const html = pepperView.toString();
// or html = `${pepperView}`

Browser compatibility

Supports every browser as GOV UK (2022) - https://www.gov.uk/service-manual/technology/designing-for-different-browsers-and-devices

(Safari 12+ and latest Chrome, Edge, Firefox, Samsung Internet)

Credits

To udomdiff for dom diff fast path inspiration.