0.0.33 • Published 8 months ago

@module-federation/esbuild v0.0.33

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

@module-federation/esbuild

This package provides an esbuild plugin for Module Federation, enabling you to easily share code between independently built and deployed applications.

Installation

Install the package using npm:

npm install @module-federation/esbuild

Usage

To use the Module Federation plugin with esbuild, add it to your esbuild configuration:

const esbuild = require('esbuild');
const path = require('path');
const { moduleFederationPlugin } = require('@module-federation/esbuild/plugin');
const federationConfig = require('./federation.config.js');

async function buildApp() {
  const tsConfig = 'tsconfig.json';
  const outputPath = path.join('dist', 'host');

  try {
    await esbuild.build({
      entryPoints: [path.join('host', 'main.ts')],
      outdir: outputPath,
      bundle: true,
      platform: 'browser',
      format: 'esm',
      mainFields: ['es2020', 'browser', 'module', 'main'],
      conditions: ['es2020', 'es2015', 'module'],
      resolveExtensions: ['.ts', '.tsx', '.mjs', '.js'],
      tsconfig: tsConfig,
      splitting: true,
      plugins: [moduleFederationPlugin(federationConfig)],
    });
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
}

buildApp();

// Example of federation.config.js

const { withFederation, shareAll } = require('@module-federation/esbuild/build');

module.exports = withFederation({
  name: 'host',
  filename: 'remoteEntry.js',
  exposes: {
    './Component': './src/Component',
  },
  shared: {
    react: {
      singleton: true,
      version: '^18.2.0',
    },
    'react-dom': {
      singleton: true,
      version: '^18.2.0',
    },
    rxjs: {
      singleton: true,
      version: '^7.8.1',
    },
    ...shareAll({
      singleton: true,
      strictVersion: true,
      requiredVersion: 'auto',
      includeSecondaries: false,
    }),
  },
});

The moduleFederationPlugin accepts a configuration object with the following properties:

  • name (string): The name of the host application.
  • filename (string, optional): The name of the remote entry file. Defaults to 'remoteEntry.js'.
  • remotes (object, optional): An object specifying the remote applications and their entry points.
  • exposes (object, optional): An object specifying the modules to be exposed by the host application.
  • shared (array, optional): An array of package names to be shared between the host and remote applications.

Plugin Features

The moduleFederationPlugin includes the following features:

  • Virtual Share Module: Creates a virtual module for sharing dependencies between the host and remote applications.
  • Virtual Remote Module: Creates a virtual module for importing exposed modules from remote applications.
  • CommonJS to ESM Transformation: Transforms CommonJS modules to ESM format for compatibility with Module Federation.
  • Shared Dependencies Linking: Links shared dependencies between the host and remote applications.
  • Manifest Generation: Generates a manifest file containing information about the exposed modules and their exports.

API

moduleFederationPlugin(config)

Creates an esbuild plugin for Module Federation.

  • config (object): The Module Federation configuration.
    • name (string): The name of the host application.
    • filename (string, optional): The name of the remote entry file. Defaults to 'remoteEntry.js'.
    • remotes (object, optional): An object specifying the remote applications and their entry points.
    • exposes (object, optional): An object specifying the modules to be exposed by the host application.
    • shared (array, optional): An array of package names to be shared between the host and remote applications.

Returns an esbuild plugin instance.

0.0.33

8 months ago

0.0.32

8 months ago

0.0.31

8 months ago

0.0.30

8 months ago

0.0.29

8 months ago

0.0.26

9 months ago

0.0.27

9 months ago

0.0.28

8 months ago

0.0.25

9 months ago

0.0.23

9 months ago

0.0.24

9 months ago

0.0.22

9 months ago

0.0.20

9 months ago

0.0.21

9 months ago

0.0.19

10 months ago

0.0.18

10 months ago

0.0.17

10 months ago

0.0.16

10 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago