4.0.20 • Published 4 months ago

@module-federation/storybook-addon v4.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Storybook addon for Module Federation

This addon enables to consume remote Module Federated apps/components

Install

# with NPM
npm install @module-federation/storybook-addon

# with Yarn
yarn add @module-federation/storybook-addon

Configuration

In file ./storybook/main.js:

const moduleFederationConfig = {
  // Module Federation config
};

const storybookConfig = {
  stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    // other addons,
    {
      name: '@module-federation/storybook-addon',
      options: {
        moduleFederationConfig,
      },
    },
  ],
  framework: '@storybook/react',
  core: {
    builder: '@storybook/builder-webpack5', // is required webpack 5 builder
  },
};

module.exports = storybookConfig;

Rsbuild App or Rslib Module

import { dirname, join } from 'node:path';
import type { StorybookConfig } from 'storybook-react-rsbuild';

function getAbsolutePath(value: string): any {
  return dirname(require.resolve(join(value, 'package.json')));
}

const config: StorybookConfig = {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  framework: {
    name: getAbsolutePath('storybook-react-rsbuild'),
    options: {},
  },
  addons: [
    {
      name: '@module-federation/storybook-addon/preset.js',
      options: {
        // add remote here and then you can load remote in your story
        remotes: {
          'rslib-module':
            'rslib-module@http://localhost:3000/mf/mf-manifest.json',
        },
      },
    },
  ],
};

export default config;

For the NX projects:

Replace NX utils withModuleFederation in webpack.config.js with our utils withModuleFederation. Example:

const { composePlugins, withNx } = require('@nx/webpack');
const { withReact } = require('@nx/react');
const { withModuleFederation } = require('@module-federation/storybook-addon');

const baseConfig = require('./module-federation.config');

const config = {
  ...baseConfig,
};

module.exports = composePlugins(withNx(), withReact(), withModuleFederation(config));

In file ./storybook/main.js:

const nxModuleFederationConfig = {
  // Module Federation config
};

const storybookConfig = {
  stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    // other addons,
    {
      name: '@module-federation/storybook-addon',
      options: {
        nxModuleFederationConfig,
      },
    },
  ],
  framework: '@storybook/react',
  core: {
    builder: '@storybook/builder-webpack5', // is required webpack 5 builder
  },
};

module.exports = storybookConfig;

Usage

import React, { Suspense } from 'react';

const LazyButton = React.lazy(() => import('remote/Button'));

const Button = (props) => (
  <Suspense fallback={<p>Please wait...</p>}>
    <LazyButton {...props} />
  </Suspense>
);

export default {
  title: 'ModuleFederation/Button',
  component: Button,
  argTypes: {
    variant: {
      control: 'select',
      options: ['primary', 'secondary'],
    },
  },
};

const Template = (args) => <Button {...args} />;

export const Primary = Template.bind({ variant: 'primary' });
Primary.args = {
  variant: 'primary',
  children: 'Button',
};

export const Secondary = Template.bind({});
Secondary.args = {
  variant: 'secondary',
  children: 'Button',
};
4.0.5

8 months ago

4.0.4

8 months ago

4.0.7

8 months ago

4.0.6

8 months ago

4.0.1

9 months ago

4.0.0

10 months ago

4.0.3

9 months ago

4.0.2

9 months ago

4.0.9

7 months ago

4.0.8

7 months ago

4.0.19

5 months ago

4.0.20

5 months ago

3.0.4

12 months ago

3.0.3

12 months ago

3.0.2

12 months ago

3.0.8

11 months ago

3.0.6

12 months ago

3.0.5

12 months ago

4.0.10

7 months ago

4.0.16

5 months ago

4.0.15

6 months ago

4.0.18

5 months ago

4.0.17

5 months ago

4.0.12

7 months ago

4.0.11

7 months ago

4.0.14

6 months ago

4.0.13

6 months ago

3.0.9

11 months ago

3.0.12

11 months ago

3.0.13

11 months ago

3.0.10

11 months ago

3.0.11

11 months ago

3.0.16

10 months ago

3.0.17

10 months ago

3.0.14

11 months ago

3.0.15

11 months ago

3.0.18

10 months ago

3.0.1

12 months ago

3.0.0

1 year ago

2.0.1

2 years ago

1.0.0

2 years ago

2.0.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago