Skip to content

Commit e4d00a2

Browse files
Update aspnet-webpack to support Webpack 2-style configs that export a function
1 parent 345b4f6 commit e4d00a2

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aspnet-webpack",
3-
"version": "1.0.26",
3+
"version": "1.0.27",
44
"description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.",
55
"main": "index.js",
66
"scripts": {
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"@types/connect": "^3.4.30",
2929
"@types/node": "^6.0.42",
30-
"@types/webpack": "^1.12.34",
30+
"@types/webpack": "^2.2.0",
3131
"rimraf": "^2.5.4",
3232
"typescript": "^2.0.0",
3333
"webpack": "^1.13.2"

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ interface DevServerOptions {
2929
ReactHotModuleReplacement: boolean;
3030
}
3131

32+
// We support these three kinds of webpack.config.js export. We don't currently support exported promises
33+
// (though we might be able to add that in the future, if there's a need).
34+
type WebpackConfigOrArray = webpack.Configuration | webpack.Configuration[];
35+
interface WebpackConfigFunc {
36+
(env?: any): WebpackConfigOrArray;
37+
}
38+
type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc;
39+
3240
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientEndpoint: string, hmrServerEndpoint: string) {
3341
// Build the final Webpack config based on supplied options
3442
if (enableHotModuleReplacement) {
@@ -165,10 +173,16 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
165173
const options: CreateDevServerOptions = JSON.parse(optionsJson);
166174

167175
// Read the webpack config's export, and normalize it into the more general 'array of configs' format
168-
let webpackConfigArray: webpack.Configuration[] = requireNewCopy(options.webpackConfigPath);
169-
if (!(webpackConfigArray instanceof Array)) {
170-
webpackConfigArray = [webpackConfigArray as webpack.Configuration];
176+
let webpackConfigExport: WebpackConfigFileExport = requireNewCopy(options.webpackConfigPath);
177+
if (webpackConfigExport instanceof Function) {
178+
// If you export a function, we'll call it with an undefined 'env' arg, since we have nothing else
179+
// to pass. This is the same as what the webpack CLI tool does if you specify no '--env.x' values.
180+
// In the future, we could add support for configuring the 'env' param in Startup.cs. But right
181+
// now, it's not clear that people will want to do that (and they can always make up their own
182+
// default env values in their webpack.config.js).
183+
webpackConfigExport = webpackConfigExport();
171184
}
185+
const webpackConfigArray = webpackConfigExport instanceof Array ? webpackConfigExport : [webpackConfigExport];
172186

173187
const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
174188
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"target": "es5",
66
"declaration": true,
77
"outDir": ".",
8-
"lib": ["es2015"]
8+
"lib": ["es2015"],
9+
"types": ["node"]
910
},
1011
"files": [
1112
"src/index.ts"

0 commit comments

Comments
 (0)