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

Refactoring and Enhancements of Node Federation Plugin and Filesystem Strategy, Including Addition of New Features and Tests #1312

Merged
merged 7 commits into from
Sep 7, 2023

Conversation

ScriptedAlchemy
Copy link
Member

Refactoring and Enhancements of Node Federation Plugin and Filesystem Strategy, Including Addition of New Features and Tests

The series of commits primarily focus on significant refactoring and improvements of the Node Federation Plugin and the filesystem strategy. The changes include enhancements to remote parsing, improved global object proxy handling, changes to module handling, and updates to the webpack configuration and plugins. In addition, new features like Dynamic Filesystem and Native Self Forming Node Federation have been introduced. Unit tests were added or updated to ensure the stability of the refactored code. Several enhancements were made to the handling of debugging including the addition or removal of console log statements.

Commits

  • refactor: Adjustments to 'default-delegate.ts' and 'NextFederationPlugin/index.ts' files, and addition to 'RemotePublicPathRuntimeModule.js'

    • Included refactoring changes and additions to three files
    • Focused on debug conditions, license comments, and removal of extra space and commented code
  • refactor: various improvements and fixes for node federation plugin

    • Multiple enhancements and bug fixes for the node federation plugin
    • Included error handling improvements, adjustments on plugin configuration, and removal of certain redundant log statements
  • refactor: enhanced global object proxy handling

    • Significant enhancements made to the handling of the global object proxy
    • Adjustments made in managing properties of the global object proxy including debugging logs
  • refactor: update URL constructor and remote registries, add error handling, refactor httpEvalStrategy and httpVmStrategy

    • Refactored strategies used for handling file loading in the context of module federation
    • Introduced an updated way of constructing URLs and added respective error handling
  • refactor: improvements to the node webpack configuration and plugins

    • Multiple refactorings across the webpack configuration and plugins
    • Modified the stage of the FederationModuleInfoRuntimeModule and added AutoPublicPathRuntimeModule
  • refactor: simplify and clean up code in pure.ts

    • Simplified and cleaned up the code in pure.ts
    • Removed unnecessary type assertions and tidied up the globalScope handling
  • refactor: update remote parsing in NodeFederationPlugin

    • Refactored the 'parseRemotes' and 'parseRemoteSyntax' functions to improve handling of remotes
    • Added validation for throwing error when remote is not a string
  • refactor: Improve remote parsing and test impact

    • Changes related to the parsing of remotes in NodeFederationPlugin.ts file
    • Included a new jest test to test remote parsing functions
  • chore: cleanup logs and things, Merge branch 'main' into node-sync-bytedance

    • Cleaned up logs and merged branch 'main' into 'node-sync-bytedance'
  • feat: native self forming node federation (feat: native self forming node federation  #1291)

    • Introduced Dynamic Filesystem and Native Self Forming Node Federation features
    • Made adjustments to FileSystem strategy
  • refactor: updated filesystem strategy and added new feature

    • Refactored the current filesystem run strategy and created a new feature called DynamicFilesystemChunkLoading
    • Added a new utility function in LoadFileChunkLoadingRuntimeModule
  • refactor: Update constants.js

    • Updated the model and increased the character maximum limit in constants.js
  • refactor: update modules and enhance functions

    • Series of refactor operations in different plugin files and LoadFileChunkLoadingRuntimeModule.ts
    • Updated class constructor functions, added new functions and refactored existing ones
  • refactor: Refactor on chunk load handling and chunk install generation in LoadFileChunkLoadingRuntimeModule

    • Introduced new function for handling chunk load and refactored the generation of the chunk installation code
  • refactor: Improved chunk loading logic in LoadFileChunkLoadingRuntimeModule

    • Refactored the chunk loading logic to improve readability
    • Included new function to handle server-side loading scenarios

ScriptedAlchemy and others added 5 commits September 5, 2023 20:37
In this commit, a few significant enhancements have been made to the handling of the global object proxy. Adjustments are made in the handling of properties 'remotes', 'cache', and '_config' - now situated under the '__remote_scope__' property of the global object. Additionally, console log statements have been added to aid in debugging.

### DynamicFilesystemChunkLoadingRuntimeModule.ts

- refactored the structure of the 'remotes' object

- changed federation remotes population method

- added a console log statement for debugging

### FederationModuleInfoRuntimeModule.ts

- created a new ES6 Proxy on '__remote_scope__' to manage 'remotes' and 'cache'

- augmented logging to trace the reading and writing operations on properties

### loadScript.ts

- enhanced error handling in 'executeLoad' function

- adjusted assignment of '_config' and 'remotes' properties in global object

- added debugging console log statements
This commit includes several changes aimed at improving the handling of remotes. The console.log statements have been reduced to minimize console clutter. The logic for checking remotes and their global scope has been enhanced to ensure safer and more precise handling. There's also a change in remote pathname rendering strategy. Other changes mainly involve syntax improvements, commenting updates or cleanups.

### packages/nextjs-mf/src/default-delegate.ts

- Added console.log statement to assist in debugging, particularly for resolving remote_global

### packages/node/src/plugins/FederationModuleInfoRuntimeModule.ts

- Cleaned up redundant console.log statements to rank up code clarity and performance

### packages/node/src/plugins/NodeFederationPlugin.ts

- Minor change of a comment

### packages/node/src/plugins/RemotePublicPathRuntimeModule.js

- Refactored the logic of determining the scriptUrl. Instead of a series of complex condition checks, the __dirname, which is more reliable, is used instead

### packages/node/src/plugins/loadScript.ts

- Changed remote loading method and final callback, which is now targeting remote[name] rather than cache[name]. Removed a console.log statement as well which is no longer needed

### packages/node/src/plugins/parts.ts

- Modified the fetching of the requested remote. Instead of using the .federation.remotes approach, it directly checks from the __remote_scope__
This commit includes several enhancements and bug fixes for the node federation plugin. These changes encompass error handling improvements, adjustments on plugin configuration, and removal of certain redundant log statements. The script loading process has also been revamped to support a broader range of environments.

### apps/node-host/src/bootstrap.js

- added console logs for debugging

### apps/node-host/webpack.config.js

- turned off caching

- turned off devtools

- set publicPath to auto

### apps/node-remote/webpack.config.js

- turned off devtools

- set caching to false

- allowed writeToDisk

### package.json

- added additional scripts for nx run-many

### packages/nextjs-mf/src/default-delegate.ts

- cleaned up loadScript function

### packages/nextjs-mf/src/plugins/NextFederationPlugin/index.ts

- uncommented optional debug configuration

### packages/node/src/filesystem/stratagies.js

- improved error handling for constructing relative chunk paths

### packages/node/src/plugins/DynamicFilesystemChunkLoadingRuntimeModule.ts

- removed debugging console log

- adjusted property reference from remotes to _config in the global scope proxy

### packages/node/src/plugins/FederationModuleInfoRuntimeModule.ts

- removed debugging console logs

- refactored how the global scope proxy is created

### packages/node/src/plugins/NodeFederationPlugin.ts

- adjusted parsing of remotes

### packages/node/src/plugins/RemotePublicPathRuntimeModule.js

- updated caching reference from remotes to _config

### packages/node/src/plugins/loadScript.ts

- added explanation comments

- enhanced console logs for debugging

- added property reference adjustment from remoteName to _config
…gin/index.ts' files, and addition to 'RemotePublicPathRuntimeModule.js'

This commit includes refactoring changes and additions to three files. For 'default-delegate.ts', new code was added to several functions and log statements, while some white space and commented code was removed in multiple locations. In 'NextFederationPlugin/index.ts', debug condition is un-commented, and in 'RemotePublicPathRuntimeModule.js', a license comment was added.

### packages/nextjs-mf/src/default-delegate.ts

- Added new code to 'loadScript' and 'getRuntimeRemotes' functions

- Removed extra white space and commented code

### packages/nextjs-mf/src/plugins/NextFederationPlugin/index.ts

- Un-commented the debug condition

### packages/node/src/plugins/RemotePublicPathRuntimeModule.js

- Added the MIT License comment
…mprove_auto_public_path

# Conflicts:
#	packages/node/src/plugins/FederationModuleInfoRuntimeModule.ts
#	packages/node/src/plugins/RemotePublicPathRuntimeModule.js
#	packages/node/src/plugins/loadScript.ts
// `if (!globalThis.__remote_scopes__) {`,
Template.indent([
'// backward compatible global proxy',
`globalThis.__remote_scope__ = new Proxy(globalThis.__remote_scope__ || __webpack_require__.federation, {`,
Copy link
Member Author

Choose a reason for hiding this comment

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

The globalThis.remote_scope proxy in FederationModuleInfoRuntimeModule.ts is used to provide backward compatibility for global proxy. It's a JavaScript Proxy object that intercepts and defines custom behavior for fundamental operations (e.g., property lookup, assignment, enumeration, function invocation, etc.) on the globalThis.remote_scope object.

Here's what it does:

  • It's created if globalThis.remote_scope doesn't already exist.
  • It proxies the federation object of RuntimeGlobals.require.
  • It has two handler functions: get and set.

The get function:

  • If the property being accessed is config, it returns the remotes object from the federation object.
  • For any other property, it returns the corresponding property from the cache object of the federation object.

The set function:

  • If the property being set is _config, it assigns the value to the remotes object of the federation object.
  • For any other property, it assigns the value to the corresponding property in the cache object of the federation object.

This proxy allows the federation object to be accessed and manipulated via globalThis.remote_scope, providing a consistent interface for managing federation module information.

Refer to the code here:

@@ -5,7 +5,9 @@

import express from 'express';
import * as path from 'path';
import('node_remote/test');
import('node_remote/test').then((m) => {
console.log(__webpack_require__.federation)
Copy link
Member Author

Choose a reason for hiding this comment

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

should return module info as well as any data pushed into remote_scope over proxy.

Proxy lets us mix global and local metadata about the system over a single local interface. require.federation

…nction

This commit enhances the readability of the httpVmStrategy by providing comprehensive inline documentation. The comments explain the purpose of the function, its parameters, and what it does. This will aid future developers in understanding the purpose and usage of the httpVmStrategy function.

### packages/node/src/filesystem/stratagies.js

- Added comprehensive inline documentation for the httpVmStrategy function
@@ -171,7 +171,7 @@ class DynamicFilesystemChunkLoadingRuntimeModule extends RuntimeModule {
}, {} as Record<string, string>)
)};`,
`Object.keys(remotes).forEach(function(remote) {
${RuntimeGlobals.require}.federation.remotes[remote] = remotes[remote];
globalThis.__remote_scope__._config[remote] = remotes[remote];
Copy link
Member Author

Choose a reason for hiding this comment

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

Write any known remotes into global proxy at startup

`var requestedRemote = ${
RuntimeGlobals.require
}.federation.remotes[${JSON.stringify(name)}]`,
`var requestedRemote = globalThis.__remote_scope__[${JSON.stringify(name)}]`,
Copy link
Member Author

Choose a reason for hiding this comment

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

local federation instance doesnt sync global remotes, this is a little backwards and needs refactoring. Relying on backward compatiable convention to read and write globally to hosts federation scope

@ScriptedAlchemy ScriptedAlchemy merged commit 9611138 into node_auto_public_path Sep 7, 2023
3 of 4 checks passed
@ScriptedAlchemy ScriptedAlchemy deleted the improve_auto_public_path branch September 7, 2023 01:09
@github-actions
Copy link
Contributor

🎉 This PR is included in version 8.1.0-canary.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

github-actions bot commented Nov 6, 2023

🎉 This PR is included in version 2.1.0-canary.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant