From bc2de2ad598d90c29e19f1b585afbf074384a93c Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Fri, 9 Sep 2016 13:21:31 +0100 Subject: [PATCH] In aspnet-webpack HMR, don't rely on assumption that entry point is called 'main'. Fixes #289. --- .../npm/aspnet-webpack/package.json | 2 +- .../src/WebpackDevMiddleware.ts | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json index e58c2b4..077e6ff 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.10", + "version": "1.0.11", "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 f34eefd..ff47855 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts @@ -43,12 +43,26 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option const listener = app.listen(suggestedHMRPortOrZero, () => { // Build the final Webpack config based on supplied options if (enableHotModuleReplacement) { - // TODO: Stop assuming there's an entry point called 'main' - if (typeof webpackConfig.entry['main'] === 'string') { - webpackConfig.entry['main'] = ['webpack-hot-middleware/client', webpackConfig.entry['main']]; - } else { - webpackConfig.entry['main'].unshift('webpack-hot-middleware/client'); + // For this, we only support the key/value config format, not string or string[], since + // those ones don't clearly indicate what the resulting bundle name will be + const entryPoints = webpackConfig.entry; + const isObjectStyleConfig = entryPoints + && typeof entryPoints === 'object' + && !(entryPoints instanceof Array); + if (!isObjectStyleConfig) { + callback('To use HotModuleReplacement, your webpack config must specify an \'entry\' value as a key-value object (e.g., "entry: { main: \'ClientApp/boot-client.ts\' }")', null); + return; } + + // Augment all entry points so they support HMR + Object.getOwnPropertyNames(entryPoints).forEach(entryPointName => { + if (typeof entryPoints[entryPointName] === 'string') { + entryPoints[entryPointName] = ['webpack-hot-middleware/client', entryPoints[entryPointName]]; + } else { + entryPoints[entryPointName].unshift('webpack-hot-middleware/client'); + } + }); + webpackConfig.plugins.push( new webpack.HotModuleReplacementPlugin() );