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

feat: add build-started and build-finished events #122

Merged
merged 2 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});