From e4d00a2da3e48cc16f3357c8b9d9a91d42022a91 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Fri, 27 Jan 2017 14:44:25 +0000 Subject: [PATCH] Update aspnet-webpack to support Webpack 2-style configs that export a function --- .../npm/aspnet-webpack/package.json | 4 ++-- .../src/WebpackDevMiddleware.ts | 20 ++++++++++++++++--- .../npm/aspnet-webpack/tsconfig.json | 3 ++- 3 files changed, 21 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 cf1f74e..058bde9 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.26", + "version": "1.0.27", "description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.", "main": "index.js", "scripts": { @@ -27,7 +27,7 @@ "devDependencies": { "@types/connect": "^3.4.30", "@types/node": "^6.0.42", - "@types/webpack": "^1.12.34", + "@types/webpack": "^2.2.0", "rimraf": "^2.5.4", "typescript": "^2.0.0", "webpack": "^1.13.2" 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 ba6643f..c9296bf 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts @@ -29,6 +29,14 @@ interface DevServerOptions { ReactHotModuleReplacement: boolean; } +// We support these three kinds of webpack.config.js export. We don't currently support exported promises +// (though we might be able to add that in the future, if there's a need). +type WebpackConfigOrArray = webpack.Configuration | webpack.Configuration[]; +interface WebpackConfigFunc { + (env?: any): WebpackConfigOrArray; +} +type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc; + function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientEndpoint: string, hmrServerEndpoint: string) { // Build the final Webpack config based on supplied options if (enableHotModuleReplacement) { @@ -165,10 +173,16 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option const options: CreateDevServerOptions = JSON.parse(optionsJson); // Read the webpack config's export, and normalize it into the more general 'array of configs' format - let webpackConfigArray: webpack.Configuration[] = requireNewCopy(options.webpackConfigPath); - if (!(webpackConfigArray instanceof Array)) { - webpackConfigArray = [webpackConfigArray as webpack.Configuration]; + let webpackConfigExport: WebpackConfigFileExport = requireNewCopy(options.webpackConfigPath); + 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. + // In the future, we could add support for configuring the 'env' param in Startup.cs. But right + // now, it's not clear that people will want to do that (and they can always make up their own + // default env values in their webpack.config.js). + webpackConfigExport = webpackConfigExport(); } + const webpackConfigArray = webpackConfigExport instanceof Array ? webpackConfigExport : [webpackConfigExport]; const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement; const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement; diff --git a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/tsconfig.json b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/tsconfig.json index 2ebeb45..edd184d 100644 --- a/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/tsconfig.json +++ b/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/tsconfig.json @@ -5,7 +5,8 @@ "target": "es5", "declaration": true, "outDir": ".", - "lib": ["es2015"] + "lib": ["es2015"], + "types": ["node"] }, "files": [ "src/index.ts"