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

baresm

v0.1.0

Published

web service worker to resolve es6 bare module specifiers

Downloads

3

Readme

baresm

Web service worker to resolve es6 bare module specifiers.

allows the use of npm installed package.json dependencies in node_modules as es6 module imports. eg.

index.html

<script type="module" src="index.mjs"></script>

index.mjs

import f, { n } from "some-lib";

f();
n();

why?

Browsers that support es6 module loading only do so with relative or absolute paths. package.json dependencies referenced in import statements will not be resolved by browsers. baresm.js will intercept bare module specifiers in javascript modules and rewrite them to absolute paths allowing you to use package.json dependencies in your module without any packaging / build stage of your code.

should I use this?

No. This is proof of concept and ultimately a stop-gap until browsers support bare module specifiers, if ever. The web specification for es6 module dependencies will likely look different to analyzing package.json files and resolving node_modules paths. Moreover, many typical/popular node_modules won't execute correctly without a packaging step directly in the browser and/or are very large and ultimatley unnecessary to deliver effectively your entire raw node_modules to browsers.

If you decide to use this:

  • use HTTP/2, streams will make a huge difference for loading a ton of small files.
  • ensure your dependencies can execute directly in the browsers you're targetting.
  • reconsider using this

install

npm install --save baresm

copy baresm.js to your web root

if you web server supports following symlinks, create a symlink as a sibling to your index.html, ie

ln -s node_modules/baresm/baresm.js .

otherwise, copy:

cp node_modules/baresm/baresm.js .

index.html

<script>
  if ("serviceWorker" in navigator) {
    window.addEventListener("load", () => navigator.serviceWorker.register("/baresm.js?your-version"));
  }
</script>

replace your-version with a version token that reflects your module and/or dependency changes.

Alternatively use Service-Worker-Allowed header

have your index request serve Service-Worker-Allowed header as / so /node_modules/baresm/baresm.js may use the root scope /

Service-Worker-Allowed: /

index.html

<script>
  if ("serviceWorker" in navigator) {
    window.addEventListener("load", () => navigator.serviceWorker.register("/node_modules/baresm/baresm.js?your-version", { scope: "/" }));
  }
</script>

caching

baresm.js will create a node_modules cache key and will effectively cache any fetch requests for your origin that are Content-Type application/javascript. It is advised to version your service worker registration to ensure you reload this cache whenever you change your code and/or package dependencies.