@@ -3,6 +3,27 @@ const querystring = require('querystring');
33/** @typedef {string | string[] | import('webpack').Entry } StaticEntry */
44/** @typedef {StaticEntry | import('webpack').EntryFunc } WebpackEntry */
55
6+ /**
7+ * Checks if a Webpack entry string is related to socket integrations.
8+ * @param {string } entry A Webpack entry string.
9+ * @returns {boolean } Whether the entry is related to socket integrations.
10+ */
11+ function isSocketEntry ( entry ) {
12+ /**
13+ * Webpack entries related to socket integrations.
14+ * They have to run before any code that sets up the error overlay.
15+ * @type {string[] }
16+ */
17+ const socketEntries = [
18+ 'webpack-dev-server/client' ,
19+ 'webpack-hot-middleware/client' ,
20+ 'webpack-plugin-serve/client' ,
21+ 'react-dev-utils/webpackHotDevClient' ,
22+ ] ;
23+
24+ return socketEntries . some ( ( socketEntry ) => entry . includes ( socketEntry ) ) ;
25+ }
26+
627/**
728 * Injects an entry to the bundle for react-refresh.
829 * @param {WebpackEntry } [originalEntry] A Webpack entry object.
@@ -34,7 +55,7 @@ function injectRefreshEntry(originalEntry, options) {
3455 require . resolve ( '../runtime/ReactRefreshEntry' ) ,
3556 ] ;
3657
37- const appendEntries = [
58+ const overlayEntries = [
3859 // Legacy WDS SockJS integration
3960 options . useLegacyWDSSockets && require . resolve ( '../runtime/LegacyWDSSocketEntry' ) ,
4061 // Error overlay runtime
@@ -43,11 +64,22 @@ function injectRefreshEntry(originalEntry, options) {
4364
4465 // Single string entry point
4566 if ( typeof originalEntry === 'string' ) {
46- return [ ...prependEntries , originalEntry , ...appendEntries ] ;
67+ if ( isSocketEntry ( originalEntry ) ) {
68+ return [ ...prependEntries , originalEntry , ...overlayEntries ] ;
69+ }
70+
71+ return [ ...prependEntries , ...overlayEntries , originalEntry ] ;
4772 }
4873 // Single array entry point
4974 if ( Array . isArray ( originalEntry ) ) {
50- return [ ...prependEntries , ...originalEntry , ...appendEntries ] ;
75+ const socketEntryIndex = originalEntry . findIndex ( isSocketEntry ) ;
76+
77+ let socketEntry = [ ] ;
78+ if ( socketEntryIndex !== - 1 ) {
79+ socketEntry = originalEntry . splice ( socketEntryIndex , 1 ) ;
80+ }
81+
82+ return [ ...prependEntries , ...socketEntry , ...overlayEntries , ...originalEntry ] ;
5183 }
5284 // Multiple entry points
5385 if ( typeof originalEntry === 'object' ) {
0 commit comments