|
|
|
|
|
|
|
|
|
|
|
const EntryParseError = new Error( |
|
|
[ |
|
|
'[ReactRefreshPlugin]', |
|
|
'Failed to parse the Webpack `entry` object!', |
|
|
'Please ensure the `entry` option in your Webpack config is specified.', |
|
|
].join(' ') |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const socketEntries = [ |
|
|
'webpack-dev-server/client', |
|
|
'webpack-hot-middleware/client', |
|
|
'webpack-plugin-serve/client', |
|
|
'react-dev-utils/webpackHotDevClient', |
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isSocketEntry(entry) { |
|
|
return socketEntries.some((socketEntry) => entry.includes(socketEntry)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function injectRefreshEntry(originalEntry, additionalEntries) { |
|
|
const { prependEntries, overlayEntries } = additionalEntries; |
|
|
|
|
|
|
|
|
if (typeof originalEntry === 'string') { |
|
|
if (isSocketEntry(originalEntry)) { |
|
|
return [...prependEntries, originalEntry, ...overlayEntries]; |
|
|
} |
|
|
|
|
|
return [...prependEntries, ...overlayEntries, originalEntry]; |
|
|
} |
|
|
|
|
|
if (Array.isArray(originalEntry)) { |
|
|
if (originalEntry.length === 0) { |
|
|
throw EntryParseError; |
|
|
} |
|
|
|
|
|
const socketEntryIndex = originalEntry.findIndex(isSocketEntry); |
|
|
|
|
|
let socketAndPrecedingEntries = []; |
|
|
if (socketEntryIndex !== -1) { |
|
|
socketAndPrecedingEntries = originalEntry.splice(0, socketEntryIndex + 1); |
|
|
} |
|
|
|
|
|
return [...prependEntries, ...socketAndPrecedingEntries, ...overlayEntries, ...originalEntry]; |
|
|
} |
|
|
|
|
|
if (typeof originalEntry === 'object') { |
|
|
const entries = Object.entries(originalEntry); |
|
|
if (entries.length === 0) { |
|
|
throw EntryParseError; |
|
|
} |
|
|
|
|
|
return entries.reduce( |
|
|
(acc, [curKey, curEntry]) => ({ |
|
|
...acc, |
|
|
[curKey]: |
|
|
typeof curEntry === 'object' && curEntry.import |
|
|
? { |
|
|
...curEntry, |
|
|
import: injectRefreshEntry(curEntry.import, additionalEntries), |
|
|
} |
|
|
: injectRefreshEntry(curEntry, additionalEntries), |
|
|
}), |
|
|
{} |
|
|
); |
|
|
} |
|
|
|
|
|
if (typeof originalEntry === 'function') { |
|
|
return (...args) => |
|
|
Promise.resolve(originalEntry(...args)).then((resolvedEntry) => |
|
|
injectRefreshEntry(resolvedEntry, additionalEntries) |
|
|
); |
|
|
} |
|
|
|
|
|
throw EntryParseError; |
|
|
} |
|
|
|
|
|
module.exports = injectRefreshEntry; |
|
|
module.exports.socketEntries = socketEntries; |
|
|
|