| 1 | const querystring = require('querystring');
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * @typedef {Object} AdditionalEntries
|
|---|
| 5 | * @property {string[]} prependEntries
|
|---|
| 6 | * @property {string[]} overlayEntries
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Creates an object that contains two entry arrays: the prependEntries and overlayEntries
|
|---|
| 11 | * @param {Object} optionsContainer This is the container for the options to this function
|
|---|
| 12 | * @param {import('../types').NormalizedPluginOptions} optionsContainer.options Configuration options for this plugin.
|
|---|
| 13 | * @param {import('webpack').Compiler["options"]["devServer"]} [optionsContainer.devServer] The webpack devServer config
|
|---|
| 14 | * @returns {AdditionalEntries} An object that contains the Webpack entries for prepending and the overlay feature
|
|---|
| 15 | */
|
|---|
| 16 | function getAdditionalEntries({ devServer, options }) {
|
|---|
| 17 | /** @type {Record<string, string | number>} */
|
|---|
| 18 | let resourceQuery = {};
|
|---|
| 19 |
|
|---|
| 20 | if (devServer) {
|
|---|
| 21 | const { client, https, http2, sockHost, sockPath, sockPort } = devServer;
|
|---|
| 22 | let { host, path, port } = devServer;
|
|---|
| 23 |
|
|---|
| 24 | let protocol = https || http2 ? 'https' : 'http';
|
|---|
| 25 | if (sockHost) host = sockHost;
|
|---|
| 26 | if (sockPath) path = sockPath;
|
|---|
| 27 | if (sockPort) port = sockPort;
|
|---|
| 28 |
|
|---|
| 29 | if (client && client.webSocketURL != null) {
|
|---|
| 30 | let parsedUrl = client.webSocketURL;
|
|---|
| 31 | if (typeof parsedUrl === 'string') parsedUrl = new URL(parsedUrl);
|
|---|
| 32 |
|
|---|
| 33 | let auth;
|
|---|
| 34 | if (parsedUrl.username) {
|
|---|
| 35 | auth = parsedUrl.username;
|
|---|
| 36 | if (parsedUrl.password) {
|
|---|
| 37 | auth += ':' + parsedUrl.password;
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | if (parsedUrl.hostname != null) {
|
|---|
| 42 | host = [auth != null && auth, parsedUrl.hostname].filter(Boolean).join('@');
|
|---|
| 43 | }
|
|---|
| 44 | if (parsedUrl.pathname != null) {
|
|---|
| 45 | path = parsedUrl.pathname;
|
|---|
| 46 | }
|
|---|
| 47 | if (parsedUrl.port != null) {
|
|---|
| 48 | port = !['0', 'auto'].includes(String(parsedUrl.port)) ? parsedUrl.port : undefined;
|
|---|
| 49 | }
|
|---|
| 50 | if (parsedUrl.protocol != null) {
|
|---|
| 51 | protocol = parsedUrl.protocol !== 'auto' ? parsedUrl.protocol.replace(':', '') : 'ws';
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (host) resourceQuery.sockHost = host;
|
|---|
| 56 | if (path) resourceQuery.sockPath = path;
|
|---|
| 57 | if (port) resourceQuery.sockPort = port;
|
|---|
| 58 | resourceQuery.sockProtocol = protocol;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | if (options.overlay) {
|
|---|
| 62 | const { sockHost, sockPath, sockPort, sockProtocol } = options.overlay;
|
|---|
| 63 | if (sockHost) resourceQuery.sockHost = sockHost;
|
|---|
| 64 | if (sockPath) resourceQuery.sockPath = sockPath;
|
|---|
| 65 | if (sockPort) resourceQuery.sockPort = sockPort;
|
|---|
| 66 | if (sockProtocol) resourceQuery.sockProtocol = sockProtocol;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // We don't need to URI encode the resourceQuery as it will be parsed by Webpack
|
|---|
| 70 | const queryString = querystring.stringify(resourceQuery, undefined, undefined, {
|
|---|
| 71 | /**
|
|---|
| 72 | * @param {string} string
|
|---|
| 73 | * @returns {string}
|
|---|
| 74 | */
|
|---|
| 75 | encodeURIComponent(string) {
|
|---|
| 76 | return string;
|
|---|
| 77 | },
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | const prependEntries = [
|
|---|
| 81 | // React-refresh runtime
|
|---|
| 82 | require.resolve('../../client/ReactRefreshEntry'),
|
|---|
| 83 | ];
|
|---|
| 84 |
|
|---|
| 85 | const overlayEntries = [
|
|---|
| 86 | // Error overlay runtime
|
|---|
| 87 | options.overlay &&
|
|---|
| 88 | options.overlay.entry &&
|
|---|
| 89 | `${require.resolve(options.overlay.entry)}${queryString ? `?${queryString}` : ''}`,
|
|---|
| 90 | ].filter(Boolean);
|
|---|
| 91 |
|
|---|
| 92 | return { prependEntries, overlayEntries };
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | module.exports = getAdditionalEntries;
|
|---|