In WebpackDevMiddleware.ts, support loading Webpack config files with __esModule. Fixes #1378

This commit is contained in:
Steve Sanderson
2017-11-09 16:57:57 -08:00
parent 0c77224f46
commit 08002e961b

View File

@@ -23,6 +23,7 @@ interface CreateDevServerOptions {
hotModuleReplacementEndpointUrl: string; hotModuleReplacementEndpointUrl: string;
} }
type EsModuleExports<T> = { __esModule: true, default: T };
type StringMap<T> = [(key: string) => T]; type StringMap<T> = [(key: string) => T];
// These are the options configured in C# and then JSON-serialized, hence the C#-style naming // 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 { interface WebpackConfigFunc {
(env?: any): WebpackConfigOrArray; (env?: any): WebpackConfigOrArray;
} }
type WebpackConfigFileExport = WebpackConfigOrArray | WebpackConfigFunc; type WebpackConfigExport = WebpackConfigOrArray | WebpackConfigFunc;
type WebpackConfigModuleExports = WebpackConfigExport | EsModuleExports<WebpackConfigExport>;
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: StringMap<string>, hmrServerEndpoint: string) { function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrClientOptions: StringMap<string>, hmrServerEndpoint: string) {
// Build the final Webpack config based on supplied options // 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 // 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<WebpackConfigExport>).default
: (webpackConfigModuleExports as WebpackConfigExport);
if (webpackConfigExport instanceof Function) { if (webpackConfigExport instanceof Function) {
// If you export a function, we'll call it with an undefined 'env' arg, since we have nothing else // 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. // to pass. This is the same as what the webpack CLI tool does if you specify no '--env.x' values.