Skip to content
This repository was archived by the owner on Jul 13, 2021. It is now read-only.
Closed
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
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING-OLD.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module. Thankfully, Github provides a means to do this. Add a dependency to the
`package.json` for such a project as follows:

```json
"webpack-hot-client": "webpack-contrib/webpack-hot-client#{id}/head"
"webpack-hmr-client": "hedgepigdaniel/webpack-hmr-client#{id}/head"
```

Where `{id}` is the # ID of your Pull Request.
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/BUG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ about: Something went awry and you'd like to tell us about it.
* Node Version:
* NPM Version:
* webpack Version:
* webpack-hot-client Version:
* webpack-hmr-client Version:


### Expected Behavior
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/FEATURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ about: Suggest an idea for this project
* Node Version:
* NPM Version:
* webpack Version:
* webpack-hot-client Version:
* webpack-hmr-client Version:


### Feature Proposal
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/MODIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ about: Would you like something work differently? Have an alternative approach?
* Node Version:
* NPM Version:
* webpack Version:
* webpack-hot-client Version:
* webpack-hmr-client Version:


### Expected Behavior / Situation
Expand Down
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="1.1.1"></a>
## [1.1.1](https://github.com/hedgepigdaniel/webpack-hmr-client/compare/v1.1.0...v1.1.1) (2019-09-20)


### Bug Fixes

* allow host/port to be unset when the server option is used ([8f03950](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/8f03950))
* use the host.client option even when the server option is used ([2791d8d](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/2791d8d))
* **client:** allow multiple client connections ([27d392c](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/27d392c))
* **socket-server:** send stats to new connections ([77ebb02](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/77ebb02))



<a name="1.1.0"></a>
# 1.1.0 (2019-09-20)


### Bug Fixes

* __hotClientOptions__ when autoConfigure: false ([#65](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/65)) ([5cc12b7](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/5cc12b7))
* apply DefinePlugin beforeCompile, and only once ([#73](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/73)) ([b3ba36c](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/b3ba36c))
* duplicate logging in MultiCompilers. fixes [#47](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/47) ([#59](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/59)) ([411ecf9](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/411ecf9))
* fixes [#86](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/86), client port bungle ([#87](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/87)) ([7a95e20](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/7a95e20))
* https option for https.Server ([#81](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/81)) ([0ac0673](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/0ac0673))
* include schemas in package files ([e059dc0](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/e059dc0))
* incorrect check for existing socket clients ([#77](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/77)) ([4b347c8](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/4b347c8)), closes [#61](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/61)
* multiple entries, add autoConfigure option ([#58](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/58)) ([1e6c6b2](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/1e6c6b2))
* prevent validation errors if no options passed ([#75](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/75)) ([0e56e17](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/0e56e17))
* strip ansi from warnings, errors. fixes [#46](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/46) ([#57](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/57)) ([3f128db](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/3f128db))
* throw error when HMR plugin exists in config ([#66](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/66)) ([22c7e68](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/22c7e68))
* webpack-defaults to devDeps ([fbece68](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/fbece68))


### Features

* allEntries applies client entry to all entries ([#64](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/64)) ([ea7625e](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/ea7625e))
* granular control of client warnings, errors. fixes [#55](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/55) ([#56](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/56)) ([e830535](https://github.com/hedgepigdaniel/webpack-hmr-client/commit/e830535))
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[![chat][chat]][chat-url]
[![size][size]][size-url]

# webpack-hot-client
# webpack-hmr-client

A client for enabling, and interacting with, webpack [Hot Module Replacement][hmr-docs].

Expand All @@ -20,31 +20,31 @@ and allows for adding Hot Module Replacement to an existing server, without a
dependency upon [`webpack-dev-server`][dev-server]. This comes in handy for testing
in projects that already use server frameworks such as `Express` or `Koa`.

`webpack-hot-client` accomplishes this by creating a `WebSocket` server, providing
`webpack-hmr-client` accomplishes this by creating a `WebSocket` server, providing
the necessary client (browser) scripts that communicate via `WebSocket`s, and
automagically adding the necessary webpack plugins and config entries. All of
that allows for a seamless integration of Hot Module Replacement Support.

Curious about the differences between this module and `webpack-hot-middleware`?
[Read more here](https://github.com/webpack-contrib/webpack-hot-client/issues/18).
[Read more here](https://github.com/hedgepigdaniel/webpack-hmr-client/issues/18).

## Requirements

This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.

## Getting Started

To begin, you'll need to install `webpack-hot-client`:
To begin, you'll need to install `webpack-hmr-client`:

```console
$ npm install webpack-hot-client --save-dev
$ npm install webpack-hmr-client --save-dev
```

## Gotchas

### Entries

In order to use `webpack-hot-client`, your `webpack` config should include an
In order to use `webpack-hmr-client`, your `webpack` config should include an
`entry` option that is set to an `Array` of `String`, or an `Object` who's keys
are set to an `Array` of `String`. You may also use a `Function`, but that
function should return a value in one of the two valid formats.
Expand All @@ -55,7 +55,7 @@ route, you must specify an `entry` option.

### Automagical Configuration

As a convenience `webpack-hot-client` adds `HotModuleReplacementPlugin`
As a convenience `webpack-hmr-client` adds `HotModuleReplacementPlugin`
and the necessary entries to your `webpack` config for you at runtime. Including
the plugin in your config manually while using this module may produce unexpected
or wonky results. If you have a need to configure entries and plugins for HMR
Expand All @@ -75,14 +75,14 @@ Express example below.) This condition does not apply if providing a static
For setting up the module for use with an `Express` server, try the following:

```js
const hotClient = require('webpack-hot-client');
const hotClient = require('webpack-hmr-client');
const middleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const config = require('./webpack.config');

const compiler = webpack(config);
const { publicPath } = config.output;
const options = { ... }; // webpack-hot-client options
const options = { ... }; // webpack-hmr-client options

// we recommend calling the client _before_ adding the dev middleware
const client = hotClient(compiler, options);
Expand Down Expand Up @@ -257,7 +257,7 @@ typically need to be modified.
Type: `Array[String]`
Default: `['web']`

By default, `webpack-hot-client` is meant to, and expects to function on the
By default, `webpack-hmr-client` is meant to, and expects to function on the
[`'web'` build target](https://webpack.js.org/configuration/target). However,
you can manipulate this by adding targets to this property. eg.

Expand All @@ -283,26 +283,26 @@ We welcome your contributions! Please have a read of

#### [MIT](./LICENSE)

[npm]: https://img.shields.io/npm/v/webpack-hot-client.svg
[npm-url]: https://npmjs.com/package/webpack-hot-client
[npm]: https://img.shields.io/npm/v/webpack-hmr-client.svg
[npm-url]: https://npmjs.com/package/webpack-hmr-client

[node]: https://img.shields.io/node/v/webpack-hot-client.svg
[node]: https://img.shields.io/node/v/webpack-hmr-client.svg
[node-url]: https://nodejs.org

[deps]: https://david-dm.org/webpack-contrib/webpack-hot-client.svg
[deps-url]: https://david-dm.org/webpack-contrib/webpack-hot-client
[deps]: https://david-dm.org/hedgepigdaniel/webpack-hmr-client.svg
[deps-url]: https://david-dm.org/hedgepigdaniel/webpack-hmr-client

[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/webpack-hot-client.svg
[tests-url]: https://circleci.com/gh/webpack-contrib/webpack-hot-client
[tests]: https://img.shields.io/circleci/project/github/hedgepigdaniel/webpack-hmr-client.svg
[tests-url]: https://circleci.com/gh/hedgepigdaniel/webpack-hmr-client

[cover]: https://codecov.io/gh/webpack-contrib/webpack-hot-client/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack-contrib/webpack-hot-client
[cover]: https://codecov.io/gh/hedgepigdaniel/webpack-hmr-client/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/hedgepigdaniel/webpack-hmr-client

[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
[chat-url]: https://gitter.im/webpack/webpack

[size]: https://packagephobia.now.sh/badge?p=webpack-hot-client
[size-url]: https://packagephobia.now.sh/result?p=webpack-hot-client
[size]: https://packagephobia.now.sh/badge?p=webpack-hmr-client
[size-url]: https://packagephobia.now.sh/result?p=webpack-hmr-client

[dev-middleware]: https://github.com/webpack/webpack-dev-middleware
[dev-server]: https://github.com/webpack/webpack-dev-server
Expand Down
2 changes: 1 addition & 1 deletion docs/REMOTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

If you're working in an environment where you have the need to run the server on
one machine (or VM) and need to test your app on another, you'll need to properly
configure both `webpack-hot-client` and the remote machine. The most stable and
configure both `webpack-hmr-client` and the remote machine. The most stable and
least error-prone method will involve setting options statically:

## Client Host and HOSTS
Expand Down
2 changes: 1 addition & 1 deletion lib/HotClientError.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class HotClientError extends Error {
constructor(message) {
super(`webpack-hot-client: ${message}`);
super(`webpack-hmr-client: ${message}`);
}
};
5 changes: 0 additions & 5 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* eslint-disable global-require, consistent-return */

(function hotClientEntry() {
// eslint-disable-next-line no-underscore-dangle
if (window.__webpackHotClient__) {
return;
}

// eslint-disable-next-line no-underscore-dangle
window.__webpackHotClient__ = {};

Expand Down
6 changes: 3 additions & 3 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { DefinePlugin, HotModuleReplacementPlugin } = require('webpack');
const HotClientError = require('./HotClientError');

function addEntry(entry, compilerName, options) {
const clientEntry = [`webpack-hot-client/client?${compilerName || uuid()}`];
const clientEntry = [`webpack-hmr-client/client?${compilerName || uuid()}`];
let newEntry = {};

if (!Array.isArray(entry) && typeof entry === 'object') {
Expand Down Expand Up @@ -101,7 +101,7 @@ function validateEntry(entry) {
if (type !== 'function') {
if (!isArray && type !== 'object') {
throw new TypeError(
'webpack-hot-client: The value of `entry` must be an Array, Object, or Function. Please check your webpack config.'
'webpack-hmr-client: The value of `entry` must be an Array, Object, or Function. Please check your webpack config.'
);
}

Expand All @@ -110,7 +110,7 @@ function validateEntry(entry) {
const value = entry[key];
if (!Array.isArray(value)) {
throw new TypeError(
'webpack-hot-client: `entry` Object values must be an Array or Function. Please check your webpack config.'
'webpack-hmr-client: `entry` Object values must be an Array or Function. Please check your webpack config.'
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ module.exports = (compiler, opts) => {

const compile = (comp) => {
const compilerName = comp.name || '<unnamed compiler>';
options.stats = null;
options.lastJsonStats = null;
log.info('webpack: Compiling...');
server.broadcast(payload('compile', { compilerName }));
};

const done = (result) => {
const done = (stats) => {
log.info('webpack: Compiling Done');
options.stats = result;

const jsonStats = options.stats.toJson(options.stats);
const jsonStats = stats.toJson(options.stats);
options.lastJsonStats = jsonStats;

/* istanbul ignore if */
if (!jsonStats) {
Expand Down
13 changes: 8 additions & 5 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ const defaults = {
server: null,
stats: {
context: process.cwd(),
assets: true,
modules: false,
chunks: false,
},
validTargets: ['web'],
test: false,
};

module.exports = (opts = {}) => {
validate({ name: 'webpack-hot-client', schema, target: opts });
validate({ name: 'webpack-hmr-client', schema, target: opts });

const options = merge({}, defaults, opts);
const log = weblog({
name: 'hot',
id: options.test ? null : 'webpack-hot-client',
id: options.test ? null : 'webpack-hmr-client',
level: options.logLevel,
timestamp: options.logTime,
});
Expand All @@ -49,7 +52,7 @@ module.exports = (opts = {}) => {
client: options.host,
server: options.host,
};
} else if (!options.host.server) {
} else if (!options.host.server && !options.server) {
throw new HotClientError(
'`host.server` must be defined when setting host to an Object'
);
Expand All @@ -64,7 +67,7 @@ module.exports = (opts = {}) => {
client: options.port,
server: options.port,
};
} else if (isNaN(parseInt(options.port.server, 10))) {
} else if (isNaN(parseInt(options.port.server, 10)) && !options.server) {
throw new HotClientError(
'`port.server` must be defined when setting host to an Object'
);
Expand All @@ -86,7 +89,7 @@ module.exports = (opts = {}) => {

if (server && server.listening) {
options.webSocket = {
host: server.address().address,
host: options.host.client || server.address().address,
// a port.client value of 0 will be falsy, so it should pull the server port
port: options.port.client || server.address().port,
};
Expand Down
25 changes: 11 additions & 14 deletions lib/socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function getServer(options) {
}
};

const send = sendData.bind(null, wss, options);
const sendPayload = (type, data) => {
broadcast(payload(type, data));
};

const send = sendData.bind(null, options, sendPayload);

return Object.assign(wss, { broadcast, close, send });
}
Expand Down Expand Up @@ -68,15 +72,12 @@ function onConnection(server, options) {

// only send stats to newly connected clients, if no previous clients have
// connected and stats has been modified by webpack
if (options.stats && options.stats.toJson && server.clients.size === 1) {
const jsonStats = options.stats.toJson(options.stats);

/* istanbul ignore if */
if (!jsonStats) {
options.log.error('Client Connection: `stats` is undefined');
}
if (options.lastJsonStats) {
const send = (type, data) => {
socket.send(payload(type, data));
};

server.send(jsonStats);
sendData(options, send, options.lastJsonStats);
}
});
}
Expand Down Expand Up @@ -116,11 +117,7 @@ function payload(type, data) {
return stringify({ type, data });
}

function sendData(server, options, stats) {
const send = (type, data) => {
server.broadcast(payload(type, data));
};

function sendData(options, send, stats) {
if (stats.errors && stats.errors.length > 0) {
if (options.send.errors) {
const errors = [].concat(stats.errors).map((error) => strip(error));
Expand Down
Loading