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

posthtml-hash

v1.2.2

Published

PostHTML plugin for hashing static assets

Downloads

585

Readme

posthtml-hash

NPM

posthtml-hash is a PostHTML plugin for hashing file names to enable caching.

<html>
  <head>
-   <link rel="stylesheet" href="styles.[hash].css" />
+   <link rel="stylesheet" href="styles.9a6cf95c41e87b9dc102.css" />
  </head>
  <body>
-   <script src="src.[hash].js"></script>
+   <script src="src.b0dcc67ffc1fd562f212.js"></script>
  </body>
</html>

Install

yarn add -D posthtml-hash
# OR
npm i -D posthtml-hash

Usage

Input

By default, the plugin will attempt to hash file names that contain [hash]. As an additional qualifier, only nodes containing href, src, or content attributes are eligible.

<html>
  <head>
    <!-- ✅ hashed -->
    <link rel="stylesheet" href="style.[hash].css" />

    <!-- ❌ not hashed -->
    <link rel="stylesheet" href="reset.css" />
  </head>
  <body>
    <!-- ✅ hashed -->
    <script src="src.[hash].js"></script>

    <!-- ❌ not hashed -->
    <script src="analytics.js"></script>
  </body>
</html>

Node.js

The recommended usage of this plugin is to incorporate it in your post-build process.

Let's say that you use Rollup to bundle and minify your CSS and JavaScript. The template index.html is copied to the build folder.

// postbuild.js
const fs = require("fs");
const posthtml = require("posthtml");
const { hash } = require("posthtml-hash");

const html = fs.readFileSync("./build/index.html");

posthtml()
  .use(hash({ path: "build" }))
  .process(html)
  .then((result) => fs.writeFileSync("./build/index.html", result.html));

For convenience, you can add the post-build script to your package.json. The postbuild script is automatically invoked following the build script.

{
  "scripts": {
    "build": "rollup -c",
    "postbuild": "node postbuild.js"
  }
}

Custom Hash Length

Customize the hash length by specifying an integer after the hash:{NUMBER}. The default hash length is 20.

Note: This only works for a pattern that uses square brackets and a colon separator. Use the hashLength option for different patterns.

<script src="src.[hash].js"></script>
<!-- src.b0dcc67ffc1fd562f212.js -->

<script src="src.[hash:8].js"></script>
<!-- src.b0dcc67f.js -->

Options

This plugin assumes that the file to process is in the same directory as the PostHTML script. If not, specify the relative path to the html file in options.path:

hash({
  /**
   * Relative path to the HTML file being processed
   * @default ""
   */
  path: "public",

  /**
   * File name pattern (regular expression) to match
   * @default new RegExp(/\[hash.*]/g)
   */
  pattern: new RegExp(/custom-file-pattern/),

  /**
   * Hash length
   * @default 20
   */
  hashLength: 8,

  /**
   * Transform the href/src/content attribute value to a relative file path
   * @default (filepath) => filepath
   */
  transformPath: (filepath) => filepath.replace("https://example.com/", ""),
});

Recipes

Custom Pattern and Hash Length

hash({
  pattern: new RegExp(/custom-file-pattern/),
  hashLength: 8,
});

Result:

- <script src="script.custom-file-pattern.js"></script>
+ <script src="script.b0dcc67f.js"></script>

Remote origin URLs

Input HTML:

<head>
  <meta charset="utf-8" />
  <!-- We want to hash this image file name and preserve the remote origin URL -->
  <meta property="og:image" content="https://example.com/image.[hash].png" />
</head>
hash({
  transformPath: (filepath) => {
    // removes the targeted remote origin URL when looking up the files locally
    return filepath.replace("https://example.com/", "");
  },
});

Examples

See the examples folder for end-to-end use cases.

Contributing

See the PostHTML Guidelines.

Changelog

License

MIT