Created
January 27, 2016 20:29
-
-
Save arronmabrey/21019f5609ce9fef1cd1 to your computer and use it in GitHub Desktop.
livereload-hotjsloader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function() { | |
window.LiveReloadPluginHotJSLoader = (function() { | |
LiveReloadPluginHotJSLoader.identifier = 'livereload-hotjsloader'; | |
LiveReloadPluginHotJSLoader.version = '1.0'; | |
function LiveReloadPluginHotJSLoader(window, host) { | |
this.window = window; | |
this.host = host; | |
this.debug = true; | |
this.pathMatcher = /\.js/; | |
} | |
LiveReloadPluginHotJSLoader.prototype.reload = function(path) { | |
if (path.match(this.pathMatcher)) { | |
var scripts = document.getElementsByTagName('script'); | |
for (var i = 0; i < scripts.length; i++) { | |
var oldScript = scripts[i]; | |
if (oldScript.src.match(new RegExp(path))) { | |
var newScript = document.createElement('script'); | |
// Set the location of the script | |
newScript.src = this.host.generateCacheBustUrl(oldScript.src); | |
if (this.debug) { | |
console.log(LiveReloadPluginHotJSLoader.identifier + ": " + newScript.src); | |
} | |
// Inject with insertBefore to avoid appendChild errors | |
oldScript.parentNode.insertBefore(newScript, oldScript); | |
// Remove old script | |
oldScript.remove(); | |
return true; | |
} | |
} | |
} | |
}; | |
return LiveReloadPluginHotJSLoader; | |
})(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment