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

mic-sample-widget

v1.0.3

Published

A simple widget example

Downloads

13

Readme

  1. Create the Widget (JavaScript or TypeScript) Start by creating a simple widget. For this example, let's assume we're building a JavaScript widget that displays a message inside a .

Example Widget Code: javascript Copiar código // my-widget.js

(function() { // Create a simple widget function function createWidget(elementId, message) { const container = document.getElementById(elementId);

if (!container) {
  console.error(`Element with ID "${elementId}" not found`);
  return;
}

// Create a new div and set its content
const widgetElement = document.createElement('div');
widgetElement.style.padding = '20px';
widgetElement.style.backgroundColor = '#f0f0f0';
widgetElement.style.border = '1px solid #ddd';
widgetElement.textContent = message;

// Append the widget to the container
container.appendChild(widgetElement);

}

// Expose the widget globally so it can be called from the page window.MyWidget = { create: createWidget }; })(); In this code:

The createWidget function takes an elementId and a message. It creates a new element and appends it to the element specified by the elementId. This is wrapped in an IIFE (Immediately Invoked Function Expression) to avoid polluting the global namespace. 2. Set Up npm and Package.json To publish your widget on UNPKG, you'll need to have an npm package. Here's how to set it up:

Initialize npm: Run the following command in the root directory of your widget project:

bash Copiar código npm init This will create a package.json file. When prompted, provide the necessary information like package name, version, description, etc.

Ensure that your package.json has the following fields at a minimum:

json Copiar código { "name": "my-widget", // Unique name of your widget "version": "1.0.0", "main": "my-widget.js", // The entry point to your widget "description": "A simple widget example", "author": "Your Name", "license": "MIT" } 3. Publish the Package on npm Before publishing to npm, you need to have an account on npm. If you don't have one, create an account.

Login to npm: bash Copiar código npm login Provide your npm username, password, and email.

Publish the Package: Once logged in, you can publish your package by running the following command:

bash Copiar código npm publish --access public This will make your package available on npm, and since UNPKG serves npm packages, it will also become available on UNPKG.

  1. Access the Widget on UNPKG Once your package is published, it can be accessed via the UNPKG CDN. The URL will be:

perl Copiar código https://unpkg.com/ For example, if your package is named my-widget and is at version 1.0.0, you can access the widget script via:

perl Copiar código https://unpkg.com/[email protected]/my-widget.js You can use the latest keyword to always load the latest version of your package:

perl Copiar código https://unpkg.com/my-widget@latest/my-widget.js 5. Usage Example for Consumers Once published, anyone can load your widget via a tag and use it in their HTML:

html Copiar código

  1. Updating the Widget If you make changes to your widget, you can publish a new version by updating the version number in package.json (e.g., 1.0.1) and running:

bash Copiar código npm publish Summary of Steps: Write your widget in JavaScript/TypeScript. Create a package.json using npm init. Publish your package to npm with npm publish. Access your widget via UNPKG using the URL format: https://unpkg.com/. Consumers can load and use your widget via a tag.