Socket
Book a DemoInstallSign in
Socket

module-federation-import-remote

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-federation-import-remote

Allow Dynamic Imports of Remotely Exposed Modules Using Webpack Module Federation

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
3.1K
-9.72%
Maintainers
1
Weekly downloads
 
Created
Source

Module Federation Import Remote

Allow a host application to dynamically import remotely exposed modules using Webpack Module Federation

Notes

This library exports an importRemote() function which will enable dynamic imports of remotely exposed modules using the Module Federation plugin. It uses the method described in the official Webpack configuration under Dynamic Remote Containers.

Update The source code of this library was reviewed by the creator of Module Federation and is now also added to the official utilities package @module-federation/utilities.

Example

  • Expose a module in the remote application's Webpack configuration:
  new ModuleFederationPlugin({
    name: "Foo",
    library: { type: "var", name: "Foo" },
    filename: "remoteEntry.js",
    exposes: {
      "./Bar": "./src/Bar",
    },
  })
  • Build the remote application and serve it so that remoteEntry.js is accessible for example in the following URL http://localhost:3001.

  • Load the remotely exposed module in the host application using importRemote() and use it:

  import { importRemote } from "module-federation-import-remote";

  // If it's a regular js module:
  importRemote({ url: "http://localhost:3001", scope: 'Foo', module: 'Bar' }).then(({/* list of Bar exports */}) => {
    // Use Bar exports
  });

  // If Bar is a React component you can use it with lazy and Suspense just like a dynamic import:
  const Bar = lazy(() => importRemote({ url: "http://localhost:3001", scope: 'Foo', module: 'Bar' }));

  return (
    <Suspense fallback={<div>Loading Bar...</div>}>
      <Bar />
    </Suspense>
  );

Additional Options

Apart from url, scope and module you can also pass additional options to the importRemote() function:

  • remoteEntryFileName: The name of the remote entry file. Defaults to "remoteEntry.js".
  • bustRemoteEntryCache: Whether to add a cache busting query parameter to the remote entry file URL. Defaults to true. You can disable it if cachebusting is handled by the server.

Keywords

module

FAQs

Package last updated on 22 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.