Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
feat: add build-started and build-finished events (#122)
Browse files Browse the repository at this point in the history
* feat: add build events

* docs: update readme with new events
  • Loading branch information
shellscape authored May 9, 2018
1 parent 715c50e commit bc6c9d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions test/tests/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit bc6c9d4

Please sign in to comment.