| [9af201e] | 1 | /** @typedef {string | string[] | import('webpack').Entry} StaticEntry */
|
|---|
| 2 | /** @typedef {StaticEntry | import('webpack').EntryFunc} WebpackEntry */
|
|---|
| 3 |
|
|---|
| 4 | const EntryParseError = new Error(
|
|---|
| 5 | [
|
|---|
| 6 | '[ReactRefreshPlugin]',
|
|---|
| 7 | 'Failed to parse the Webpack `entry` object!',
|
|---|
| 8 | 'Please ensure the `entry` option in your Webpack config is specified.',
|
|---|
| 9 | ].join(' ')
|
|---|
| 10 | );
|
|---|
| 11 |
|
|---|
| 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 | /**
|
|---|
| 25 | * Checks if a Webpack entry string is related to socket integrations.
|
|---|
| 26 | * @param {string} entry A Webpack entry string.
|
|---|
| 27 | * @returns {boolean} Whether the entry is related to socket integrations.
|
|---|
| 28 | */
|
|---|
| 29 | function isSocketEntry(entry) {
|
|---|
| 30 | return socketEntries.some((socketEntry) => entry.includes(socketEntry));
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Injects an entry to the bundle for react-refresh.
|
|---|
| 35 | * @param {WebpackEntry} [originalEntry] A Webpack entry object.
|
|---|
| 36 | * @param {import('./getAdditionalEntries').AdditionalEntries} additionalEntries An object that contains the Webpack entries for prepending and the overlay feature
|
|---|
| 37 | * @returns {WebpackEntry} An injected entry object.
|
|---|
| 38 | */
|
|---|
| 39 | function injectRefreshEntry(originalEntry, additionalEntries) {
|
|---|
| 40 | const { prependEntries, overlayEntries } = additionalEntries;
|
|---|
| 41 |
|
|---|
| 42 | // Single string entry point
|
|---|
| 43 | if (typeof originalEntry === 'string') {
|
|---|
| 44 | if (isSocketEntry(originalEntry)) {
|
|---|
| 45 | return [...prependEntries, originalEntry, ...overlayEntries];
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | return [...prependEntries, ...overlayEntries, originalEntry];
|
|---|
| 49 | }
|
|---|
| 50 | // Single array entry point
|
|---|
| 51 | if (Array.isArray(originalEntry)) {
|
|---|
| 52 | if (originalEntry.length === 0) {
|
|---|
| 53 | throw EntryParseError;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | const socketEntryIndex = originalEntry.findIndex(isSocketEntry);
|
|---|
| 57 |
|
|---|
| 58 | let socketAndPrecedingEntries = [];
|
|---|
| 59 | if (socketEntryIndex !== -1) {
|
|---|
| 60 | socketAndPrecedingEntries = originalEntry.splice(0, socketEntryIndex + 1);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | return [...prependEntries, ...socketAndPrecedingEntries, ...overlayEntries, ...originalEntry];
|
|---|
| 64 | }
|
|---|
| 65 | // Multiple entry points
|
|---|
| 66 | if (typeof originalEntry === 'object') {
|
|---|
| 67 | const entries = Object.entries(originalEntry);
|
|---|
| 68 | if (entries.length === 0) {
|
|---|
| 69 | throw EntryParseError;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | return entries.reduce(
|
|---|
| 73 | (acc, [curKey, curEntry]) => ({
|
|---|
| 74 | ...acc,
|
|---|
| 75 | [curKey]:
|
|---|
| 76 | typeof curEntry === 'object' && curEntry.import
|
|---|
| 77 | ? {
|
|---|
| 78 | ...curEntry,
|
|---|
| 79 | import: injectRefreshEntry(curEntry.import, additionalEntries),
|
|---|
| 80 | }
|
|---|
| 81 | : injectRefreshEntry(curEntry, additionalEntries),
|
|---|
| 82 | }),
|
|---|
| 83 | {}
|
|---|
| 84 | );
|
|---|
| 85 | }
|
|---|
| 86 | // Dynamic entry points
|
|---|
| 87 | if (typeof originalEntry === 'function') {
|
|---|
| 88 | return (...args) =>
|
|---|
| 89 | Promise.resolve(originalEntry(...args)).then((resolvedEntry) =>
|
|---|
| 90 | injectRefreshEntry(resolvedEntry, additionalEntries)
|
|---|
| 91 | );
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | throw EntryParseError;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | module.exports = injectRefreshEntry;
|
|---|
| 98 | module.exports.socketEntries = socketEntries;
|
|---|