Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.