Skip to content
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

WIP: fix(enhanced): use enhanced resolve for package resolution #3237

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/enhanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"enhanced-resolve": "^5.0.0"
},
"dependencies": {
"enhanced-resolve": "^5.17.1",
"@module-federation/sdk": "workspace:*",
"@module-federation/runtime-tools": "workspace:*",
"@module-federation/manifest": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import EmbedFederationRuntimePlugin from './EmbedFederationRuntimePlugin';
import FederationModulesPlugin from './FederationModulesPlugin';
import HoistContainerReferences from '../HoistContainerReferencesPlugin';
import FederationRuntimeDependency from './FederationRuntimeDependency';
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';

const ModuleDependency = require(
normalizeWebpackPath('webpack/lib/dependencies/ModuleDependency'),
Expand Down Expand Up @@ -58,6 +59,14 @@ const federationGlobal = getFederationGlobalScope(RuntimeGlobals);

const onceForCompler = new WeakSet<Compiler>();

// Create a resolver instance
const resolver = ResolverFactory.createResolver({
//@ts-ignore
fileSystem: new CachedInputFileSystem(fs, 4000),
mainFields: ['module', 'exports', 'main'],
conditionNames: ['import', 'require', 'node'],
});

class FederationRuntimePlugin {
options?: moduleFederationPlugin.ModuleFederationPluginOptions;
entryFilePath: string;
Expand Down Expand Up @@ -370,6 +379,22 @@ class FederationRuntimePlugin {
);
}

if (isHoisted) {
// Use the resolver to resolve the path synchronously
try {
const resolvedPath = resolver.resolveSync(
{},
runtimePath.replace('.cjs.js', '').replace('.esm.mjs', ''),
'',
);
if (resolvedPath) {
runtimePath = resolvedPath;
}
} catch (err) {
console.error('Error resolving runtime path:', err);
}
}

const alias: any = compiler.options.resolve.alias || {};
alias['@module-federation/runtime$'] =
alias['@module-federation/runtime$'] || runtimePath;
Expand Down Expand Up @@ -433,18 +458,36 @@ class FederationRuntimePlugin {
}

if (this.options?.experiments?.federationRuntime === 'hoisted') {
new EmbedFederationRuntimePlugin().apply(compiler);
// Use the sync resolver to resolve the path instead of string replacement
try {
const resolvedPath = resolver.resolveSync(
{},
RuntimePath.replace('.cjs.js', '').replace('.esm.mjs', ''),
'',
);
if (resolvedPath) {
this.bundlerRuntimePath = resolvedPath;
}
} catch (err) {
// Handle error if needed
}

new EmbedFederationRuntimePlugin().apply(compiler);
new HoistContainerReferences().apply(compiler);

new compiler.webpack.NormalModuleReplacementPlugin(
/@module-federation\/runtime/,
(resolveData) => {
if (/webpack-bundler-runtime/.test(resolveData.contextInfo.issuer)) {
resolveData.request = RuntimePath;

if (resolveData.createData) {
resolveData.createData.request = resolveData.request;
try {
const resolvedPath = resolver.resolveSync({}, RuntimePath, '');
if (resolvedPath) {
resolveData.request = resolvedPath;
if (resolveData.createData) {
resolveData.createData.request = resolvedPath;
}
}
} catch (err) {
// Handle error if needed
}
}
},
Expand Down
Loading
Loading