Make the fix for #365 not be a breaking change (at least, for the considerable majority of aspnet-webpack users)

This commit is contained in:
SteveSandersonMS
2016-10-10 12:36:52 +01:00
parent 3568476cca
commit d5055dbaf3

View File

@@ -50,7 +50,13 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
}
// Now also inject eventsource polyfill so this can work on IE/Edge (unless it's already there)
// To avoid this being a breaking change for everyone who uses aspnet-webpack, we only do this if you've
// referenced event-source-polyfill in your package.json. Note that having event-source-polyfill available
// on the server in node_modules doesn't imply that you've also included it in your client-side bundle,
// but the converse is true (if it's not in node_modules, then you obviously aren't trying to use it at
// all, so it would definitely not work to take a dependency on it).
const eventSourcePolyfillEntryPoint = 'event-source-polyfill';
if (npmModuleIsPresent(eventSourcePolyfillEntryPoint)) {
const entryPointsArray: string[] = entryPoints[entryPointName]; // We know by now that it's an array, because if it wasn't, we already wrapped it in one
if (entryPointsArray.indexOf(eventSourcePolyfillEntryPoint) < 0) {
const webpackHmrIndex = firstIndexOfStringStartingWith(entryPointsArray, webpackHotMiddlewareEntryPoint);
@@ -62,6 +68,7 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
// Insert the polyfill just before the HMR entrypoint
entryPointsArray.splice(webpackHmrIndex, 0, eventSourcePolyfillEntryPoint);
}
}
});
webpackConfig.plugins = [].concat(webpackConfig.plugins || []); // Be sure not to mutate the original array, as it might be shared
@@ -189,3 +196,12 @@ function firstIndexOfStringStartingWith(array: string[], prefixToFind: string) {
return -1; // Not found
}
function npmModuleIsPresent(moduleName: string) {
try {
require.resolve(moduleName);
return true;
} catch (ex) {
return false;
}
}