All Projects → xolvio → storybook-webpack-federation-plugin

xolvio / storybook-webpack-federation-plugin

Licence: MIT license
Exposes all the components in your Storybook as Webpack 5 federated components.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to storybook-webpack-federation-plugin

collaborated
Social chat and productivity app primarily for developers (and maybe gamers)
Stars: ✭ 12 (-77.78%)
Mutual labels:  storybook
orfium-ictinus
This repo will include the building blocks to create the Orfium Parthenons to come ...
Stars: ✭ 20 (-62.96%)
Mutual labels:  storybook
twitter-web-react
twitter-web-react.now.sh
Stars: ✭ 104 (+92.59%)
Mutual labels:  storybook
react-uswds
USWDS 3.0 components built in React
Stars: ✭ 108 (+100%)
Mutual labels:  storybook
app-starter
Angular mono-repo (Ionic/Capacitor/StencilJS/Web Component) app starter for supporting cross platform apps.
Stars: ✭ 75 (+38.89%)
Mutual labels:  storybook
storybook-addon-next
A no config Storybook addon that makes Next.js features just work in Storybook
Stars: ✭ 184 (+240.74%)
Mutual labels:  storybook
ember-cli-storybook
📒 Ember storybook adapter
Stars: ✭ 35 (-35.19%)
Mutual labels:  storybook
storybook-talk
Storybook, the playground you need for your UI components! 🎨
Stars: ✭ 11 (-79.63%)
Mutual labels:  storybook
gatsby-typescript-emotion-storybook
Gatsby Starter: TypeScript + Emotion + Storybook + React Intl + SVGR + Jest
Stars: ✭ 63 (+16.67%)
Mutual labels:  storybook
storybook-addon-mock
This addon allows you to mock fetch or XMLHttpRequest in the storybook.
Stars: ✭ 67 (+24.07%)
Mutual labels:  storybook
chicio.github.io
👻 Fabrizio Duroni (me 😄) personal website. Created using GatsbyJS, Styled Components, Storybook, Typescript, tsParticles, GitHub pages, Github Actions, Upptime.
Stars: ✭ 20 (-62.96%)
Mutual labels:  storybook
storybook-addon-xd-designs
For adobe XD. Demo at https://morgs32.github.io/storybook-addon-xd-designs
Stars: ✭ 41 (-24.07%)
Mutual labels:  storybook
react-library-starter
A library starter kit and bundler for your React projects, powered by Rollup. ⚡
Stars: ✭ 22 (-59.26%)
Mutual labels:  storybook
react-ui-kit-boilerplate
A minimal React UI Kit boilerplate with Storybook, hot reloading, Styled Components, Typescript and Jest
Stars: ✭ 88 (+62.96%)
Mutual labels:  storybook
astraea
TypeScript + React Boilerplate
Stars: ✭ 15 (-72.22%)
Mutual labels:  storybook
wongames
🎮 Ecommerce de jogos no estilo Steam. Desenvolvido com Next.js, TypeScript, GraphQL, etc.
Stars: ✭ 18 (-66.67%)
Mutual labels:  storybook
bs-storybook
Storybook bindings for Rescript
Stars: ✭ 76 (+40.74%)
Mutual labels:  storybook
react-imageViewer
React component for image displaying in full screen
Stars: ✭ 61 (+12.96%)
Mutual labels:  storybook
chanyeong
👨‍💻 chanyeong's portfolio and blog webpage
Stars: ✭ 39 (-27.78%)
Mutual labels:  storybook
gatsby-starter-bulma-storybook
A minimal Gatsby starter which includes Bulma and Storybook
Stars: ✭ 16 (-70.37%)
Mutual labels:  storybook

Storybook Webpack Federation Plugin

Exposes all the components in your Storybook as Webpack 5 federated components.

Cover image

Motivation

Design systems are all the fad these days, and we wanted to create an easy way to share them. Since Storybook has proven to be a great way do that, we figured why not also make the source of truth for the current state of components also be the place where you use them?

Check out the article we wrote about it here.. And you can follow along in the the example repo here.

Installation

For your Storybook

Install the plugin.

yarn add storybook-webpack-federation-plugin -D

Storybook currently uses Webpack 4, which means we have to do a few extra steps to install Webpack 5 as that's where federation has been added. Once Storybook starts using Webpack 5 we won't need to do these steps.

First we need to install the latest Beta of Webpack5:

yarn add [email protected] webpack-cli -D

Storybook has its own webpack configuration that you can normally extend, but we can't do that yet so we have to create a new webpack.config.js specific for WP5. Here's an example configuration which you might want to customize based on your setup.

const path = require("path");

module.exports = {
  cache: false,

  mode: "development",
  devtool: "source-map",

  optimization: {
    minimize: false,
  },

  resolve: {
    extensions: [".jsx", ".js", ".json", ".tsx", ".ts"],
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: require.resolve("babel-loader"),
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
    ],
  },

  output: {},

  plugins: [],
};

Next we'll import the Storybook Webpack Federation Plugin:

const {
  StorybookWebpackFederationPlugin,
} = require("storybook-webpack-federation-plugin");

Add a new endpoint as an output of Storybook:

  output: {
    // location of where the compiled Storybook lives
    path: path.resolve(__dirname, "storybook-static/federation"),
    // the url where Storybook will be accessible from
    publicPath: "//localhost:3030/federation/",
  },

And finally configure the plugin itself in the plugins section:

plugins: [
    new StorybookWebpackFederationPlugin({
      name: "xolvio_ui", // this will be used by the consuming federation host
      files: { // paths to the components
        paths: ["./src/**/*.ts{,x}"],
      },
    }),
  ],

For convenience you'll probably want to set npm scripts for building your storybook and federation like this:

"scripts": {
    "start": "start-storybook -p 9009 -s public,assets",
    "build": "yarn build:storybook && yarn build:federation",
    "build:federation": "rm -rf storybook-static/federation && webpack --mode production",
    "serve": "http-server ./storybook-static -p 3030 --cors",
    "build:storybook": "build-storybook -s public,assets"
}

Let's now build and serve it:

yarn build && yarn serve

And that's all for the Storybook side!

For your app

Install the plugin:

yarn add storybook-webpack-federation-plugin -D

We need to make sure we are using the beta version of Webpack 5 here as well:

yarn add [email protected] -D

Go to your webpack.config.js and require the plugin at the top as before:

const {
  StorybookWebpackFederationPlugin,
} = require("storybook-webpack-federation-plugin");

Use it in the plugins section:

  plugins: [
    new StorybookWebpackFederationPlugin({
      remotes: ["xolvio_ui"],
    }),
  ],

Let's add the Storybook endpoint that we exposed above in the app's index.html:

<head>
  <script src="http://localhost:3030/federation/remoteEntry.js"></script>
</head>

And now you can start using components from your published Storybook!

import { Background } from "xolvio_ui/elements/Background";
import { Title } from "xolvio_ui/components/Title";
import { CenteredContentWrapper } from "xolvio_ui/helpers/CenteredContentWrapper";

export const Services = () => (
  <CenteredContentWrapper>
    <Background/>
    <Title title="hello" subheading="world" />
  </CenteredContentWrapper>
);

You can also use lazy loading:

const CenteredContentWrapper = React.lazy(() =>
  import("xolvio_ui/CenteredContentWrapper")
);
const Background = React.lazy(() => import("xolvio_ui/Background"));
const Title = React.lazy(() => import("xolvio_ui/Title"));

export const Services = () => (
  <React.Suspense fallback={"Loading Components from the Design System"}>
    <CenteredContentWrapper>
      <Background />
      <Title title="hello" subheading="world" />
    </CenteredContentWrapper>
  </React.Suspense>
);

And that's all there is to it! Enjoy :)

We'll come back and update the docs to make this even easier once Storybook is using Webpack 5.

Do I need this plugin?

We wanted to have the configuration focus on the essentials by using smart defaults and to use globs for exposing multiple modules instead of listing them one by one, which is error prone and boring.

So this is how you would do it if you wanted to use Webpack Federation without our plugin:

new ModuleFederationPlugin({
  name: "xolvio_ui",
  library: { type: "var", name: "xolvio_ui" },
  filename: "remoteEntry.js",
  exposes: {
    CenteredContentWrapper: "./src/helpers/CenteredContentWrapper.tsx",
    Title: "./src/components/Title.tsx",
    Background: "./src/elements/Background.tsx",
    Sections: "./src/components/Sections.tsx",
    ScreenIcon: "./src/components/icons/ScreenIcon.tsx",
    FlipchartIcon: "./src/components/icons/FlipchartIcon.tsx",
    ShapesIcon: "./src/components/icons/ShapesIcon.tsx",
    // (..)
  },
  shared: ["react", "react-dom"],
})

And here's with our plugin:

new StorybookWebpackFederationPlugin({
  name: "xolvio_ui",
  files: {
    paths: ["./src/**/*.ts{,x}"],
  },
});

API

Below you can find a description of the fields in the configuration for this plugin:

{
  // The name that the consumers will reference as the remote
  name: "xolvio_ui",

  files: {

    // an array of globs to match your component files
    paths: ["./src/components/**/*.ts{,x}"],

    // files with .stories. will get ignored, so they don't get exposed on the endpoints. Can also be a RegExp
    storiesExtension: ".stories." | /\.stories\.,

     // so your App can import "xolvio_ui/components/Title" instead of  "xolvio_ui/src/components/Title"
    removePrefix: "./src/",

  },

  // by default we share react and react-dom, you can add any aditional packages you would want to be shared
  shared: ["styled-components"],

  // you can import modules from other federated remotes into your Storybook as well!
  remotes: ["first-remote", "second-remote"]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].