123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- const querystring = require('querystring');
- const createError = require('./createError');
- function isSocketEntry(entry) {
-
- const socketEntries = [
- 'webpack-dev-server/client',
- 'webpack-hot-middleware/client',
- 'webpack-plugin-serve/client',
- 'react-dev-utils/webpackHotDevClient',
- ];
- return socketEntries.some((socketEntry) => entry.includes(socketEntry));
- }
- function injectRefreshEntry(originalEntry, options) {
-
- let resourceQuery = {};
- if (options.overlay) {
- options.overlay.sockHost && (resourceQuery.sockHost = options.overlay.sockHost);
- options.overlay.sockPath && (resourceQuery.sockPath = options.overlay.sockPath);
- options.overlay.sockPort && (resourceQuery.sockPort = options.overlay.sockPort);
- }
-
- const queryString = querystring.stringify(resourceQuery, undefined, undefined, {
-
- encodeURIComponent(string) {
- return string;
- },
- });
- const prependEntries = [
-
- require.resolve('../../client/ReactRefreshEntry'),
- ];
- const overlayEntries = [
-
- options.overlay &&
- options.overlay.useLegacyWDSSockets &&
- require.resolve('../../client/LegacyWDSSocketEntry'),
-
- options.overlay &&
- options.overlay.entry &&
- options.overlay.entry + (queryString && `?${queryString}`),
- ].filter(Boolean);
-
- if (typeof originalEntry === 'string') {
- if (isSocketEntry(originalEntry)) {
- return [...prependEntries, originalEntry, ...overlayEntries];
- }
- return [...prependEntries, ...overlayEntries, originalEntry];
- }
-
- if (Array.isArray(originalEntry)) {
- 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') {
- return Object.entries(originalEntry).reduce(
- (acc, [curKey, curEntry]) => ({
- ...acc,
- [curKey]:
- typeof curEntry === 'object' && curEntry.import
- ? {
- ...curEntry,
- import: injectRefreshEntry(curEntry.import, options),
- }
- : injectRefreshEntry(curEntry, options),
- }),
- {}
- );
- }
-
- if (typeof originalEntry === 'function') {
- return (...args) =>
- Promise.resolve(originalEntry(...args)).then((resolvedEntry) =>
- injectRefreshEntry(resolvedEntry, options)
- );
- }
- throw createError('Failed to parse the Webpack `entry` object!');
- }
- module.exports = injectRefreshEntry;
|