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

happo-static

v1.0.0

Published

This library can be used to integrate with happo using your own "static" javascript bundle. Here's how to do it --

Downloads

4

Readme

Introduction

This library can be used to integrate with happo using your own "static" javascript bundle. Here's how to do it --

Installation

First, install the happo-static and happo.io npm libraries.

npm install --save-dev happo-static happo.io

Configuration

Then, create or modify .happo.js and add a generateStaticPackage. Point it to the root of a static folder. In our example, we're using ./static.

// .happo.js
module.exports = {
  apiKey: process.env.HAPPO_API_KEY,
  apiSecret: process.env.HAPPO_API_SECRET,
  generateStaticPackage: () => ({ path: './static' }),
};

The following configuration assumes a pre-built static folder. You can also generate the package on the fly here, something like

// .happo.js
const makeStaticPackage = require('./makeStaticPackage');

module.exports = {
  apiKey: process.env.HAPPO_API_KEY,
  apiSecret: process.env.HAPPO_API_SECRET,
  generateStaticPackage: async () => {
    const pathToStaticFolder = await makeStaticPackage();
    return { path: pathToStaticFolder };
  },
};

Prepare javascript bundle

The happo-static library has two methods you should use when creating your javascript bundle:

happoStatic.init()

Call this method once in your bundle. This will prep the bundle for usage on Happo workers. It doesn't matter when you call init (can be first, last or in between).

happoStatic.registerExample()

Call this method to register your Happo examples. Takes an object with the following structure:

  • component - (string) name of the component
  • variant - (string) name of the component variant
  • render - (async function) render things into the document here

Here's a full example:

// main.js

const happoStatic = require('happo-static');

happoStatic.init();

happoStatic.registerExample({
  component: 'Hello',
  variant: 'red',
  render: () => {
    document.body.innerHTML = '<div style="background-color:red">Hello</div>';
  },
});

happoStatic.registerExample({
  component: 'Hello',
  variant: 'blue',
  render: () => {
    document.body.innerHTML = '<div style="background-color:blue">Hello</div>';
  },
});

Create an iframe.html file

Once you have your bundle, you need a minimal html file to serve the bundle to Happo's workers. Save this file as static/iframe.html (replace "static" with the name of your static folder):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script src="/bundle.js"></script>
  </head>
  <body></body>
</html>

/bundle.js is the path to your javascript bundle. You can assume that the static folder is the root, so in our case, /bundle.js would refer to ./static/bundle.js.

Running happo

Once you have everything setup, you can invoke the happo run command via the command line.

npx happo run

Testing locally

If you serve the static folder (./static in our case) through an http server, you can open up iframe.html and test the integration straight in your browser. You can use http-server for that:

npx http-server ./static

Once the server is up and running, open http://localhost:8080/iframe.html in a browser window. Then, in the javascript console of the page (e.g. through Chrome DevTools), call the following function:

window.happo.nextExample();

This should render the first example. Repeat calling this method until you've rendered all your examples.