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

feat(runtime): add loadEntry hook #2826

Merged
merged 22 commits into from
Aug 15, 2024

Conversation

jbroma
Copy link
Contributor

@jbroma jbroma commented Aug 5, 2024

Description

  1. added loadEntry loader hook that handles loading entry in different environments
  2. added dom and node runtime plugins that use loadEntry hook

change suggested here: #2812 (comment)

Related Issue

#2811

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.

Copy link

changeset-bot bot commented Aug 5, 2024

🦋 Changeset detected

Latest commit: 8240346

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 35 packages
Name Type
@module-federation/runtime Minor
@module-federation/sdk Minor
@module-federation/devtools Minor
@module-federation/dts-plugin Minor
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/runtime-tools Minor
@module-federation/webpack-bundler-runtime Minor
@module-federation/enhanced Minor
@module-federation/esbuild Patch
@module-federation/managers Minor
@module-federation/manifest Minor
@module-federation/modern-js Minor
@module-federation/rspack Minor
@module-federation/utilities Patch
@module-federation/bridge-react-webpack-plugin Minor
3008-runtime-remote Patch
host Patch
host-v5 Patch
host-vue3 Patch
remote1 Patch
remote2 Patch
remote3 Patch
@module-federation/modernjs Patch
modernjs-ssr-dynamic-nested-remote Patch
modernjs-ssr-dynamic-remote-new-version Patch
modernjs-ssr-dynamic-remote Patch
modernjs-ssr-host Patch
modernjs-ssr-nested-remote Patch
modernjs-ssr-remote-new-version Patch
modernjs-ssr-remote Patch
@module-federation/third-party-dts-extractor Minor
@module-federation/bridge-react Minor
@module-federation/bridge-vue3 Minor
@module-federation/bridge-shared Minor

Not sure what this means? Click here to learn what changesets are.

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

Copy link

netlify bot commented Aug 5, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 8240346
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/66bc8c549ea8120008401f70
😎 Deploy Preview https://deploy-preview-2826--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.

@jbroma
Copy link
Contributor Author

jbroma commented Aug 7, 2024

@2heal1 looking forward to hearing your thoughts on this implementation.

I feel like with the current parameters of loadEntry it feels more like a hook for remoteHandler rather than a loader hook, WDYT?

@RussellCanfield
Copy link
Collaborator

@squadronai review

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 introduces a new loadEntry loader hook and implements runtime plugins for DOM and Node environments. The core changes include:

  1. Added the loadEntry loader hook to handle loading entries in different environments.
  2. Implemented dom and node runtime plugins that utilize the loadEntry hook.
  3. Introduced new functions for loading remote entries in DOM and Node.js environments, supporting various module types (ESM, SystemJS, script-based).
  4. Implemented preloading functionality for CSS and JavaScript assets.
  5. Improved error handling for remote entry initialization.

These changes aim to enhance the overall functionality and flexibility of the Module Federation runtime, allowing for better integration with different environments and module types.

File Summary
packages/runtime/src/core.ts The code introduces a new loadEntry loader hook and implements runtime plugins for DOM and Node environments. It sets up default options for the Federation Host, including plugins for snapshot and preload asset generation, and initializes various handlers for snapshots, shared modules, and remotes.
packages/runtime/src/plugins/dom/index.ts The code introduces a domPlugin function that returns a FederationRuntimePlugin. It implements a loadEntry method, which utilizes the loadEntryDom function to handle loading entries in a DOM environment, supporting different module types.
packages/runtime/src/plugins/dom/loadEntry.ts undefined
packages/runtime/src/plugins/node/index.ts The code introduces a nodePlugin function that returns a FederationRuntimePlugin. It implements a loadEntry method using the loadEntryNode function, passing necessary parameters including a createScriptHook callback.
packages/runtime/src/plugins/node/loadEntry.ts The code introduces two functions for loading remote entry scripts in a Node.js environment. 'loadEntryScript' handles the core logic of loading and validating remote entry exports, while 'loadEntryNode' serves as a wrapper function with a simplified interface.
packages/runtime/src/utils/load.ts The code introduces new functions for loading remote entries in different environments. It adds support for ESM, SystemJS, and script-based entry loading, with separate implementations for DOM and Node environments.
packages/runtime/src/utils/preload.ts The code implements preloading functionality for CSS and JavaScript assets. It creates link elements for CSS and script elements for JavaScript, with different attributes based on the useLinkPreload flag. The code also utilizes lifecycle hooks for creating these elements.
packages/runtime/src/module/index.ts The code implements error handling for remote entry initialization. It checks if the 'init' function exists in the remote entry exports and logs an error message if it's undefined. Then, it proceeds to initialize the remote entry with provided options.
packages/sdk/src/dom.ts The code introduces a DOM-specific implementation for loading remote entries. It creates a script element, sets its attributes, and applies a custom hook for script creation if provided.

Commits reviewed:

72bcb40fac84fd62762850cb7a277b2afddc8efa...dafedb2cee3abc765217def871874dc3529ca84b
72bcb40fac84fd62762850cb7a277b2afddc8efa...ab4bfbd66e0a4264050e3f4a7aeaed1617ebf303

ab4bfbd66e0a4264050e3f4a7aeaed1617ebf303...82403465599a230d1ccb9eeb941736824aa6758b
ab4bfbd66e0a4264050e3f4a7aeaed1617ebf303...82403465599a230d1ccb9eeb941736824aa6758b
ab4bfbd66e0a4264050e3f4a7aeaed1617ebf303...82403465599a230d1ccb9eeb941736824aa6758b
72bcb40fac84fd62762850cb7a277b2afddc8efa...82403465599a230d1ccb9eeb941736824aa6758b
72bcb40fac84fd62762850cb7a277b2afddc8efa...82403465599a230d1ccb9eeb941736824aa6758b
72bcb40fac84fd62762850cb7a277b2afddc8efa...82403465599a230d1ccb9eeb941736824aa6758b

Issues Reviewed:

Issue Result Reason
2811 The pull request addresses the issue of supporting Module Federation V2 in the React-Native environment, which is described in the provided issue information.

packages/runtime/src/core.ts Outdated Show resolved Hide resolved
packages/runtime/src/core.ts Outdated Show resolved Hide resolved
packages/runtime/src/plugins/dom/index.ts Outdated Show resolved Hide resolved
packages/runtime/src/plugins/dom/index.ts Outdated Show resolved Hide resolved
packages/runtime/src/plugins/dom/index.ts Outdated Show resolved Hide resolved
packages/sdk/src/dom.ts Show resolved Hide resolved
packages/sdk/src/dom.ts Show resolved Hide resolved
packages/sdk/src/node.ts Show resolved Hide resolved
packages/sdk/src/node.ts Show resolved Hide resolved
packages/sdk/src/node.ts Show resolved Hide resolved
packages/runtime/src/core.ts Outdated Show resolved Hide resolved
@2heal1
Copy link
Member

2heal1 commented Aug 8, 2024

@2heal1 looking forward to hearing your thoughts on this implementation.

I feel like with the current parameters of loadEntry it feels more like a hook for remoteHandler rather than a loader hook, WDYT?

Yes i agree , loadEntry hook looks like like more suitable be put in remoteHandler.hooks than loaderHook. WDYT @zhoushaw ?

packages/runtime/src/core.ts Outdated Show resolved Hide resolved
packages/runtime/src/core.ts Outdated Show resolved Hide resolved
packages/runtime/src/utils/load.ts Outdated Show resolved Hide resolved
@2heal1
Copy link
Member

2heal1 commented Aug 8, 2024

thanks your contribution , i have finished review . And it needs something to modify

packages/runtime/src/core.ts Outdated Show resolved Hide resolved
@jbroma
Copy link
Contributor Author

jbroma commented Aug 9, 2024

added fixes after review, skipped most of squadronAI stuff since it didn't seem useful - if you want me to address some of these automated checks then I can do this no problem

@2heal1
Copy link
Member

2heal1 commented Aug 13, 2024

Here is still a question, don't forget to handle ~
#2826 (comment)

@jbroma
Copy link
Contributor Author

jbroma commented Aug 13, 2024

I've resolved the issue in question, please check whether its ok, thanks!

@2heal1
Copy link
Member

2heal1 commented Aug 14, 2024

Yes , no big issues , just still a small issues need to handle .

And you need to merge/rebase newest branch , and fix the CI

@jbroma jbroma changed the title [WIP] feat: loadEntry loader hook feat: loadEntry loader hook Aug 14, 2024
@jbroma jbroma changed the title feat: loadEntry loader hook feat(runtime): add loadEntry hook Aug 14, 2024
@jbroma
Copy link
Contributor Author

jbroma commented Aug 14, 2024

I've addressed the issues you've mentioned and added changeset, let me know if there is anything else to be done here 🫡

@2heal1 2heal1 merged commit 8378a77 into module-federation:main Aug 15, 2024
13 checks passed
@2heal1
Copy link
Member

2heal1 commented Aug 15, 2024

Thanks for your contribution ! Merged!

@zhangHongEn
Copy link
Contributor

Is there a demo of loaderEntry hook?
For example, how do I use loadEntry to load a json file?

@jbroma
Copy link
Contributor Author

jbroma commented Sep 4, 2024

@zhangHongEn you can take a look at PR here that utilises it:

@zhangHongEn
Copy link
Contributor

zhangHongEn commented Sep 4, 2024

Thanks, awesome, that's what I was looking for. #2905

@zhangHongEn
Copy link
Contributor

@jbroma
If I don't return a value in loadEntry, should the original behavior be maintained?
image

@zhangHongEn
Copy link
Contributor

You can review here

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.

4 participants