question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not possible to extract MF dynamic loading code to util library

See original GitHub issue

In order to dynamically load remote modules I have created the following “importRemote” function:

import script from "scriptjs";

const loadEntryPoint = url =>
  new Promise(resolve => {
    script.urlArgs(`t=${new Date().getTime()}`);
    script(url, () => resolve());
  });

// Dynamically import a remote module using Webpack's loading mechanism:
// https://webpack.js.org/concepts/module-federation/
const importRemote = async (url, scope, module) => {
  if (!window[scope]) {
    //Get the remote entry point:
    await loadEntryPoint(`${url}/remoteEntry.js`);
    if (!window[scope]) {
      return Promise.reject(new Error(`${scope} could not be located!`));
    }
    // Initializes the share scope. This fills it with known provided modules from this build and all remotes
    await __webpack_init_sharing__("default");
    // Initialize the container, it may provide shared modules
    await window[scope].init(__webpack_share_scopes__.default);
  }

  const moduleFactory = await window[scope].get(
    module.startsWith("./") ? module : `./${module}`,
  );
  return moduleFactory();
};

export default importRemote;

I use this like React.lazy(()=>importRemote(url, scope, module))

This code is used in several projects so I want to extract the importRemote module to an external util library but I can’t because __webpack_init_sharing__ and __webpack_share_scopes__ are compilation scope variables from the project in which the module is located. If I move this code to a library and import it from there it doesn’t work because it seems that the scope variables are taken from the library’s build instead. Is there a way to achieve this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:38 (25 by maintainers)

github_iconTop GitHub Comments

2reactions
ScriptedAlchemycommented, Jan 13, 2022

I currently have all dynamic loading abstracted to a npm package. Build it with Babel or something standard. It should work

2reactions
ScriptedAlchemycommented, Dec 28, 2021

Make npm package a webpack build itself. Then it loads its own remote as needed if you don’t register the remote in your main build

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to load JAR files dynamically at Runtime? - Stack Overflow
It will dedicate a class loader for the jar to make sure there are no library conflicts. Users will be able to create...
Read more >
module-federation/nextjs-mf - npm
Ive added a util for dynamic chunk loading, in the event you need to load remote containers dynamically. InjectScript. import { injectScript } ......
Read more >
CEELOAD macro — Dynamically load a Language ... - IBM
CEELOAD is used to dynamically load a Language Environment-conforming routine. ... If MF=I or MF=L, you must specify either NAME or NAMEADDR, but...
Read more >
Chapter 3. Class Loading and Modules Red Hat JBoss ...
A dynamic module can declare dependencies in the deployment's MANIFEST.MF or jboss-deployment-structure.xml deployment descriptor. Implicit Dependencies.
Read more >
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
Just thought I'd share that it's also worth checking permissions on the class libraries your application is loading. Incorrect user permissions (for instance ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found