Update aspnet-webpack to support Webpack 2-style configs that export a function

This commit is contained in:
SteveSandersonMS
2017-01-27 14:44:25 +00:00
parent 345b4f64b5
commit e4d00a2da3
3 changed files with 21 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "aspnet-webpack", "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.", "description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@@ -27,7 +27,7 @@
"devDependencies": { "devDependencies": {
"@types/connect": "^3.4.30", "@types/connect": "^3.4.30",
"@types/node": "^6.0.42", "@types/node": "^6.0.42",
"@types/webpack": "^1.12.34", "@types/webpack": "^2.2.0",
"rimraf": "^2.5.4", "rimraf": "^2.5.4",
"typescript": "^2.0.0", "typescript": "^2.0.0",
"webpack": "^1.13.2" "webpack": "^1.13.2"

View File

@@ -29,6 +29,14 @@ interface DevServerOptions {
ReactHotModuleReplacement: boolean; 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) { function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientEndpoint: string, hmrServerEndpoint: string) {
// Build the final Webpack config based on supplied options // Build the final Webpack config based on supplied options
if (enableHotModuleReplacement) { if (enableHotModuleReplacement) {
@@ -165,10 +173,16 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
const options: CreateDevServerOptions = JSON.parse(optionsJson); const options: CreateDevServerOptions = JSON.parse(optionsJson);
// Read the webpack config's export, and normalize it into the more general 'array of configs' format // Read the webpack config's export, and normalize it into the more general 'array of configs' format
let webpackConfigArray: webpack.Configuration[] = requireNewCopy(options.webpackConfigPath); let webpackConfigExport: WebpackConfigFileExport = requireNewCopy(options.webpackConfigPath);
if (!(webpackConfigArray instanceof Array)) { if (webpackConfigExport instanceof Function) {
webpackConfigArray = [webpackConfigArray as webpack.Configuration]; // 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 enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement; const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;

View File

@@ -5,7 +5,8 @@
"target": "es5", "target": "es5",
"declaration": true, "declaration": true,
"outDir": ".", "outDir": ".",
"lib": ["es2015"] "lib": ["es2015"],
"types": ["node"]
}, },
"files": [ "files": [
"src/index.ts" "src/index.ts"