22
33[ ![ NPM] [ npm ]] [ npm-url ]
44
5- ` posthtml-hash ` is a [ PostHTML] ( https://github.com/posthtml/posthtml ) plugin for hashing static CSS/JS assets to enable caching.
5+ ` posthtml-hash ` is a [ PostHTML] ( https://github.com/posthtml/posthtml ) plugin for hashing static assets to enable caching.
66
77``` diff
88<html>
99 <head>
10- - <link rel="stylesheet" href="stylesheet .[hash].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" />
1212 </head>
1313 <body>
14- - <script src="main .[hash].js"></script>
15- + <script src="main .b0dcc67ffc1fd562f212.js"></script>
14+ - <script src="src .[hash].js"></script>
15+ + <script src="src .b0dcc67ffc1fd562f212.js"></script>
1616 </body>
1717</html>
1818```
@@ -27,6 +27,29 @@ npm i -D posthtml-hash
2727
2828## Usage
2929
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+
3053``` js
3154const fs = require (" fs" );
3255const posthtml = require (" posthtml" );
@@ -40,43 +63,29 @@ posthtml()
4063 .then ((result ) => fs .writeFileSync (" ./index.html" , result .html ));
4164```
4265
43- ## Options
66+ ### Options
4467
4568This 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 ` :
4669
4770``` js
48- const fs = require (" fs" );
49- const posthtml = require (" posthtml" );
50- const { hash } = require (" posthtml-hash" );
71+ hash ({
72+ /**
73+ * Relative path to processed HTML file
74+ */
75+ path: " public" , // default: ""
76+ });
77+ ```
5178
52- const html = fs . readFileSync ( " ./public/index.html " );
79+ ### Custom Hash Length
5380
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 ));
81+ Customize the hash length by specifying an integer after the ` hash:{NUMBER} ` . The default hash length is ` 20 ` .
82+
83+ ``` html
84+ <script src =" src.[hash].js" ></script >
85+ <!-- src.b0dcc67ffc1fd562f212.js -->
86+
87+ <script src =" src.[hash:8].js" ></script >
88+ <!-- src.b0dcc67f.js -->
8089```
8190
8291## [ Examples] ( examples )
0 commit comments