Skip to content

Commit 2ccdefc

Browse files
committed
docs: update readme
1 parent 2591cfb commit 2ccdefc

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,37 @@ The plugin will only attempt to hash files with `[hash]` in the name.
5050
</html>
5151
```
5252

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+
5361
```js
62+
// postbuild.js
5463
const fs = require("fs");
5564
const posthtml = require("posthtml");
5665
const { hash } = require("posthtml-hash");
5766

58-
const html = fs.readFileSync("./index.html");
67+
const html = fs.readFileSync("./build/index.html");
5968

6069
posthtml()
61-
.use(hash())
70+
.use(hash({ path: "build" }))
6271
.process(html)
63-
.then((result) => fs.writeFileSync("./index.html", result.html));
72+
.then((result) => fs.writeFileSync("./build/index.html", result.html));
73+
```
74+
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+
}
6484
```
6585

6686
### Options

0 commit comments

Comments
 (0)