From 6aaac1906ef7809686765904b4d331b75bdb4e13 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 12 May 2018 10:24:54 -0400 Subject: [PATCH 1/2] feat: improve event parameters --- index.js | 30 ++++++++++++++---------------- lib/server.js | 2 +- test/tests/events.js | 24 ++++++++++++++---------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 5a0aead..7e0638a 100644 --- a/index.js +++ b/index.js @@ -31,27 +31,25 @@ module.exports = (opts) => { } } - const done = (stats) => { - const json = stats.toJson(); - if (stats.hasErrors()) { - bus.emit('compiler-error', json); - } - - if (stats.hasWarnings()) { - bus.emit('compiler-warning', json); - } - - bus.emit('build-finished', stats); - }; - const compilers = options.compiler.compilers || [options.compiler]; for (const comp of compilers) { comp.hooks.compile.tap('WebpackServe', () => { - bus.emit('build-started', comp); + bus.emit('build-started', { compiler: comp }); }); - } - options.compiler.hooks.done.tap('WebpackServe', done); + comp.hooks.done.tap('WebpackServe', (stats) => { + const json = stats.toJson(); + if (stats.hasErrors()) { + bus.emit('compiler-error', { json, compiler: comp }); + } + + if (stats.hasWarnings()) { + bus.emit('compiler-warning', { json, compiler: comp }); + } + + bus.emit('build-finished', { stats, compiler: comp }); + }); + } const { close, server, start } = getServer(options); diff --git a/lib/server.js b/lib/server.js index 32e7491..071c4a1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -105,7 +105,7 @@ module.exports = (options) => { } } - bus.emit('listening', server); + bus.emit('listening', { server, options }); if (options.open) { const open = require('opn'); // eslint-disable-line global-require diff --git a/test/tests/events.js b/test/tests/events.js index 4d53151..8c9e962 100644 --- a/test/tests/events.js +++ b/test/tests/events.js @@ -12,8 +12,9 @@ describe('webpack-serve Events', () => { it('should emit the listening event', (done) => { const config = load('./fixtures/basic/webpack.config.js'); serve({ config }).then((server) => { - server.on('listening', () => { - assert(true); + server.on('listening', ({ server: servr, options }) => { + assert(servr); + assert(options); // occasionally close() will be called before the WebSocket server is up setTimeout(() => { server.close(done); @@ -25,8 +26,9 @@ describe('webpack-serve Events', () => { it('should emit the compiler-error event', (done) => { const config = load('./fixtures/error/webpack.config.js'); serve({ config }).then((server) => { - server.on('compiler-error', () => { - assert(true); + server.on('compiler-error', ({ json, compiler }) => { + assert(json); + assert(compiler); setTimeout(() => { server.close(done); }, timeout); @@ -37,8 +39,9 @@ describe('webpack-serve Events', () => { it('should emit the compiler-warning event', (done) => { const config = load('./fixtures/warning/webpack.config.js'); serve({ config }).then((server) => { - server.on('compiler-warning', () => { - assert(true); + server.on('compiler-warning', ({ json, compiler }) => { + assert(json); + assert(compiler); setTimeout(() => { server.close(done); }, timeout); @@ -49,8 +52,8 @@ describe('webpack-serve Events', () => { it('should emit the build-started event', (done) => { const config = load('./fixtures/basic/webpack.config.js'); serve({ config }).then((server) => { - server.on('build-started', () => { - assert(true); + server.on('build-started', ({ compiler }) => { + assert(compiler); setTimeout(() => { server.close(done); }, timeout); @@ -61,8 +64,9 @@ describe('webpack-serve Events', () => { it('should emit the build-finished event', (done) => { const config = load('./fixtures/basic/webpack.config.js'); serve({ config }).then((server) => { - server.on('build-finished', () => { - assert(true); + server.on('build-finished', ({ stats, compiler }) => { + assert(stats); + assert(compiler); setTimeout(() => { server.close(done); }, timeout); From 24c3730429d71f78bcd820b64a1935bed6059bdf Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 12 May 2018 10:32:04 -0400 Subject: [PATCH 2/2] docs: add, update event documentation --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 387b69d..e9d1709 100644 --- a/README.md +++ b/README.md @@ -340,19 +340,40 @@ serve({ config }).then((server) => { #### build-started -Arguments: [`Compiler`](https://webpack.js.org/api/node/#compiler-instance) _compiler_ +Arguments: + [`Compiler`](https://webpack.js.org/api/node/#compiler-instance) _compiler_ Emitted when a compiler has started a build. #### build-finished -Arguments: [`Stats`](https://webpack.js.org/api/node/#stats-object) _stats_ +Arguments: + [`Stats`](https://webpack.js.org/api/node/#stats-object) _stats_ + [`Compiler`](https://webpack.js.org/api/node/#compiler-instance) _compiler_ Emitted when a compiler has finished a build. +#### compile-error + +Arguments: + [`Stats`](https://webpack.js.org/api/node/#stats-tojson-options-) _json_ + [`Compiler`](https://webpack.js.org/api/node/#compiler-instance) _compiler_ + +Emitted when a compiler has encountered and error, or a build has errors. + +#### compile-warning + +Arguments: + [`Stats`](https://webpack.js.org/api/node/#stats-tojson-options-) _json_ + [`Compiler`](https://webpack.js.org/api/node/#compiler-instance) _compiler_ + +Emitted when a compiler has encountered a warning, or a build has warnings. + #### listening -Arguments: _None_ +Arguments: + `Koa` _server_ + `Object` _options_ Emitted when the server begins listening for connections.