Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit d5055db

Browse files
Make the fix for #365 not be a breaking change (at least, for the considerable majority of aspnet-webpack users)
1 parent 3568476 commit d5055db

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,24 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
5050
}
5151

5252
// Now also inject eventsource polyfill so this can work on IE/Edge (unless it's already there)
53+
// To avoid this being a breaking change for everyone who uses aspnet-webpack, we only do this if you've
54+
// referenced event-source-polyfill in your package.json. Note that having event-source-polyfill available
55+
// on the server in node_modules doesn't imply that you've also included it in your client-side bundle,
56+
// but the converse is true (if it's not in node_modules, then you obviously aren't trying to use it at
57+
// all, so it would definitely not work to take a dependency on it).
5358
const eventSourcePolyfillEntryPoint = 'event-source-polyfill';
54-
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
55-
if (entryPointsArray.indexOf(eventSourcePolyfillEntryPoint) < 0) {
56-
const webpackHmrIndex = firstIndexOfStringStartingWith(entryPointsArray, webpackHotMiddlewareEntryPoint);
57-
if (webpackHmrIndex < 0) {
58-
// This should not be possible, since we just added it if it was missing
59-
throw new Error('Cannot find ' + webpackHotMiddlewareEntryPoint + ' in entry points array: ' + entryPointsArray);
60-
}
59+
if (npmModuleIsPresent(eventSourcePolyfillEntryPoint)) {
60+
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
61+
if (entryPointsArray.indexOf(eventSourcePolyfillEntryPoint) < 0) {
62+
const webpackHmrIndex = firstIndexOfStringStartingWith(entryPointsArray, webpackHotMiddlewareEntryPoint);
63+
if (webpackHmrIndex < 0) {
64+
// This should not be possible, since we just added it if it was missing
65+
throw new Error('Cannot find ' + webpackHotMiddlewareEntryPoint + ' in entry points array: ' + entryPointsArray);
66+
}
6167

62-
// Insert the polyfill just before the HMR entrypoint
63-
entryPointsArray.splice(webpackHmrIndex, 0, eventSourcePolyfillEntryPoint);
68+
// Insert the polyfill just before the HMR entrypoint
69+
entryPointsArray.splice(webpackHmrIndex, 0, eventSourcePolyfillEntryPoint);
70+
}
6471
}
6572
});
6673

@@ -189,3 +196,12 @@ function firstIndexOfStringStartingWith(array: string[], prefixToFind: string) {
189196

190197
return -1; // Not found
191198
}
199+
200+
function npmModuleIsPresent(moduleName: string) {
201+
try {
202+
require.resolve(moduleName);
203+
return true;
204+
} catch (ex) {
205+
return false;
206+
}
207+
}

0 commit comments

Comments
 (0)