Skip to content

Commit 8b8da62

Browse files
authored
fix: replace require with lazyRequire to ensure proper initialization ordering in plugins (#4557)
1 parent 95282ac commit 8b8da62

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

.changeset/salty-doodles-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
---
4+
5+
fix(enhanced): replace require with lazyRequire to ensure proper initialization ordering in plugins

packages/enhanced/src/wrapper/BaseWrapperPlugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { WebpackPluginInstance, Compiler } from 'webpack';
22
import { getWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
33

4+
// Use module.require instead of require to prevent bundlers (rolldown/tsdown) from
5+
// hoisting the call to the top of the file. This ensures the require only executes
6+
// at call time, preserving strict initialization ordering. CJS-only.
7+
const lazyRequire = (id: string): any => module.require(id);
8+
49
/**
510
* Base Wrapper Plugin Class
611
*
@@ -33,7 +38,7 @@ export default abstract class BaseWrapperPlugin implements WebpackPluginInstance
3338
process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler);
3439

3540
// Lazily load core plugin after webpack path is set.
36-
const CorePlugin = require(this.coreModulePath).default as any;
41+
const CorePlugin = lazyRequire(this.coreModulePath).default as any;
3742

3843
// Create core plugin instance and apply it
3944
this.createCorePluginInstance(CorePlugin, compiler);

packages/enhanced/src/wrapper/FederationModulesPlugin.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { Compilation } from 'webpack';
22
import BaseWrapperPlugin from './BaseWrapperPlugin';
33

4+
// Use module.require instead of require to prevent bundlers (rolldown/tsdown) from
5+
// hoisting the call to the top of the file. This ensures the require only executes
6+
// at call time, preserving strict initialization ordering. CJS-only.
7+
const lazyRequire = (id: string): any => module.require(id);
8+
49
const PLUGIN_NAME = 'FederationModulesPlugin';
510

611
export default class FederationModulesPlugin extends BaseWrapperPlugin {
@@ -9,9 +14,10 @@ export default class FederationModulesPlugin extends BaseWrapperPlugin {
914
}
1015

1116
static getCompilationHooks(compilation: Compilation) {
12-
const CoreFederationModulesPlugin =
13-
require('../lib/container/runtime/FederationModulesPlugin')
14-
.default as typeof import('../lib/container/runtime/FederationModulesPlugin').default;
17+
const CoreFederationModulesPlugin = lazyRequire(
18+
'../lib/container/runtime/FederationModulesPlugin',
19+
)
20+
.default as typeof import('../lib/container/runtime/FederationModulesPlugin').default;
1521
return CoreFederationModulesPlugin.getCompilationHooks(compilation);
1622
}
1723

packages/enhanced/src/wrapper/ModuleFederationPlugin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ export default class ModuleFederationPlugin extends BaseWrapperPlugin {
103103
if (!enableBridgePlugin && hasBridgeReact) {
104104
compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', () => {
105105
try {
106-
const path = require('path');
107-
const fs = require('fs');
108106
const bridgeReactBasePath = path.resolve(
109107
compiler.context,
110108
'node_modules/@module-federation/bridge-react/dist/base.es.js',

0 commit comments

Comments
 (0)