From 08002e961bcd63db839896a610d90e62db337a9f Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 9 Nov 2017 16:57:57 -0800 Subject: [PATCH] In WebpackDevMiddleware.ts, support loading Webpack config files with __esModule. Fixes #1378 --- .../npm/aspnet-webpack/src/WebpackDevMiddleware.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 6731f97..a160f83 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts @@ -23,6 +23,7 @@ interface CreateDevServerOptions { hotModuleReplacementEndpointUrl: string; } +type EsModuleExports = { __esModule: true, default: T }; type StringMap = [(key: string) => T]; // These are the options configured in C# and then JSON-serialized, hence the C#-style naming @@ -39,7 +40,8 @@ type WebpackConfigOrArray = webpack.Configuration | webpack.Configuration[]; interface WebpackConfigFunc { (env?: any): WebpackConfigOrArray; } -type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc; +type WebpackConfigExport = WebpackConfigOrArray | WebpackConfigFunc; +type WebpackConfigModuleExports = WebpackConfigExport | EsModuleExports; function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: StringMap, hmrServerEndpoint: string) { // Build the final Webpack config based on supplied options @@ -235,7 +237,11 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option } // Read the webpack config's export, and normalize it into the more general 'array of configs' format - let webpackConfigExport: WebpackConfigFileExport = requireNewCopy(options.webpackConfigPath); + const webpackConfigModuleExports: WebpackConfigModuleExports = requireNewCopy(options.webpackConfigPath); + let webpackConfigExport = (webpackConfigModuleExports as EsModuleExports<{}>).__esModule === true + ? (webpackConfigModuleExports as EsModuleExports).default + : (webpackConfigModuleExports as WebpackConfigExport); + if (webpackConfigExport instanceof Function) { // If you export a function, we'll call it with an undefined 'env' arg, since we have nothing else // to pass. This is the same as what the webpack CLI tool does if you specify no '--env.x' values.