Skip to content

Commit ce40973

Browse files
novascreenSteveSandersonMS
authored andcommitted
Add option to configure Webpack Hot Middleware client
Addresses aspnet#667
1 parent 347524a commit ce40973

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/Microsoft.AspNetCore.SpaServices/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,21 @@ Webpack has built-in HMR support for various types of module, such as styles and
633633

634634
This is [documented in detail on the Webpack site](https://webpack.github.io/docs/hot-module-replacement.html). Or to get a working HMR-enabled ASP.NET Core site with Angular 2, React, React+Redux, or Knockout, you can use the [aspnetcore-spa generator](http://blog.stevensanderson.com/2016/05/02/angular2-react-knockout-apps-on-aspnet-core/).
635635

636+
#### Passing options to the Webpack Hot Middleware client
637+
638+
You can configure the [Webpack Hot Middleware client](https://github.com/glenjamin/webpack-hot-middleware#client)
639+
by modifying your `UseWebpackDevMiddleware` call to include `HotModuleReplacementClientOptions`:
640+
641+
```csharp
642+
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
643+
HotModuleReplacement = true,
644+
HotModuleReplacementClientOptions = new Dictionary<string, string> {
645+
{ "reload", "true" },
646+
},
647+
});
648+
```
649+
650+
**Note**: The `path` option will be ignored, because it is controlled by the `HotModuleReplacementEndpoint` setting.
636651

637652
## Routing helper: MapSpaFallbackRoute
638653

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddlewareOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class WebpackDevMiddlewareOptions
3030
/// </summary>
3131
public bool ReactHotModuleReplacement { get; set; }
3232

33+
/// <summary>
34+
/// Specifies additional options to be passed to the Webpack Hot Middleware client
35+
/// </summary>
36+
public IDictionary<string, string> HotModuleReplacementClientOptions { get; set; }
37+
3338
/// <summary>
3439
/// Specifies the Webpack configuration file to be used. If not set, defaults to 'webpack.config.js'.
3540
/// </summary>

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as webpack from 'webpack';
33
import * as url from 'url';
44
import * as fs from 'fs';
55
import * as path from 'path';
6+
import * as querystring from 'querystring';
67
import { requireNewCopy } from './RequireNewCopy';
78

89
export type CreateDevServerResult = {
@@ -26,6 +27,7 @@ interface CreateDevServerOptions {
2627
interface DevServerOptions {
2728
HotModuleReplacement: boolean;
2829
HotModuleReplacementServerPort: number;
30+
HotModuleReplacementClientOptions: Object;
2931
ReactHotModuleReplacement: boolean;
3032
}
3133

@@ -37,7 +39,7 @@ interface WebpackConfigFunc {
3739
}
3840
type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc;
3941

40-
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientEndpoint: string, hmrServerEndpoint: string) {
42+
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: HotModuleReplacementClientOptions, hmrClientEndpoint: string, hmrServerEndpoint: string) {
4143
// Build the final Webpack config based on supplied options
4244
if (enableHotModuleReplacement) {
4345
// For this, we only support the key/value config format, not string or string[], since
@@ -53,7 +55,8 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
5355
// Augment all entry points so they support HMR (unless they already do)
5456
Object.getOwnPropertyNames(entryPoints).forEach(entryPointName => {
5557
const webpackHotMiddlewareEntryPoint = 'webpack-hot-middleware/client';
56-
const webpackHotMiddlewareOptions = `?path=` + encodeURIComponent(hmrClientEndpoint);
58+
hmrClientOptions.path = hmrClientEndpoint;
59+
const webpackHotMiddlewareOptions = '?' + querystring.stringify(hmrClientOptions);
5760
if (typeof entryPoints[entryPointName] === 'string') {
5861
entryPoints[entryPointName] = [webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions, entryPoints[entryPointName]];
5962
} else if (firstIndexOfStringStartingWith(entryPoints[entryPointName], webpackHotMiddlewareEntryPoint) < 0) {
@@ -225,6 +228,7 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
225228

226229
const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
227230
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;
231+
const hmrClientOptions = options.suppliedOptions.HotModuleReplacementClientOptions || {};
228232
if (enableReactHotModuleReplacement && !enableHotModuleReplacement) {
229233
callback('To use ReactHotModuleReplacement, you must also enable the HotModuleReplacement option.', null);
230234
return;
@@ -263,7 +267,7 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
263267
|| `http://localhost:${listener.address().port}/__webpack_hmr`; // Fall back on absolute URL to bypass proxying
264268
const hmrServerEndpoint = options.hotModuleReplacementEndpointUrl
265269
|| '/__webpack_hmr'; // URL is relative to webpack dev server root
266-
attachWebpackDevMiddleware(app, webpackConfig, enableHotModuleReplacement, enableReactHotModuleReplacement, hmrClientEndpoint, hmrServerEndpoint);
270+
attachWebpackDevMiddleware(app, webpackConfig, enableHotModuleReplacement, enableReactHotModuleReplacement, hmrClientOptions, hmrClientEndpoint, hmrServerEndpoint);
267271
}
268272
});
269273

0 commit comments

Comments
 (0)