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;
}
// 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;
// Interface as defined in es6-promise
interface Thenable<T> {
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
}
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>;
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) {
// Build the final Webpack config based on supplied options
if (enableHotModuleReplacement) {
@@ -251,7 +261,13 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
// your Startup.cs.
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 enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;
@@ -321,6 +337,9 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
callback(ex.stack, null);
}
});
},
err => callback(err.stack, null)
);
}
function removeLeadingSlash(str: string) {