webpack-plugin-serve
Version:
A Development Server in a Webpack Plugin
108 lines (90 loc) • 3.45 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.
*/
/* eslint-disable global-require */
const run = (buildHash, options) => {
const { address, client = {}, hmr, progress, secure, status } = options;
options.firstInstance = !window.webpackPluginServe; // eslint-disable-line no-param-reassign
window.webpackPluginServe = window.webpackPluginServe || {
compilers: {}
};
window.webpackPluginServe.silent = !!client.silent;
const { ClientSocket } = require('./ClientSocket');
const { replace } = require('./hmr');
const { error, info, warn } = require('./log')();
const protocol = secure ? 'wss' : 'ws';
const socket = new ClientSocket(client, `${client.protocol || protocol}://${client.address || address}/wps`);
const { compilerName } = options;
window.webpackPluginServe.compilers[compilerName] = {};
// prevents ECONNRESET errors on the server
window.addEventListener('beforeunload', () => socket.close());
socket.addEventListener('message', (message) => {
const { action, data = {} } = JSON.parse(message.data);
const { errors, hash = '<?>', warnings } = data || {};
const shortHash = hash.slice(0, 7);
const identifier = options.compilerName ? `(Compiler: ${options.compilerName}) ` : '';
const compiler = window.webpackPluginServe.compilers[compilerName];
const { wpsId } = data;
switch (action) {
case 'build':
compiler.done = false;
break;
case 'connected':
info(`WebSocket connected ${identifier}`);
break;
case 'done':
compiler.done = true;
break;
case 'problems':
if (data.errors.length) {
error(`${identifier}Build ${shortHash} produced errors:\n`, errors);
}
if (data.warnings.length) {
warn(`${identifier}Build ${shortHash} produced warnings:\n`, warnings);
}
break;
case 'reload':
window.location.reload();
break;
case 'replace':
// actions with a wpsId in tow indicate actions that should only be executed when the wpsId sent
// matches the wpsId set in options. this is how we can identify multiple compilers in the
// client.
if (wpsId && wpsId === options.wpsId) {
replace(buildHash, hash, hmr === 'refresh-on-failure');
}
break;
default:
}
});
if (options.firstInstance) {
if (progress === 'minimal') {
const { init } = require('./overlays/progress-minimal');
init(options, socket);
} else if (progress) {
const { init } = require('./overlays/progress');
init(options, socket);
}
if (status) {
const { init } = require('./overlays/status');
init(options, socket);
}
if (module.hot) {
info('Hot Module Replacement is active');
if (options.liveReload) {
info('Live Reload taking precedence over Hot Module Replacement');
}
} else {
warn('Hot Module Replacement is inactive');
}
if (!module.hot && options.liveReload) {
info('Live Reload is active');
}
}
};
module.exports = { run };