Code Monkey home page Code Monkey logo

asset-hash's Introduction

repo npm

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 as css/main.css or ./css/main.css
    • NOT relative from parents .., such as ../css/main.css
  • rewrites in place, so make a copy before calling if that matters

Usage

You can use hash-assets as a command line tool, or as a JavaScript library.

CLI

npm install -g @ryanburnette/asset-hash
hash-assets public/

With npx:

npm install --save @ryanburnette/asset-hash
npx hash-assets public/

JS

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);
  });

Example

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.

Warning

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.

asset-hash's People

Contributors

ryanburnette avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.