@@ -26,10 +26,6 @@ interface DevServerOptions {
2626 ReactHotModuleReplacement : boolean ;
2727}
2828
29- function arrayContainsStringStartingWith ( array : string [ ] , prefixToFind : string ) {
30- return array . some ( item => item . substring ( 0 , prefixToFind . length ) === prefixToFind ) ;
31- }
32-
3329function attachWebpackDevMiddleware ( app : any , webpackConfig : webpack . Configuration , enableHotModuleReplacement : boolean , enableReactHotModuleReplacement : boolean , hmrEndpoint : string ) {
3430 // Build the final Webpack config based on supplied options
3531 if ( enableHotModuleReplacement ) {
@@ -49,9 +45,23 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
4945 const webpackHotMiddlewareOptions = `?path=` + encodeURIComponent ( hmrEndpoint ) ;
5046 if ( typeof entryPoints [ entryPointName ] === 'string' ) {
5147 entryPoints [ entryPointName ] = [ webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions , entryPoints [ entryPointName ] ] ;
52- } else if ( ! arrayContainsStringStartingWith ( entryPoints [ entryPointName ] , webpackHotMiddlewareEntryPoint ) ) {
48+ } else if ( firstIndexOfStringStartingWith ( entryPoints [ entryPointName ] , webpackHotMiddlewareEntryPoint ) < 0 ) {
5349 entryPoints [ entryPointName ] . unshift ( webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions ) ;
5450 }
51+
52+ // Now also inject eventsource polyfill so this can work on IE/Edge (unless it's already there)
53+ 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+ }
61+
62+ // Insert the polyfill just before the HMR entrypoint
63+ entryPointsArray . splice ( webpackHmrIndex , 0 , eventSourcePolyfillEntryPoint ) ;
64+ }
5565 } ) ;
5666
5767 webpackConfig . plugins = [ ] . concat ( webpackConfig . plugins || [ ] ) ; // Be sure not to mutate the original array, as it might be shared
@@ -168,3 +178,14 @@ function removeTrailingSlash(str: string) {
168178function getPath ( publicPath : string ) {
169179 return url . parse ( publicPath ) . path ;
170180}
181+
182+ function firstIndexOfStringStartingWith ( array : string [ ] , prefixToFind : string ) {
183+ for ( let index = 0 ; index < array . length ; index ++ ) {
184+ const candidate = array [ index ] ;
185+ if ( candidate . substring ( 0 , prefixToFind . length ) === prefixToFind ) {
186+ return index ;
187+ }
188+ }
189+
190+ return - 1 ; // Not found
191+ }
0 commit comments