From 18e8b7101c1e812c479b2ff75c4b4f9a9dbc5941 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Tue, 4 Oct 2016 11:36:10 +0100 Subject: [PATCH] In aspnet-webpack, allow webpack-hot-middleware/client to be added manually with options. Fixes #353 --- .../npm/aspnet-webpack/package.json | 2 +- .../npm/aspnet-webpack/src/WebpackDevMiddleware.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json index 4b0d282..93cd9b5 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json @@ -1,6 +1,6 @@ { "name": "aspnet-webpack", - "version": "1.0.16", + "version": "1.0.17", "description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.", "main": "index.js", "scripts": { diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts index 6ae1a1f..3c53aea 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts @@ -26,6 +26,10 @@ interface DevServerOptions { ReactHotModuleReplacement: boolean; } +function arrayContainsStringStartingWith(array: string[], prefixToFind: string) { + return array.some(item => item.substring(0, prefixToFind.length) === prefixToFind); +} + function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean) { // Build the final Webpack config based on supplied options if (enableHotModuleReplacement) { @@ -39,12 +43,13 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati throw new Error('To use HotModuleReplacement, your webpack config must specify an \'entry\' value as a key-value object (e.g., "entry: { main: \'ClientApp/boot-client.ts\' }")'); } - // Augment all entry points so they support HMR + // Augment all entry points so they support HMR (unless they already do) Object.getOwnPropertyNames(entryPoints).forEach(entryPointName => { + const webpackHotMiddlewareEntryPoint = 'webpack-hot-middleware/client'; if (typeof entryPoints[entryPointName] === 'string') { - entryPoints[entryPointName] = ['webpack-hot-middleware/client', entryPoints[entryPointName]]; - } else { - entryPoints[entryPointName].unshift('webpack-hot-middleware/client'); + entryPoints[entryPointName] = [webpackHotMiddlewareEntryPoint, entryPoints[entryPointName]]; + } else if (!arrayContainsStringStartingWith(entryPoints[entryPointName], webpackHotMiddlewareEntryPoint)) { + entryPoints[entryPointName].unshift(webpackHotMiddlewareEntryPoint); } });