An script for asset hash script for static websites.
- works in .css and .js files
- find the hash of a given file
- rename the file with the hash
- gz the file in place for web servers that support precompressed assets
- update all the references to the file
- references be made
- absolute from
/
, such as/css/main.css
- children relative from
.
, such ascss/main.css
or./css/main.css
- NOT relative from parents
..
, such as../css/main.css
- absolute from
- rewrites in place, so make a copy before calling if that matters
You can use hash-assets as a command line tool, or as a JavaScript library.
npm install -g @ryanburnette/asset-hash
hash-assets public/
With npx
:
npm install --save @ryanburnette/asset-hash
npx hash-assets public/
npm install --save @ryanburnette/asset-hash
"use strict";
var hashAssets = require("../index.js");
var siteroot = "/home/me/sites/example.com/public/";
console.info("\nWorking from '%s'", siteroot);
hashAssets(siteroot)
.then(function (changes) {
console.info("\nRenamed:");
Object.keys(changes.renamed).forEach(function (name) {
console.info("\t'%s' => '%s'", name, changes.renamed[name]);
});
console.info("\nRewrote urls in each of:");
changes.rewritten.forEach(function (name) {
console.info("\t'%s'", name);
});
})
.then(function () {
console.info("\nComplete.\n");
})
.catch(function (err) {
console.error("something bad happened", err);
});
rsync -a --delete ./test/html_/ ./test/html/
hash-assets test/html
Working from '/Users/me/github.com/ryanburnette/asset-hash/test/html'
Renamed:
'/js/main.js' => '/js/main.b58d988070d02e44d27a6b019e23a1665bd1f790.js'
'/foo/js/main.js' => '/foo/js/main.b58d988070d02e44d27a6b019e23a1665bd1f790.js'
'/foo/css/style.css' => '/foo/css/style.e0bc30e345c7b5fc33f2186bd2a8d387b088e1eb.css'
'/css/style.css' => '/css/style.e0bc30e345c7b5fc33f2186bd2a8d387b088e1eb.css'
Rewrote urls in each of:
'/absolute.html'
'/relative.html'
'/foo/absolute.html'
'/foo/relative.html'
Complete.
I reference all my assets absolutely from root /
, which is what is best tested.
This library should also handle child-relative references ./
, but not parent-relative references ../
.
This DOES NOT respect <base>
urls.