Skip to content

Commit fcfe540

Browse files
committed
chore: code cleanup
1 parent 3cce460 commit fcfe540

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

lib/index.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const {
99
const ConstDependency = require('webpack/lib/dependencies/ConstDependency');
1010
const { refreshGlobal, webpackRequire, webpackVersion } = require('./globals');
1111
const {
12+
getAdditionalEntries,
13+
getIntegrationEntry,
1214
getParserHelpers,
1315
getRefreshGlobal,
1416
getSocketIntegration,
1517
injectRefreshEntry,
1618
injectRefreshLoader,
1719
normalizeOptions,
18-
getAdditionalEntries,
1920
} = require('./utils');
2021
const schema = require('./options.json');
2122

@@ -33,27 +34,6 @@ const REPLACEMENTS = {
3334
},
3435
};
3536

36-
/**
37-
* Gets the socket integration to use for Webpack messages.
38-
* @param {'wds' | 'whm' | 'wps' | string} integrationType A valid socket integration type or a path to a module.
39-
* @returns {string | undefined} Path to the resolved socket integration module.
40-
*/
41-
function getSocketIntegrationEntry(integrationType) {
42-
let resolvedEntry;
43-
switch (integrationType) {
44-
case 'whm': {
45-
resolvedEntry = 'webpack-hot-middleware/client';
46-
break;
47-
}
48-
case 'wps': {
49-
resolvedEntry = 'webpack-plugin-serve/client';
50-
break;
51-
}
52-
}
53-
54-
return resolvedEntry;
55-
}
56-
5737
class ReactRefreshPlugin {
5838
/**
5939
* @param {import('./types').ReactRefreshPluginOptions} [options] Options for react-refresh-plugin.
@@ -107,20 +87,19 @@ class ReactRefreshPlugin {
10787
options: this.options,
10888
});
10989
if (EntryPlugin) {
90+
// Prepended entries does not care about injection order,
91+
// so we can utilise EntryPlugin for simpler logic.
11092
additional.prependEntries.forEach((entry) => {
11193
new EntryPlugin(compiler.context, entry, { name: undefined }).apply(compiler);
11294
});
11395

96+
const integrationEntry = getIntegrationEntry(this.options.overlay.sockIntegration);
11497
const socketEntryData = [];
11598
compiler.hooks.make.tap(
11699
{ name: this.constructor.name, stage: Number.POSITIVE_INFINITY },
117100
(compilation) => {
118-
const integrationEntry = getSocketIntegrationEntry(this.options.overlay.sockIntegration);
119-
120101
// Exhaustively search all entries for `integrationEntry`.
121-
// If found, inject `overlayEntries` to those entries,
122-
// and ensure the order of dependencies either here or in `seal`.
123-
// Else, inject `overlayEntries` to global entries.
102+
// If found, mark those entries and the index of `integrationEntry`.
124103
for (const [name, entryData] of compilation.entries.entries()) {
125104
const index = entryData.dependencies.findIndex((dep) =>
126105
dep.request.includes(integrationEntry)
@@ -132,6 +111,9 @@ class ReactRefreshPlugin {
132111
}
133112
);
134113

114+
// Overlay entries need to be injected AFTER integration's entry,
115+
// so we will loop through everything in `finishMake` instead of `make`.
116+
// This ensures we can traverse all entry points and inject stuff with the correct order.
135117
additional.overlayEntries.forEach((entry, idx, arr) => {
136118
compiler.hooks.finishMake.tapPromise(
137119
{ name: this.constructor.name, stage: Number.MIN_SAFE_INTEGER + (arr.length - idx - 1) },
@@ -150,6 +132,10 @@ class ReactRefreshPlugin {
150132
compilation.addEntry(compiler.context, dep, options, (err) => {
151133
if (err) return reject(err);
152134

135+
// If the entry is not a global one,
136+
// and we have registered the index for integration entry,
137+
// we will reorder all entry dependencies to our desired order.
138+
// That is, to have additional entries DIRECTLY behind integration entry.
153139
if (name && typeof index !== 'undefined') {
154140
const entryData = compilation.entries.get(name);
155141
entryData.dependencies.splice(

lib/utils/getIntegrationEntry.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Gets entry point of a supported socket integration.
3+
* @param {'wds' | 'whm' | 'wps' | string} integrationType A valid socket integration type or a path to a module.
4+
* @returns {string | undefined} Path to the resolved integration entry point.
5+
*/
6+
function getIntegrationEntry(integrationType) {
7+
let resolvedEntry;
8+
switch (integrationType) {
9+
case 'whm': {
10+
resolvedEntry = 'webpack-hot-middleware/client';
11+
break;
12+
}
13+
case 'wps': {
14+
resolvedEntry = 'webpack-plugin-serve/client';
15+
break;
16+
}
17+
}
18+
19+
return resolvedEntry;
20+
}
21+
22+
module.exports = getIntegrationEntry;

lib/utils/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
const getAdditionalEntries = require('./getAdditionalEntries');
2+
const getIntegrationEntry = require('./getIntegrationEntry');
13
const getParserHelpers = require('./getParserHelpers');
24
const getRefreshGlobal = require('./getRefreshGlobal');
35
const getSocketIntegration = require('./getSocketIntegration');
46
const injectRefreshEntry = require('./injectRefreshEntry');
57
const injectRefreshLoader = require('./injectRefreshLoader');
68
const normalizeOptions = require('./normalizeOptions');
7-
const getAdditionalEntries = require('./getAdditionalEntries');
89

910
module.exports = {
11+
getAdditionalEntries,
12+
getIntegrationEntry,
1013
getParserHelpers,
1114
getRefreshGlobal,
1215
getSocketIntegration,
1316
injectRefreshEntry,
1417
injectRefreshLoader,
1518
normalizeOptions,
16-
getAdditionalEntries,
1719
};

0 commit comments

Comments
 (0)