Added support for Thenables

This commit is contained in:
waterfoul
2017-12-29 15:32:53 -06:00
committed by Steve Sanderson
parent 78e583d0fb
commit 5f6f288056

View File

@@ -35,15 +35,25 @@ interface DevServerOptions {
EnvParam: any; EnvParam: any;
} }
// We support these three kinds of webpack.config.js export. We don't currently support exported promises // Interface as defined in es6-promise
// (though we might be able to add that in the future, if there's a need). interface Thenable<T> {
type WebpackConfigOrArray = webpack.Configuration | webpack.Configuration[]; then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
interface WebpackConfigFunc { then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
(env?: any): WebpackConfigOrArray;
} }
type WebpackConfigExport = WebpackConfigOrArray | WebpackConfigFunc;
// We support these four kinds of webpack.config.js export
type WebpackConfigOrArray = webpack.Configuration | webpack.Configuration[];
type WebpackConfigOrArrayOrThenable = WebpackConfigOrArray | Thenable<WebpackConfigOrArray>;
interface WebpackConfigFunc {
(env?: any): WebpackConfigOrArrayOrThenable;
}
type WebpackConfigExport = WebpackConfigOrArrayOrThenable | WebpackConfigFunc;
type WebpackConfigModuleExports = WebpackConfigExport | EsModuleExports<WebpackConfigExport>; type WebpackConfigModuleExports = WebpackConfigExport | EsModuleExports<WebpackConfigExport>;
function isThenable(obj: any) {
return obj && typeof (<Thenable<any>>obj).then === 'function';
}
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
if (enableHotModuleReplacement) { if (enableHotModuleReplacement) {
@@ -251,7 +261,13 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
// your Startup.cs. // your Startup.cs.
webpackConfigExport = webpackConfigExport(options.suppliedOptions.EnvParam); webpackConfigExport = webpackConfigExport(options.suppliedOptions.EnvParam);
} }
const webpackConfigArray = webpackConfigExport instanceof Array ? webpackConfigExport : [webpackConfigExport];
const webpackConfigThenable = isThenable(webpackConfigExport)
? webpackConfigExport
: { then: callback => callback(webpackConfigExport) };
webpackConfigThenable.then(webpackConfigResolved => {
const webpackConfigArray = webpackConfigResolved instanceof Array ? webpackConfigResolved : [webpackConfigResolved];
const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement; const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement; const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;
@@ -321,6 +337,9 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
callback(ex.stack, null); callback(ex.stack, null);
} }
}); });
},
err => callback(err.stack, null)
);
} }
function removeLeadingSlash(str: string) { function removeLeadingSlash(str: string) {