Skip to content

Conversation

@ScriptedAlchemy
Copy link
Member

Description

Export getAllReferencedModules as a utility and not a class method

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

@changeset-bot
Copy link

changeset-bot bot commented Sep 9, 2024

⚠️ No Changeset found

Latest commit: 94ca9af

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This pull request refactors the getAllReferencedModules function by moving it from a class method to a standalone utility function. The primary goal of this change is to improve the reusability and maintainability of this functionality across the codebase.

The key changes include:

  • Extracting the getAllReferencedModules function from the HoistContainerReferencesPlugin class and making it a standalone utility function.
  • Updating the FederationRuntimePlugin class to use the new utility function instead of the previous class method implementation.

These changes will make the getAllReferencedModules functionality more easily accessible and usable in other parts of the codebase, enhancing the overall code organization and flexibility.

File Summaries
File Summary
packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts The code changes refactor the getAllReferencedModules function by moving it from a class method to a standalone utility function. This change allows the function to be reused more easily across different parts of the codebase.
packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts The changes in this file involve refactoring the getAllReferencedModules functionality into a utility function, making it more reusable and easier to maintain. The primary purpose of this change is to improve the overall code structure and organization within the FederationRuntimePlugin class.

@netlify
Copy link

netlify bot commented Sep 9, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 94ca9af
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/66df72c28b4c6b0008ea8bac
😎 Deploy Preview https://deploy-preview-2937--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 3

Configuration

Squadron Mode: essential

Commits Reviewed

760cc35c53dd9beb7ebba20b729108b2c61b0966...57409e5aa0e23a624e9635ffc858458790522f55

Files Reviewed
  • packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts

Comment on lines +260 to +292
// Helper method to collect all referenced modules recursively
export function getAllReferencedModules(
compilation: Compilation,
module: Module,
type?: 'all' | 'initial',
): Set<Module> {
const collectedModules = new Set<Module>([module]);
const stack = [module];

while (stack.length > 0) {
const currentModule = stack.pop();
if (!currentModule) continue;
const mgm = compilation.moduleGraph._getModuleGraphModule(currentModule);
if (mgm && mgm.outgoingConnections) {
for (const connection of mgm.outgoingConnections) {
if (type === 'initial') {
const parentBlock = compilation.moduleGraph.getParentBlock(
connection.dependency,
);
if (parentBlock instanceof AsyncDependenciesBlock) {
continue;
}
}
if (connection.module && !collectedModules.has(connection.module)) {
collectedModules.add(connection.module);
stack.push(connection.module);
}
}
}
}

return collectedModules;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getAllReferencedModules function has been extracted as a standalone utility function, which is a good refactoring. However, there are a few improvements that could be made:

  1. Consider adding type annotations for the compilation, module, and type parameters to improve readability and type safety.

  2. The function uses a depth-first search algorithm to collect modules. For large module graphs, this could potentially lead to stack overflow issues. Consider using an iterative approach instead of a recursive one.

  3. The function modifies the collectedModules set while iterating over it, which could lead to unexpected behavior. Consider using a separate set for newly discovered modules.

  4. The type parameter is only used for the 'initial' case. Consider renaming it to initialOnly or similar to make its purpose clearer.

Here's an example of how you could refactor this function:

Suggested change
// Helper method to collect all referenced modules recursively
export function getAllReferencedModules(
compilation: Compilation,
module: Module,
type?: 'all' | 'initial',
): Set<Module> {
const collectedModules = new Set<Module>([module]);
const stack = [module];
while (stack.length > 0) {
const currentModule = stack.pop();
if (!currentModule) continue;
const mgm = compilation.moduleGraph._getModuleGraphModule(currentModule);
if (mgm && mgm.outgoingConnections) {
for (const connection of mgm.outgoingConnections) {
if (type === 'initial') {
const parentBlock = compilation.moduleGraph.getParentBlock(
connection.dependency,
);
if (parentBlock instanceof AsyncDependenciesBlock) {
continue;
}
}
if (connection.module && !collectedModules.has(connection.module)) {
collectedModules.add(connection.module);
stack.push(connection.module);
}
}
}
}
return collectedModules;
}
export function getAllReferencedModules(
compilation: Compilation,
module: Module,
initialOnly: boolean = false
): Set<Module> {
const collectedModules = new Set<Module>([module]);
const stack: Module[] = [module];
while (stack.length > 0) {
const currentModule = stack.pop()!;
const mgm = compilation.moduleGraph._getModuleGraphModule(currentModule);
if (mgm && mgm.outgoingConnections) {
for (const connection of mgm.outgoingConnections) {
if (initialOnly) {
const parentBlock = compilation.moduleGraph.getParentBlock(connection.dependency);
if (parentBlock instanceof AsyncDependenciesBlock) {
continue;
}
}
if (connection.module && !collectedModules.has(connection.module)) {
collectedModules.add(connection.module);
stack.push(connection.module);
}
}
}
}
return collectedModules;
}

This refactored version uses an iterative approach, which is less likely to cause stack overflow issues. It also clarifies the purpose of the type parameter by renaming it to initialOnly.

Comment on lines 92 to 98
module instanceof NormalModule &&
module.resource === this.bundlerRuntimeDep
) {
const allRefs = this.getAllReferencedModules(
const allRefs = getAllReferencedModules(
compilation,
module,
'initial',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of getAllReferencedModules here looks correct. However, it's worth noting that the function call is incomplete in the provided snippet. Make sure the function call is completed correctly in the actual code. Also, consider adding a comment explaining why you're using 'initial' as the third argument, as it might not be immediately clear to other developers.

Comment on lines 192 to 198
return;
}

const allReferencedModules = this.getAllReferencedModules(
const allReferencedModules = getAllReferencedModules(
compilation,
runtimeModule,
'initial',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the usage of getAllReferencedModules here is correct. However, make sure the function call is completed properly in the actual code. It might be helpful to add a brief comment explaining the purpose of this call and why you're using 'initial' as the third argument.

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 1

Configuration

Squadron Mode: essential

Commits Reviewed

57409e5aa0e23a624e9635ffc858458790522f55...88aca7017071000f0546a218e9b20fb959d2c97c

Files Reviewed
  • packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts
  • packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Comment on lines 382 to 388
// hoist all modules of federation entry
this.getFilePath(),
this.bundlerRuntimePath,
this.options.experiments,
).apply(compiler);

new compiler.webpack.NormalModuleReplacementPlugin(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code snippet shows the usage of getAllReferencedModules, which is now likely a utility function. This change aligns with the commit message to export it as a utility. It's a good practice to modularize and separate concerns, making the code more maintainable and reusable.

If there are other parts of the codebase that used getAllReferencedModules as a class method, ensure they've been updated to use the new utility function to maintain consistency across the project.

@ScriptedAlchemy ScriptedAlchemy changed the title refactor: export getAllReferencedModules as a util refactor(enhanced): export getAllReferencedModules as a util Sep 9, 2024
@ScriptedAlchemy ScriptedAlchemy merged commit cf62587 into main Sep 9, 2024
@ScriptedAlchemy ScriptedAlchemy deleted the util-all-ref-mod branch September 9, 2024 22:21
@2heal1 2heal1 mentioned this pull request Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants