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

feat: improve event parameters #136

Merged
merged 2 commits into from
May 12, 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
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
30 changes: 14 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions test/tests/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down