diff --git a/README.md b/README.md index 815baf2..f1452a9 100644 --- a/README.md +++ b/README.md @@ -338,10 +338,24 @@ serve({ config }).then((server) => { }); ``` +#### build-started + +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_ + +Emitted when a compiler has finished a build. + #### listening Arguments: _None_ +Emitted when the server begins listening for connections. + ## Add-on Features A core tenant of `webpack-serve` is to stay lean in terms of feature set, and to @@ -435,4 +449,4 @@ We welcome your contributions! Please have a read of [dev-ware]: https://github.com/webpack/webpack-dev-middleware#options [hot-client]: https://github.com/webpack-contrib/webpack-hot-client#options -[https-opts]: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options +[https-opts]: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options \ No newline at end of file diff --git a/index.js b/index.js index 452c640..6da834c 100644 --- a/index.js +++ b/index.js @@ -52,14 +52,19 @@ module.exports = (opts) => { if (stats.hasWarnings()) { bus.emit('compiler-warning', json); } + + bus.emit('build-finished', stats); }; - if (options.compiler.hooks) { - options.compiler.hooks.done.tap('WebpackServe', done); - } else { - options.compiler.plugin('done', done); + const compilers = options.compiler.compilers || [options.compiler]; + for (const comp of compilers) { + comp.hooks.compile.tap('WebpackServe', () => { + bus.emit('build-started', comp); + }); } + options.compiler.hooks.done.tap('WebpackServe', done); + const { close, server, start } = getServer(options); start(options); diff --git a/test/tests/events.js b/test/tests/events.js index f908d2b..b7ca078 100644 --- a/test/tests/events.js +++ b/test/tests/events.js @@ -41,4 +41,24 @@ describe('webpack-serve Events', () => { }); }); }).timeout(5e3); + + 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); + setTimeout(() => { server.close(done); }, timeout); + }); + }); + }).timeout(5e3); + + 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); + setTimeout(() => { server.close(done); }, timeout); + }); + }); + }).timeout(5e3); });