@@ -26,10 +26,6 @@ interface DevServerOptions {
26
26
ReactHotModuleReplacement : boolean ;
27
27
}
28
28
29
- function arrayContainsStringStartingWith ( array : string [ ] , prefixToFind : string ) {
30
- return array . some ( item => item . substring ( 0 , prefixToFind . length ) === prefixToFind ) ;
31
- }
32
-
33
29
function attachWebpackDevMiddleware ( app : any , webpackConfig : webpack . Configuration , enableHotModuleReplacement : boolean , enableReactHotModuleReplacement : boolean , hmrEndpoint : string ) {
34
30
// Build the final Webpack config based on supplied options
35
31
if ( enableHotModuleReplacement ) {
@@ -49,9 +45,23 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
49
45
const webpackHotMiddlewareOptions = `?path=` + encodeURIComponent ( hmrEndpoint ) ;
50
46
if ( typeof entryPoints [ entryPointName ] === 'string' ) {
51
47
entryPoints [ entryPointName ] = [ webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions , entryPoints [ entryPointName ] ] ;
52
- } else if ( ! arrayContainsStringStartingWith ( entryPoints [ entryPointName ] , webpackHotMiddlewareEntryPoint ) ) {
48
+ } else if ( firstIndexOfStringStartingWith ( entryPoints [ entryPointName ] , webpackHotMiddlewareEntryPoint ) < 0 ) {
53
49
entryPoints [ entryPointName ] . unshift ( webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions ) ;
54
50
}
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
+ }
55
65
} ) ;
56
66
57
67
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) {
168
178
function getPath ( publicPath : string ) {
169
179
return url . parse ( publicPath ) . path ;
170
180
}
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