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

babel-plugin-add-contenthash-to-imports

v0.3.0

Published

Add content hash to import module names (static and dynamic): import('./mod.js') => import('./mod.h45H.js')

Downloads

7

Readme

Add content hash to imports Babel Plugin

This Babel plugin adds hashes to static and dynamic import module names for cache busting.

For example:

import foo from './js/foo.js';

import('./bar.js').then(bar => bar());

is transformed into

import foo from './js/foo.abcd1234.js?';

import('./bar.1234abcd.js').then(bar => bar());

This is intended for use in multipage web applications (more on that later), so it only works with relative module names (e.g., ./foo.js or ../bar/baz.js) with .js extension. Feel free to adjust to your needs, obviously.

The content hash is simply the first 8 characters of the file's md5.

Why?

Now that browsers support import natively, web developers can use it without transpilers or bundlers (like webpack).

This not only makes deploying code easier because less tooling is required. For multipage apps (MPAs), configuring dozens (or even hundreds) of entry points with splitChunks is a nightmare.

There is one major downside of using imports natively however, namely caching.

Mapping module name to URLs

Since module names for static imports must be, well, static, we can't change the URL that will be fetched.

In principe, we could improve dynamic imports with something like:

const VERSION = '1.2.3-abc';     // global constant
function cacheBustImport (mod) {
    return import(`${mod}?v=${VERSION}`);   // e.g. foo.js?v=1.2.3-abc
}

cacheBustImport('./foo.js')
    .then(mod => console.log(mod));

But this is nasty. Also, since this breaks static code analysis, IDEs and build tools won't be able to analyze the imports.

Caching

Since we can't add the build version or content hash to enable Cache-Control: immutable, we need to fall back to must-revalidate.

If we author code with deep dependency trees, this will cause a huge number of sequential requests. HTTP/2 helps performance significantly, especially when combined with modulepreload.

Unfortunately, browser support for modulepreload is (currently) awful, plus it does still require quite a bit of effort to make it work.

Solution: content hash in URL

The ideal solution is to add a content hash for each imported file. This is a very old trick, so I was very surprised when I couldn't find any solution for imports (other than bundlers).

Luckily, writing plugins for Babel is a breeze (I myself have no experience with compilers and wrote this in a few hours!).