christophercliff / metalsmith-fingerprint

A fingerprint plugin for Metalsmith
MIT License
28 stars 7 forks source link

Optionally use the original file #16

Open doublejosh opened 6 years ago

doublejosh commented 6 years ago

It would be super great to optionally use the original URL when working on a local dev environment. That way the finger print helper {{ fingerprint.[js/my-script.js] }} would still work, but you could avoid rebuilding content when just editing JS files for ultra fast livereloads.

doublejosh commented 6 years ago

For now I create a helper plugin to remove the hashes based on an environment variable...

var _ = require('underscore');

module.exports = function (options) {
  return function metalsmithPlugin (files, metalsmith, done) {
    var metadata = metalsmith.metadata();
    // Avoid fingerprinting files when working locally without a CDN.
    if (!options.env.USE_CDN) {
      // Strip fingerprint hash off file list.
      _.mapObject(metadata.fingerprint, function (hashed, filename) {
        metadata.fingerprint[filename] = filename;
      });
      return process.nextTick(done);
    }
    // Do nothing.
    setImmediate(done);
  };
};
var fingerprintPattern = [
      'css/*.css',
      'js/*/*.js'
    ];

Metalsmith(__dirname)
  .use(fingerprint({pattern: fingerprintPattern}))
  .use(fingerprintFixer({
    pattern: fingerprintPattern
    env: process.env,
  }))
  .build()