From 5eb1bdb152d13d38260f5e3e3f7d40e50a22a074 Mon Sep 17 00:00:00 2001 From: JTA Date: Thu, 23 Mar 2017 11:59:41 +0100 Subject: [PATCH] Bypass missing files when using MD5 --- lib/cachebust.js | 7 ++++++- test/fixtures/default_options.html | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cachebust.js b/lib/cachebust.js index b818ec7..a2d1deb 100644 --- a/lib/cachebust.js +++ b/lib/cachebust.js @@ -32,8 +32,13 @@ exports.busted = function(fileContents, options) { self.MD5 = function(fileContents, originalAttrValue, options) { var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0], - hash = MD5(fs.readFileSync(options.basePath + originalAttrValueWithoutCacheBusting).toString()); + filePath = options.basePath + originalAttrValueWithoutCacheBusting; + if (!fs.existsSync(filePath)) { + return fileContents; + } + + var hash = MD5(fs.readFileSync(filePath).toString()); return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?v=' + hash); }; diff --git a/test/fixtures/default_options.html b/test/fixtures/default_options.html index f845fae..a95357b 100644 --- a/test/fixtures/default_options.html +++ b/test/fixtures/default_options.html @@ -17,5 +17,6 @@