|
2 | 2 |
|
3 | 3 | [![NPM][npm]][npm-url] |
4 | 4 |
|
5 | | -`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static CSS/JS assets to enable caching. [hasha](https://www.npmjs.com/package/hasha) is used to generate hashes. |
| 5 | +`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static assets to enable caching. |
6 | 6 |
|
7 | 7 | ```diff |
8 | 8 | <html> |
9 | 9 | <head> |
10 | | -- <link rel="stylesheet" href="stylesheet.css" /> |
11 | | -+ <link rel="stylesheet" href="stylesheet.9a6cf95c41e87b9dc102.css" /> |
| 10 | +- <link rel="stylesheet" href="styles.[hash].css" /> |
| 11 | ++ <link rel="stylesheet" href="styles.9a6cf95c41e87b9dc102.css" /> |
12 | 12 | </head> |
13 | 13 | <body> |
14 | | -- <script src="main.js"></script> |
15 | | -+ <script src="main.b0dcc67ffc1fd562f212.js"></script> |
| 14 | +- <script src="src.[hash].js"></script> |
| 15 | ++ <script src="src.b0dcc67ffc1fd562f212.js"></script> |
16 | 16 | </body> |
17 | 17 | </html> |
18 | 18 | ``` |
|
22 | 22 | ```bash |
23 | 23 | yarn add -D posthtml-hash |
24 | 24 | # OR |
25 | | -npm i posthtml-hash |
| 25 | +npm i -D posthtml-hash |
26 | 26 | ``` |
27 | 27 |
|
28 | 28 | ## Usage |
29 | 29 |
|
| 30 | +### Input |
| 31 | + |
| 32 | +The plugin will only attempt to hash files with `[hash]` in the name. |
| 33 | + |
| 34 | +```html |
| 35 | +<html> |
| 36 | + <head> |
| 37 | + <!-- not hashed --> |
| 38 | + <link rel="stylesheet" href="reset.css" /> |
| 39 | + |
| 40 | + <!-- hashed --> |
| 41 | + <link rel="stylesheet" href="style.[hash].css" /> |
| 42 | + </head> |
| 43 | + <body> |
| 44 | + <!-- not hashed --> |
| 45 | + <script src="analytics.js"></script> |
| 46 | + |
| 47 | + <!-- hashed --> |
| 48 | + <script src="src.[hash].js"></script> |
| 49 | + </body> |
| 50 | +</html> |
| 51 | +``` |
| 52 | + |
| 53 | +### Node.js |
| 54 | + |
| 55 | +The recommended usage is to hash static assets in your post-build process using Node.js. |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +A post-build script could look like: |
| 60 | + |
30 | 61 | ```js |
| 62 | +// postbuild.js |
31 | 63 | const fs = require("fs"); |
32 | 64 | const posthtml = require("posthtml"); |
33 | 65 | const { hash } = require("posthtml-hash"); |
34 | 66 |
|
35 | | -const html = fs.readFileSync("./index.html"); |
| 67 | +const html = fs.readFileSync("./build/index.html"); |
36 | 68 |
|
37 | 69 | posthtml() |
38 | | - .use(hash()) |
| 70 | + .use(hash({ path: "build" })) |
39 | 71 | .process(html) |
40 | | - .then((result) => fs.writeFileSync("./index.html", result.html)); |
| 72 | + .then((result) => fs.writeFileSync("./build/index.html", result.html)); |
41 | 73 | ``` |
42 | 74 |
|
43 | | -## Options |
| 75 | +For convenience, you can add the post-build script to your package.json. The `postbuild` script is automatically invoked following the `build` script. |
| 76 | + |
| 77 | +```json |
| 78 | +{ |
| 79 | + "scripts": { |
| 80 | + "build": "rollup -c", |
| 81 | + "postbuild": "node postbuild.js" |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Options |
44 | 87 |
|
45 | 88 | 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`: |
46 | 89 |
|
47 | 90 | ```js |
48 | | -const fs = require("fs"); |
49 | | -const posthtml = require("posthtml"); |
50 | | -const { hash } = require("posthtml-hash"); |
| 91 | +hash({ |
| 92 | + /** |
| 93 | + * Relative path to processed HTML file |
| 94 | + */ |
| 95 | + path: "public", // default: "" |
| 96 | +}); |
| 97 | +``` |
51 | 98 |
|
52 | | -const html = fs.readFileSync("./public/index.html"); |
| 99 | +### Custom Hash Length |
53 | 100 |
|
54 | | -posthtml() |
55 | | - .use( |
56 | | - hash({ |
57 | | - /** |
58 | | - * Relative path to processed HTML file |
59 | | - */ |
60 | | - path: "public", // default: "" |
61 | | - |
62 | | - /** |
63 | | - * Length of hash |
64 | | - */ |
65 | | - hashLength: 10, // default: 20 |
66 | | - |
67 | | - /** |
68 | | - * Hash CSS files |
69 | | - */ |
70 | | - css: true, // default: true |
71 | | - |
72 | | - /** |
73 | | - * Hash JS files |
74 | | - */ |
75 | | - js: true, // default: true |
76 | | - }) |
77 | | - ) |
78 | | - .process(html) |
79 | | - .then((result) => fs.writeFileSync("./index.html", result.html)); |
| 101 | +Customize the hash length by specifying an integer after the `hash:{NUMBER}`. The default hash length is `20`. |
| 102 | + |
| 103 | +```html |
| 104 | +<script src="src.[hash].js"></script> |
| 105 | +<!-- src.b0dcc67ffc1fd562f212.js --> |
| 106 | + |
| 107 | +<script src="src.[hash:8].js"></script> |
| 108 | +<!-- src.b0dcc67f.js --> |
80 | 109 | ``` |
81 | 110 |
|
82 | 111 | ## [Examples](examples) |
|
0 commit comments