Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
feat: granular control of client warnings, errors. fixes #55 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored May 4, 2018
1 parent f13237f commit e830535
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 39 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const stringify = require('json-stringify-safe');
const weblog = require('webpack-log');
const WebSocket = require('ws');
const HotClientError = require('./lib/HotClientError');
const { modifyCompiler, payload, sendStats, validateCompiler } = require('./lib/util');
const { modifyCompiler, payload, sendData, validateCompiler } = require('./lib/util');

const defaults = {
host: 'localhost',
Expand All @@ -14,6 +14,10 @@ const defaults = {
logTime: false,
port: 8081,
reload: true,
send: {
errors: true,
warnings: true
},
server: null,
stats: {
context: process.cwd()
Expand Down Expand Up @@ -110,15 +114,14 @@ module.exports = (compiler, opts) => {
options.log.error('compiler done: `stats` is undefined');
}

sendStats(broadcast, jsonStats);
sendData(broadcast, jsonStats, options);
};

const invalid = () => {
log.info('webpack: Bundle Invalidated');
broadcast(payload('invalid'));
};


// as of webpack@4 MultiCompiler no longer exports the compile hook
const compilers = compiler.compilers || [compiler];
for (const comp of compilers) {
Expand Down Expand Up @@ -168,7 +171,7 @@ module.exports = (compiler, opts) => {
options.log.error('Client Connection: `stats` is undefined');
}

sendStats(broadcast, jsonStats);
sendData(broadcast, jsonStats, options);
}
});

Expand Down
10 changes: 7 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ module.exports = {
return stringify({ type, data });
},

sendStats(broadcast, stats) {
sendData(broadcast, stats, options) {
const send = (type, data) => {
broadcast(module.exports.payload(type, data));
};

if (stats.errors && stats.errors.length > 0) {
send('errors', stats.errors);
if (options.send.errors) {
send('errors', stats.errors);
}
return;
}

Expand All @@ -133,7 +135,9 @@ module.exports = {
send('hash', stats.hash);

if (stats.warnings.length > 0) {
send('warnings', stats.warnings);
if (options.send.warnings) {
send('warnings', stats.warnings);
}
} else {
send('ok');
}
Expand Down
28 changes: 15 additions & 13 deletions test/tests/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const assert = require('assert');
const webpack = require('webpack');
const client = require('../../index');

const logLevel = 'silent';

describe('Webpack Hot Client', () => {
beforeEach(() => {
process.env.WHC_TARGET = '';
Expand All @@ -20,23 +22,23 @@ describe('Webpack Hot Client', () => {
it('should reject string entry', () => {
const config = require('../fixtures/webpack.config-invalid.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };

assert.throws(() => { client(compiler, options); });
});

it('should reject object with string entry', () => {
const config = require('../fixtures/webpack.config-invalid-object.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };

assert.throws(() => { client(compiler, options); });
});

it('should allow string array entry', (done) => {
const config = require('../fixtures/webpack.config-array.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => { close(done); }, 2000);
Expand All @@ -45,7 +47,7 @@ describe('Webpack Hot Client', () => {
it('should allow object with string array entry', (done) => {
const config = require('../fixtures/webpack.config-object.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => { close(done); }, 2000);
Expand All @@ -54,7 +56,7 @@ describe('Webpack Hot Client', () => {
it('should set WHC_TARGET to web', (done) => {
const config = require('../fixtures/webpack.config-array.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => {
Expand All @@ -70,7 +72,7 @@ describe('Webpack Hot Client', () => {
config.target = 'electron-renderer';

const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => {
Expand All @@ -82,7 +84,7 @@ describe('Webpack Hot Client', () => {
it('should allow setting host', (done) => {
const config = require('../fixtures/webpack.config-array.js');
const compiler = webpack(config);
const options = { host: '0.0.0.0', hot: true, logLevel: 'info' };
const options = { host: '0.0.0.0', hot: true, logLevel };
const { close, wss } = client(compiler, options);

setTimeout(() => {
Expand All @@ -97,7 +99,7 @@ describe('Webpack Hot Client', () => {
const options = {
host: { client: '127.0.0.1', server: '127.0.0.1' },
hot: true,
logLevel: 'info'
logLevel
};
const { close, options: opts, wss } = client(compiler, options);

Expand All @@ -117,7 +119,7 @@ describe('Webpack Hot Client', () => {
const options = {
host: { client: '127.0.0.1', server: '0.0.0.0' },
hot: true,
logLevel: 'info'
logLevel
};
const { close, options: opts, wss } = client(compiler, options);

Expand All @@ -134,7 +136,7 @@ describe('Webpack Hot Client', () => {
it('should allow function entry that returns array', (done) => {
const config = require('../fixtures/webpack.config-function.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => {
Expand All @@ -145,7 +147,7 @@ describe('Webpack Hot Client', () => {
it('should reject function entry that returns string', (done) => {
const config = require('../fixtures/webpack.config-function-invalid.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => {
Expand Down Expand Up @@ -176,7 +178,7 @@ describe('Webpack Hot Client', () => {
server.listen(1337, '127.0.0.1', () => {
const config = require('../fixtures/webpack.config.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info', server };
const options = { hot: true, logLevel, server };
const { close } = client(compiler, options);

setTimeout(() => {
Expand All @@ -188,7 +190,7 @@ describe('Webpack Hot Client', () => {
it('should function with MultiCompiler config', (done) => {
const config = require('../fixtures/multi/webpack.config.js');
const compiler = webpack(config);
const options = { hot: true, logLevel: 'info' };
const options = { hot: true, logLevel };
const { close } = client(compiler, options);

setTimeout(() => { close(done); }, 2000);
Expand Down
124 changes: 105 additions & 19 deletions test/tests/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@ const webpackPackage = require('webpack/package.json');
const hotClient = require('../../index');
const config = require('../fixtures/webpack.config.js');

describe('Sockets', function d() {
const entryPath = path.join(__dirname, '../fixtures/app.js');
const cleanPath = path.join(__dirname, '../fixtures/app-clean.js');
const clean = fs.readFileSync(cleanPath, 'utf-8');
const webpackVersion = parseInt(webpackPackage.version, 10);

if (webpackVersion > 3) {
config.mode = 'development';
const entryPath = path.join(__dirname, '../fixtures/app.js');
const cleanPath = path.join(__dirname, '../fixtures/app-clean.js');
const clean = fs.readFileSync(cleanPath, 'utf-8');
const webpackVersion = parseInt(webpackPackage.version, 10);
const logLevel = 'silent';

if (webpackVersion > 3) {
config.mode = 'development';
}

function parse(...args) {
try {
return JSON.parse(...args);
} catch (e) {
console.log(e);
}
}

this.timeout(30000);

describe('Sockets', () => {
let compiler;
let client;
let watchers;

function parse(...args) {
try {
return JSON.parse(...args);
} catch (e) {
console.log(e);
}
}

before((done) => {
compiler = webpack(config);
client = hotClient(compiler, { hot: true, logLevel: 'silent' });
client = hotClient(compiler, { hot: true, logLevel });

const isMemoryFs = !compiler.compilers && compiler.outputFileSystem instanceof MemoryFileSystem;

Expand Down Expand Up @@ -159,3 +158,90 @@ describe('Sockets', function d() {
fs.writeFileSync(entryPath, clean + errorCode, 'utf-8');
}).timeout(10000);
});

describe('Sockets: send option', () => {
let compiler;
let client;
let watchers;

before((done) => {
compiler = webpack(config);
client = hotClient(compiler, {
hot: true,
logLevel,
send: { errors: false, warnings: false }
});

const isMemoryFs = !compiler.compilers && compiler.outputFileSystem instanceof MemoryFileSystem;

if (!isMemoryFs) {
compiler.outputFileSystem = new MemoryFileSystem();
}

watchers = compiler.watch({}, (err) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
}
});

setTimeout(done, 1000);
});

afterEach(() => {
fs.writeFileSync(entryPath, clean, 'utf-8');
});

after(function after(done) {
this.timeout(5000);
setTimeout(() => {
watchers.close(() => {
client.close(done);
});
}, 4000);
});

it('sockets should not receive warnings', (done) => {
const warningCode = '\nconsole.log(require)';
const socket = new WebSocket('ws://localhost:8081');
let received = false;

socket.on('message', (data) => {
const message = parse(data);

if (message.type === 'warnings') {
received = true;
}
});

setTimeout(() => {
assert(!received);
done();
}, 1000);

fs.writeFileSync(entryPath, clean + warningCode, 'utf-8');
}).timeout(10000);

it('sockets should not receive errors', (done) => {
const errorCode = '\nif(!window) { require("test"); }';
const socket = new WebSocket('ws://localhost:8081');
let received = false;

socket.on('message', (data) => {
const message = parse(data);

if (message.type === 'errors') {
received = true;
}
});

setTimeout(() => {
assert(!received);
done();
}, 1000);

fs.writeFileSync(entryPath, clean + errorCode, 'utf-8');
}).timeout(10000);
});

0 comments on commit e830535

Please sign in to comment.