0.0.0-next-20240814104132 • Published 1 year ago

@module-federation/esbuild v0.0.0-next-20240814104132

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.43

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.37

1 year ago

0.0.38

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.36

1 year ago

0.0.70

8 months ago

0.0.71

8 months ago

0.0.62

10 months ago

0.0.63

9 months ago

0.0.64

9 months ago

0.0.65

9 months ago

0.0.66

9 months ago

0.0.67

8 months ago

0.0.68

8 months ago

0.0.69

8 months ago

0.0.60

10 months ago

0.0.61

10 months ago

0.0.59

10 months ago

0.0.51

1 year ago

0.0.52

1 year ago

0.0.53

11 months ago

0.0.54

11 months ago

0.0.55

11 months ago

0.0.56

11 months ago

0.0.57

11 months ago

0.0.58

11 months ago

0.0.50

1 year ago

0.0.48

1 year ago

0.0.49

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.25

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.22

1 year ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago