Code Monkey home page Code Monkey logo

server-side-module-federation-plugin's Introduction

Server Side Module Federation Plugin

This plugin for Webpack will allow for servers to federation portions of the code. It is useful for server side rendering, in particular.

Usage

  1. Install the plugin
yarn add -D server-side-module-federation-plugin
  1. Apply the plugin inside your webpack config for the SERVER, being sure to add some server side appropriate options
const ServerSideModuleFederationPlugin = require('server-side-module-federation-plugin');
module.exports = {
  entry: {}
  output: {
    libraryTarget: "commonjs-module",
    chunkLoading: "async-http-node",
    publicPath: 'http://some.cdn1.com/server/this-package/'
  },
  target: 'node',
  plugins: [
    new ServerSideModuleFederationPlugin({
      name: "nameOfYourBundle",
      library: {
        type: 'commonjs-module'
      },
      exposes: {
        './exposed1': './src/exposed1'
      }
      remotes: {
        remote1: 'http://some.cdn.com/server/remote1/remote-entry.js',
        remote2: 'http://some.othercdn.com/server/remote2/remote-entry.js',
      }
    });
  ]
}
  1. Make sure to have an equivalent webpack config for the CLIENT
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
module.exports = {
  entry: {}
  plugins: [
    new ModuleFederationPlugin({
      name: "nameOfYourBundle",
      exposes: {
        './exposed1': './src/exposed1'
      }
      remotes: {
        remote1: 'remote1@http://some.cdn.com/client/remote1/remote-entry.js',
        remote2: 'remote2@http://some.othercdn.com/client/remote2/remote-entry.js',
      }
    });
  ]
}
  1. Now use the exposed modules from inside "nameOfYourBundle" code
// App.js
import React, { lazy } from "react";
import Component1 from "remote1/Component1";
const Component2 = lazy(() => import("remote2/Component2"));

export default () => {
  return (
    <React.Suspense fallback="loading">
      <p>
        Alice: <Component1 /> is the best!
      </p>
      <p>
        Bob: no, <Component2 /> is the best!
      </p>
    </React>
  );
};
  1. You can use App.js inside a client bootstrapped code OR server bootstrapped code!

Trying out this repo

git clone https://github.com/kenotron/server-side-module-federation-plugin.git
cd server-side-module-federation-plugin
yarn
yarn start

In another terminal:

yarn workspace app1 test

server-side-module-federation-plugin's People

Contributors

kenotron avatar renovate-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.