Skip to content

chore: cleanup old utils for Webpack 4 #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ class ReactRefreshPlugin {
} = webpack;

// Inject react-refresh context to all Webpack entry points.
// This should create `EntryDependency` objects when available,
// and fallback to patching the `entry` object for legacy workflows.
const addEntries = getAdditionalEntries(this.options);
const { overlayEntries, prependEntries } = getAdditionalEntries(this.options);
// Prepended entries does not care about injection order,
// so we can utilise EntryPlugin for simpler logic.
addEntries.prependEntries.forEach((entry) => {
for (const entry of prependEntries) {
new EntryPlugin(compiler.context, entry, { name: undefined }).apply(compiler);
});
}

const integrationEntry = getIntegrationEntry(this.options.overlay.sockIntegration);
const socketEntryData = [];
Expand All @@ -93,11 +91,11 @@ class ReactRefreshPlugin {
// Overlay entries need to be injected AFTER integration's entry,
// so we will loop through everything in `finishMake` instead of `make`.
// This ensures we can traverse all entry points and inject stuff with the correct order.
for (const [idx, entry] of addEntries.overlayEntries.entries()) {
for (const [idx, entry] of overlayEntries.entries()) {
compiler.hooks.finishMake.tapPromise(
{
name: this.constructor.name,
stage: Number.MIN_SAFE_INTEGER + (addEntries.overlayEntries.length - idx - 1),
stage: Number.MIN_SAFE_INTEGER + (overlayEntries.length - idx - 1),
},
(compilation) => {
// Only hook into the current compiler
Expand Down
96 changes: 0 additions & 96 deletions lib/utils/getRefreshGlobal.js

This file was deleted.

4 changes: 0 additions & 4 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const getAdditionalEntries = require('./getAdditionalEntries');
const getIntegrationEntry = require('./getIntegrationEntry');
const getRefreshGlobal = require('./getRefreshGlobal');
const getSocketIntegration = require('./getSocketIntegration');
const injectRefreshEntry = require('./injectRefreshEntry');
const injectRefreshLoader = require('./injectRefreshLoader');
const makeRefreshRuntimeModule = require('./makeRefreshRuntimeModule');
const normalizeOptions = require('./normalizeOptions');

module.exports = {
getAdditionalEntries,
getIntegrationEntry,
getRefreshGlobal,
getSocketIntegration,
injectRefreshEntry,
injectRefreshLoader,
makeRefreshRuntimeModule,
normalizeOptions,
Expand Down
98 changes: 0 additions & 98 deletions lib/utils/injectRefreshEntry.js

This file was deleted.

53 changes: 29 additions & 24 deletions lib/utils/makeRefreshRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ function makeRefreshRuntimeModule(webpack) {
*/
generate() {
const { runtimeTemplate } = this.compilation;
const declareVar = runtimeTemplate.supportsConst() ? 'const' : 'var';
const constDeclaration = runtimeTemplate.supportsConst() ? 'const' : 'var';
return webpack.Template.asString([
`${declareVar} setup = ${runtimeTemplate.basicFunction('moduleId', [
`${declareVar} refresh = {`,
`${constDeclaration} setup = ${runtimeTemplate.basicFunction('moduleId', [
`${constDeclaration} refresh = {`,
webpack.Template.indent([
`moduleId: moduleId,`,
`register: ${runtimeTemplate.basicFunction('type, id', [
`${declareVar} typeId = moduleId + " " + id;`,
`refresh.runtime.register(type, typeId);`,
`${constDeclaration} typeId = moduleId + ' ' + id;`,
'refresh.runtime.register(type, typeId);',
])},`,
`signature: ${runtimeTemplate.returningFunction(
'refresh.runtime.createSignatureFunctionForTransform()'
Expand All @@ -42,36 +42,41 @@ function makeRefreshRuntimeModule(webpack) {
]),
`};`,
`return refresh;`,
])}`,
])};`,
'',
`${webpack.RuntimeGlobals.interceptModuleExecution}.push(${runtimeTemplate.basicFunction(
'options',
[
`${declareVar} originalFactory = options.factory;`,
`${constDeclaration} originalFactory = options.factory;`,
`options.factory = ${runtimeTemplate.basicFunction(
['moduleObject', 'moduleExports', 'webpackRequire'],
'moduleObject, moduleExports, webpackRequire',
[
// Our require function delegates to the original require function
`${declareVar} hotRequire = ${runtimeTemplate.returningFunction(
`${constDeclaration} hotRequire = ${runtimeTemplate.returningFunction(
'webpackRequire(request)',
'request'
)};`,
// The propery descriptor factory below ensures that all properties but $Refresh$ are proxied through to the original require function
`${declareVar} createPropertyDescriptor = ${runtimeTemplate.basicFunction('name', [
`return {`,
webpack.Template.indent([
`configurable: true,`,
`enumerable: true,`,
`get: ${runtimeTemplate.returningFunction('webpackRequire[name]')},`,
`set: ${runtimeTemplate.basicFunction('value', [
'webpackRequire[name] = value;',
])},`,
]),
`};`,
])};`,
`for (${declareVar} name in webpackRequire) {`,
// The propery descriptor factory below
// ensures all properties but $Refresh$ are proxied through to the original require function
`${constDeclaration} createPropertyDescriptor = ${runtimeTemplate.basicFunction(
'name',
[
`return {`,
webpack.Template.indent([
`configurable: true,`,
`enumerable: true,`,
`get: ${runtimeTemplate.returningFunction('webpackRequire[name]')},`,
`set: ${runtimeTemplate.basicFunction('value', [
'webpackRequire[name] = value;',
])},`,
]),
`};`,
]
)};`,
`for (${constDeclaration} name in webpackRequire) {`,
webpack.Template.indent([
`if (Object.prototype.hasOwnProperty.call(webpackRequire, name) && name !== "$Refresh$") {`,
'if (name === "$Refresh$") continue;',
'if (Object.prototype.hasOwnProperty.call(webpackRequire, name)) {',
webpack.Template.indent([
`Object.defineProperty(hotRequire, name, createPropertyDescriptor(name));`,
]),
Expand Down
Loading