fuse-box
Version:
Fuse-Box a bundler that does it right
53 lines (52 loc) • 1.67 kB
JavaScript
;
/**
* @module listens to `source-changed` socket events and actions hot reload
*/
Object.defineProperty(exports, "__esModule", { value: true });
var Client = require('fusebox-websocket').SocketClient;
exports.connect = function (port, uri) {
if (FuseBox.isServer) {
return;
}
port = port || window.location.port;
var client = new Client({
port: port,
uri: uri,
});
client.connect();
client.on('source-changed', function (data) {
console.info("%cupdate \"" + data.path + "\"", 'color: #237abe');
/**
* If a plugin handles this request then we don't have to do anything
**/
for (var index = 0; index < FuseBox.plugins.length; index++) {
var plugin = FuseBox.plugins[index];
if (plugin.hmrUpdate && plugin.hmrUpdate(data)) {
return;
}
}
if (data.type === 'js') {
FuseBox.flush();
FuseBox.dynamic(data.path, data.content);
if (FuseBox.mainFile) {
try {
FuseBox.import(FuseBox.mainFile);
}
catch (e) {
if (typeof e === 'string') {
if (/not found/.test(e)) {
return window.location.reload();
}
}
console.error(e);
}
}
}
if (data.type === 'css' && __fsbx_css) {
__fsbx_css(data.path, data.content);
}
});
client.on('error', function (error) {
console.log(error);
});
};