posthtml-hash

PostHTML plugin for hashing static assets


Keywords
posthtml, posthtml plugin, hash, asset, cache, posthtml-plugin
License
MIT
Install
npm install posthtml-hash@1.2.2

Documentation

posthtml-hash

NPM Build Coverage

posthtml-hash is a PostHTML plugin for hashing static CSS/JS assets to enable caching. hasha is used to generate hashes.

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

Install

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

Usage

const fs = require("fs");
const posthtml = require("posthtml");
const { hash } = require("posthtml-hash");

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

posthtml()
  .use(hash())
  .process(html)
  .then((result) => fs.writeFileSync("./index.html", result.html));

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:

const fs = require("fs");
const posthtml = require("posthtml");
const { hash } = require("posthtml-hash");

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

posthtml()
  .use(
    hash({
      /**
       * Relative path to processed HTML file
       */
      path: "public",

      /**
       * Length of hash
       * Default is 20
       */
      hashLength: 10,
    })
  )
  .process(html)
  .then((result) => fs.writeFileSync("./index.html", result.html));

Examples

Contributing

See the PostHTML Guidelines.

Changelog

License

MIT