Skip to content
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
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ class WebpackPluginServe extends EventEmitter {
return result;
}

// #138. handle emitted events that don't have a listener registered so they can be sent via WebSocket
emit(eventName, ...args) {
const listeners = this.eventNames();

if (listeners.includes(eventName)) {
super.emit(eventName, ...args);
} else {
const [data] = args;
super.emit('unhandled', { eventName, data });
}
}

hook(compiler) {
const { done, invalid, watchClose, watchRun } = compiler.hooks;

Expand Down
6 changes: 5 additions & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const setupRoutes = function setupRoutes() {

socket.invalid = (filePath = '<unknown>', compiler) => {
const context = compiler.context || compiler.options.context || process.cwd();
const fileName = filePath.replace && filePath.replace(context, '') || filePath;
const fileName = (filePath.replace && filePath.replace(context, '')) || filePath;
const { wpsId } = compiler;

send(prep({ action: 'invalid', data: { fileName, wpsId } }));
Expand All @@ -97,6 +97,10 @@ const setupRoutes = function setupRoutes() {
});
}

// #138. handle emitted events that don't have a listener registered, and forward the message
// onto the client via the socket
this.on('unhandled', ({ eventName, data }) => send(prep({ action: eventName, data })));

send(prep({ action: 'connected' }));
}
};
Expand Down
4 changes: 3 additions & 1 deletion recipes/watch-static-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ serve.on('listening', () => {
});
```

The important change there is `serve.emit('reload')`. We're including some sugar data there so that any listeners know where the event originated. That could be useful in an app that has multiple instruction points calling for reloads.
The important change there is `serve.emit('reload')`. The `reload` event isn't actually handled by the `WebpackPluginServe` instance, but the plugin _does_ forward on unhandled, emitted events to all connected `WebSocket`s. The `reload` event is already handled in the client scripts as part of the `liveReload` option, so we can utilize that `WebSocket` message to reload the page. Alternatively, you might choose to connect your own `WebSocket`, use a different action (such as `static-change`), and add more magic than a plain old `window.location.reload()`.

We're also including some sugar data there in `{source: 'config' }` so that any listeners know where the event originated. That could be useful in an app that has multiple instruction points calling for reloads.

### 🍰 Dessert

Expand Down