Skip to content

Instantly share code, notes, and snippets.

@arronmabrey
Created January 27, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arronmabrey/21019f5609ce9fef1cd1 to your computer and use it in GitHub Desktop.
Save arronmabrey/21019f5609ce9fef1cd1 to your computer and use it in GitHub Desktop.
livereload-hotjsloader.js
"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