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

Commit 5cb1846

Browse files
Fix and reorganise WebpackDevMiddleware.ts following PR
1 parent 1f03b1e commit 5cb1846

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ interface CreateDevServerOptions {
2323
hotModuleReplacementEndpointUrl: string;
2424
}
2525

26+
type StringMap<T> = [(key: string) => T];
27+
2628
// These are the options configured in C# and then JSON-serialized, hence the C#-style naming
2729
interface DevServerOptions {
2830
HotModuleReplacement: boolean;
2931
HotModuleReplacementServerPort: number;
30-
HotModuleReplacementClientOptions: Object;
32+
HotModuleReplacementClientOptions: StringMap<string>;
3133
ReactHotModuleReplacement: boolean;
3234
}
3335

@@ -39,7 +41,7 @@ interface WebpackConfigFunc {
3941
}
4042
type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc;
4143

42-
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: HotModuleReplacementClientOptions, hmrClientEndpoint: string, hmrServerEndpoint: string) {
44+
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: StringMap<string>, hmrServerEndpoint: string) {
4345
// Build the final Webpack config based on supplied options
4446
if (enableHotModuleReplacement) {
4547
// For this, we only support the key/value config format, not string or string[], since
@@ -55,7 +57,6 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
5557
// Augment all entry points so they support HMR (unless they already do)
5658
Object.getOwnPropertyNames(entryPoints).forEach(entryPointName => {
5759
const webpackHotMiddlewareEntryPoint = 'webpack-hot-middleware/client';
58-
hmrClientOptions.path = hmrClientEndpoint;
5960
const webpackHotMiddlewareOptions = '?' + querystring.stringify(hmrClientOptions);
6061
if (typeof entryPoints[entryPointName] === 'string') {
6162
entryPoints[entryPointName] = [webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions, entryPoints[entryPointName]];
@@ -228,7 +229,6 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
228229

229230
const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
230231
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;
231-
const hmrClientOptions = options.suppliedOptions.HotModuleReplacementClientOptions || {};
232232
if (enableReactHotModuleReplacement && !enableHotModuleReplacement) {
233233
callback('To use ReactHotModuleReplacement, you must also enable the HotModuleReplacement option.', null);
234234
return;
@@ -267,7 +267,12 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
267267
|| `http://localhost:${listener.address().port}/__webpack_hmr`; // Fall back on absolute URL to bypass proxying
268268
const hmrServerEndpoint = options.hotModuleReplacementEndpointUrl
269269
|| '/__webpack_hmr'; // URL is relative to webpack dev server root
270-
attachWebpackDevMiddleware(app, webpackConfig, enableHotModuleReplacement, enableReactHotModuleReplacement, hmrClientOptions, hmrClientEndpoint, hmrServerEndpoint);
270+
271+
// We always overwrite the 'path' option as it needs to match what the .NET side is expecting
272+
const hmrClientOptions = options.suppliedOptions.HotModuleReplacementClientOptions || <StringMap<string>>{};
273+
hmrClientOptions['path'] = hmrClientEndpoint;
274+
275+
attachWebpackDevMiddleware(app, webpackConfig, enableHotModuleReplacement, enableReactHotModuleReplacement, hmrClientOptions, hmrServerEndpoint);
271276
}
272277
});
273278

0 commit comments

Comments
 (0)