From 4bf8403ce3c3e246c9e448b453b64bd6a41bd5f9 Mon Sep 17 00:00:00 2001 From: Rusty Weneck Date: Wed, 13 May 2015 11:18:58 -0400 Subject: [PATCH 1/3] if the contents is on a sub path, and the mapping is relative from there, like a CSS file including a font in a sub dir, we need to remove the subpath of the file being mapped, since it will not match the resource --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6a20df3..d2bd160 100644 --- a/index.js +++ b/index.js @@ -98,11 +98,26 @@ CacheBuster.prototype.references = function references() { var contents = file.contents.toString(encoding); + + var relativeRegex = null; + var lastIndexOfPathSeperator = file.relative.lastIndexOf("/"); + if(lastIndexOfPathSeperator > -1){ + var relativePath = file.relative.substring(0, file.relative.lastIndexOf("/")+1); + relativeRegex = new RegExp(relativePath, 'g'); + } + var mappings = cachebuster.getRelativeMappings(file.path); for (var i=0; i < mappings.length; i++) { var original = mappings[i].original; var cachebusted = mappings[i].cachebusted; + //if the contents is on a sub path, and the mapping is relative from there, + //we need to remove the subpath of the file being mapped, since it will not match the resource + if(relativeRegex){ + original = original.replace(relativeRegex, ""); + cachebusted = cachebusted.replace(relativeRegex, ""); + } + contents = contents.replace(new RegExp('\\b' + original + '(?!\\.)\\b', 'g'), cachebusted); } @@ -110,4 +125,4 @@ CacheBuster.prototype.references = function references() { this.push(file); return callback(); }); -}; +}; \ No newline at end of file From c45725f7c866f6fae0a777c0e28b7b9789b7dca2 Mon Sep 17 00:00:00 2001 From: Rusty Weneck Date: Wed, 28 Sep 2016 12:27:39 -0400 Subject: [PATCH 2/3] replacing string operators with the path module. --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d2bd160..2d31cce 100644 --- a/index.js +++ b/index.js @@ -100,10 +100,9 @@ CacheBuster.prototype.references = function references() { var relativeRegex = null; - var lastIndexOfPathSeperator = file.relative.lastIndexOf("/"); - if(lastIndexOfPathSeperator > -1){ - var relativePath = file.relative.substring(0, file.relative.lastIndexOf("/")+1); - relativeRegex = new RegExp(relativePath, 'g'); + var pathComponents = path.parse(file); + if(pathComponents && pathComponents.dir){ + relativeRegex = new RegExp(pathComponent.dir, 'g'); } var mappings = cachebuster.getRelativeMappings(file.path); From e9129e21f80c8ce81502f6d92098aee12452217e Mon Sep 17 00:00:00 2001 From: Rusty Weneck Date: Wed, 28 Sep 2016 12:39:22 -0400 Subject: [PATCH 3/3] use file.relative. --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2d31cce..1dc6ae4 100644 --- a/index.js +++ b/index.js @@ -100,9 +100,9 @@ CacheBuster.prototype.references = function references() { var relativeRegex = null; - var pathComponents = path.parse(file); - if(pathComponents && pathComponents.dir){ - relativeRegex = new RegExp(pathComponent.dir, 'g'); + var pathComponents = path.parse(file.relative); + if(pathComponents && pathComponents.dir && pathComponents.dir!==""){ + relativeRegex = new RegExp(pathComponents.dir, 'g'); } var mappings = cachebuster.getRelativeMappings(file.path);