webpack-plugin-serve
Version:
A Development Server in a Webpack Plugin
47 lines (35 loc) • 1.2 kB
JavaScript
/*
Copyright © 2018 Andrew Powell
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of this Source Code Form.
*/
const addHtml = (html, parent) => {
const div = document.createElement('div');
const nodes = [];
div.innerHTML = html.trim();
while (div.firstChild) {
nodes.push((parent || document.body).appendChild(div.firstChild));
}
return nodes;
};
const addCss = (css) => {
const style = document.createElement('style');
style.type = 'text/css';
if (css.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
// append the stylesheet for the svg
document.head.appendChild(style);
};
const socketMessage = (socket, handler) => {
socket.addEventListener('message', (message) => {
const { action, data = {} } = JSON.parse(message.data);
handler(action, data);
});
};
module.exports = { addCss, addHtml, socketMessage };